@@ -2,6 +2,7 @@ import {twMerge} from "tailwind-merge"
2
2
import { ForwardedRef , forwardRef , useState } from "react"
3
3
import { HiSwitchHorizontal , HiLogout , HiUserAdd } from "react-icons/hi"
4
4
import Link from "next/link"
5
+ import { useRouter } from "next/navigation" ;
5
6
6
7
const AccountList = ( props : { accountList : string [ ] } ) => {
7
8
return < div className = "flex flex-col" >
@@ -31,26 +32,36 @@ const AccountList = (props: { accountList: string[] }) => {
31
32
} ;
32
33
33
34
const SignOutOfAllButton = ( ) => {
34
- return < Link
35
+ const router = useRouter ( ) ;
36
+
37
+ const signOut = async ( ) => {
38
+ const request = new Request ( '/api/auth/logout' , {
39
+ method : 'POST'
40
+ } )
41
+ //TODO: handle errors
42
+ await fetch ( request )
43
+ router . push ( '/auth/login' )
44
+ } ;
45
+
46
+ return < div
35
47
className = { twMerge (
36
48
"text-text-800 hover:bg-base-warning-200 hover:cursor-pointer" ,
37
49
"dark:text-text-100 dark:hover:bg-base-warning-600" ,
38
50
"flex items-center justify-between py-2 px-1 space-x-4" ,
39
51
"text-right" ,
40
52
) }
41
- href = "/api/auth/logout"
42
- prefetch = { false }
53
+ onClick = { ( ) => signOut ( ) }
43
54
>
44
- < span > < b > Sign out</ b > of all accounts</ span >
55
+ < span > Sign < b > out</ b > of all accounts</ span >
45
56
< HiLogout className = "dark:text-text-100 text-2xl text-text-900 shrink-0" />
46
- </ Link >
57
+ </ div >
47
58
}
48
59
49
60
const SignIntoButton = ( ) => {
50
61
return < Link
51
62
className = { twMerge (
52
- "text-text-800 hover:bg-base-warning -200 hover:cursor-pointer" ,
53
- "dark:text-text-100 dark:hover:bg-base-warning -600" ,
63
+ "text-text-800 hover:bg-base-success -200 hover:cursor-pointer" ,
64
+ "dark:text-text-100 dark:hover:bg-base-success -600" ,
54
65
"flex items-center justify-between py-2 px-1 space-x-4" ,
55
66
"text-right" ,
56
67
) }
@@ -71,6 +82,7 @@ export const AccountDropdown = forwardRef(function AccountDropdown
71
82
} ,
72
83
ref : ForwardedRef < HTMLDivElement >
73
84
) {
85
+ const hasAccountChoice = props . accountsPossible . length !== 1 ;
74
86
return (
75
87
76
88
< div
@@ -112,10 +124,11 @@ export const AccountDropdown = forwardRef(function AccountDropdown
112
124
</ a >
113
125
</ div >
114
126
</ div >
115
- { props . accountsPossible . length !== 1 &&
127
+ { hasAccountChoice &&
116
128
< AccountList accountList = { props . accountsPossible . filter ( account => account !== props . accountActive ) } />
117
129
}
118
130
< SignIntoButton />
131
+ { hasAccountChoice && < SignOutOfAllButton /> }
119
132
</ div >
120
133
)
121
134
}
0 commit comments