1
1
'use client'
2
2
3
+ import { usePathname , useRouter , useSearchParams } from 'next/navigation'
3
4
import { Avatar } from '@/components/common/Avatar'
4
5
import { ModeToggle } from '@/components/common/ModeToggle'
5
6
import { TrustedDeviceManager } from '@/components/settings/account/TrustedDeviceManager'
@@ -11,26 +12,50 @@ import { userIsAdmin } from '@/utils/permissions'
11
12
import { Tab } from '@headlessui/react'
12
13
import clsx from 'clsx'
13
14
import { useSession } from 'next-auth/react'
14
- import { Fragment , useContext } from 'react'
15
+ import { Fragment , useContext , useEffect , useState } from 'react'
15
16
import { FaMoon , FaSun } from 'react-icons/fa'
16
17
import Spinner from '@/components/common/Spinner'
17
18
18
19
export default function Settings ( { params } : { params : { team : string } } ) {
20
+ const searchParams = useSearchParams ( )
21
+ const router = useRouter ( )
22
+ const pathname = usePathname ( )
23
+
19
24
const { activeOrganisation } = useContext ( organisationContext )
20
25
21
26
const { data : session } = useSession ( )
22
27
23
28
const activeUserIsAdmin = activeOrganisation ? userIsAdmin ( activeOrganisation . role ! ) : false
24
29
25
- const tabList = ( ) => [
30
+ const [ tabIndex , setTabIndex ] = useState ( 0 )
31
+
32
+ const tabList = [
26
33
...( activeUserIsAdmin ? [ { name : 'Organisation' } ] : [ ] ) ,
27
- ...[ { name : 'Account' } , { name : 'App' } ] ,
34
+ { name : 'Account' } ,
35
+ { name : 'App' } ,
28
36
]
29
37
38
+ useEffect ( ( ) => {
39
+ const initialTabName = searchParams ?. get ( 'tab' ) ?? 'organisation'
40
+ const initialTabIndex = tabList . findIndex (
41
+ ( tab ) => tab . name . toLowerCase ( ) === initialTabName . toLowerCase ( )
42
+ )
43
+
44
+ if ( initialTabIndex !== - 1 ) setTabIndex ( initialTabIndex )
45
+ // eslint-disable-next-line react-hooks/exhaustive-deps
46
+ } , [ activeUserIsAdmin , searchParams ] )
47
+
48
+ const updateTab = ( index : number ) => {
49
+ const tab = tabList [ index ]
50
+ const params = new URLSearchParams ( searchParams ?. toString ( ) )
51
+ params . set ( 'tab' , tab . name )
52
+ router . push ( `${ pathname } ?${ params . toString ( ) } ` )
53
+ }
54
+
30
55
if ( ! activeOrganisation )
31
56
return (
32
57
< div className = "flex items-center justify-center py-40" >
33
- < Spinner size = "md" /> { ' ' }
58
+ < Spinner size = "md" />
34
59
</ div >
35
60
)
36
61
@@ -39,17 +64,17 @@ export default function Settings({ params }: { params: { team: string } }) {
39
64
< h1 className = "text-3xl font-semibold" > Settings</ h1 >
40
65
41
66
< div className = "pt-8" >
42
- < Tab . Group >
67
+ < Tab . Group selectedIndex = { tabIndex } onChange = { ( index ) => updateTab ( index ) } >
43
68
< Tab . List className = "flex gap-4 w-full border-b border-neutral-500/20" >
44
- { tabList ( ) . map ( ( tab ) => (
69
+ { tabList . map ( ( tab ) => (
45
70
< Tab key = { tab . name } as = { Fragment } >
46
71
{ ( { selected } ) => (
47
72
< div
48
73
className = { clsx (
49
74
'p-3 font-medium border-b focus:outline-none text-black dark:text-white' ,
50
75
selected
51
76
? 'border-emerald-500 font-semibold'
52
- : ' border-transparent cursor-pointer'
77
+ : 'border-transparent cursor-pointer'
53
78
) }
54
79
>
55
80
{ tab . name }
@@ -63,7 +88,7 @@ export default function Settings({ params }: { params: { team: string } }) {
63
88
< div className = "max-h-[80vh] overflow-y-auto px-4" >
64
89
{ activeUserIsAdmin && (
65
90
< Tab . Panel >
66
- < div className = "space-y-10 py-4" >
91
+ < div className = "space-y-10 py-4" >
67
92
< div className = "space-y-1" >
68
93
< h2 className = "text-2xl font-semibold" > Organisation</ h2 >
69
94
< p className = "text-neutral-500" > Organisation info and settings</ p >
0 commit comments