@@ -4,19 +4,40 @@ import {
4
4
GlobeAltIcon ,
5
5
UserGroupIcon ,
6
6
EnvelopeIcon ,
7
- KeyIcon
7
+ KeyIcon ,
8
8
} from "@heroicons/react/24/outline" ;
9
9
import UsersSettings from "./users-settings" ;
10
10
import WebhookSettings from "./webhook-settings" ;
11
11
import APIKeySettings from "./api-key-settings" ;
12
- import { useSession } from "next-auth/react"
12
+ import { useSession } from "next-auth/react" ;
13
13
import Loading from "app/loading" ;
14
14
import SmtpSettings from "./smtp-settings" ;
15
- import { useRouter } from "next/navigation" ;
15
+ import { usePathname , useRouter , useSearchParams } from "next/navigation" ;
16
+ import { useState } from "react" ;
16
17
17
18
export default function SettingsPage ( ) {
18
19
const { data : session , status } = useSession ( ) ;
19
20
const router = useRouter ( ) ;
21
+ const searchParams = useSearchParams ( ) ! ;
22
+ const pathname = usePathname ( ) ;
23
+ const [ selectedTab , setSelectedTab ] = useState < string > (
24
+ searchParams ?. get ( "selectedTab" ) || "users"
25
+ ) ;
26
+
27
+ const handleTabChange = ( tab : string ) => {
28
+ setSelectedTab ( tab ) ;
29
+ router . push ( `${ pathname } ?selectedTab=${ tab } ` ) ;
30
+ } ;
31
+
32
+ // TODO: more robust way to handle this
33
+ const tabIndex =
34
+ selectedTab === "users"
35
+ ? 0
36
+ : selectedTab === "webhook"
37
+ ? 1
38
+ : selectedTab === "smtp"
39
+ ? 2
40
+ : 3 ;
20
41
21
42
if ( status === "loading" ) return < Loading /> ;
22
43
if ( status === "unauthenticated" ) router . push ( "/signin" ) ;
@@ -28,28 +49,46 @@ export default function SettingsPage() {
28
49
* Think about a proper way to implement it.
29
50
*/
30
51
return (
31
- < TabGroup >
52
+ < TabGroup index = { tabIndex } >
32
53
< TabList color = "orange" >
33
- < Tab icon = { UserGroupIcon } > Users</ Tab >
34
- < Tab icon = { GlobeAltIcon } > Webhook</ Tab >
35
- < Tab icon = { EnvelopeIcon } > SMTP</ Tab >
36
- < Tab icon = { KeyIcon } > Api Key</ Tab >
54
+ < Tab icon = { UserGroupIcon } onClick = { ( ) => handleTabChange ( "users" ) } >
55
+ Users
56
+ </ Tab >
57
+ < Tab icon = { GlobeAltIcon } onClick = { ( ) => handleTabChange ( "webhook" ) } >
58
+ Webhook
59
+ </ Tab >
60
+ < Tab icon = { EnvelopeIcon } onClick = { ( ) => handleTabChange ( "smtp" ) } >
61
+ SMTP
62
+ </ Tab >
63
+ < Tab icon = { KeyIcon } onClick = { ( ) => handleTabChange ( "api-key" ) } >
64
+ API Key
65
+ </ Tab >
37
66
</ TabList >
38
67
< TabPanels >
39
68
< TabPanel >
40
69
< UsersSettings
41
70
accessToken = { session ?. accessToken ! }
42
71
currentUser = { session ?. user }
72
+ selectedTab = { selectedTab }
43
73
/>
44
74
</ TabPanel >
45
75
< TabPanel >
46
- < WebhookSettings accessToken = { session ?. accessToken ! } />
76
+ < WebhookSettings
77
+ accessToken = { session ?. accessToken ! }
78
+ selectedTab = { selectedTab }
79
+ />
47
80
</ TabPanel >
48
81
< TabPanel >
49
- < SmtpSettings accessToken = { session ?. accessToken ! } />
82
+ < SmtpSettings
83
+ accessToken = { session ?. accessToken ! }
84
+ selectedTab = { selectedTab }
85
+ />
50
86
</ TabPanel >
51
87
< TabPanel >
52
- < APIKeySettings accessToken = { session ?. accessToken ! } />
88
+ < APIKeySettings
89
+ accessToken = { session ?. accessToken ! }
90
+ selectedTab = { selectedTab }
91
+ />
53
92
</ TabPanel >
54
93
</ TabPanels >
55
94
</ TabGroup >
0 commit comments