11import QuestionMarkIcon from "@mui/icons-material/QuestionMark" ;
22import SyncIcon from "@mui/icons-material/Sync" ;
3- import { Button as _Button , Menu , MenuItem } from "@mui/material" ;
3+ import { Button as _Button , Link } from "@mui/material" ;
4+ import { Box } from "@mui/system" ;
45import { signIn , signOut , useSession } from "next-auth/react" ;
5- import Link from "next/link" ;
6- import React from "react" ;
76import styles from "./LoginSection.module.css" ;
7+ import { NavBarGroupDropdown , NavItem } from "./NavBarGroupDropdown" ;
88
99const Button = ( props : any ) => {
1010 // Make button as small as possible
@@ -13,64 +13,78 @@ const Button = (props: any) => {
1313
1414export default function LoginSection ( ) {
1515 const { data : session , status } = useSession ( ) ;
16- const [ anchorEl , setAnchorEl ] = React . useState ( null ) ;
17- const onClick = ( event : any ) => {
18- setAnchorEl ( event . currentTarget ) ;
19- } ;
20- const onClose = ( ) => {
21- setAnchorEl ( null ) ;
22- } ;
2316
24- return (
25- < >
26- { status == "loading" && (
27- // Shows up very briefly while api responds
28- < Button disabled >
29- < SyncIcon fontSize = "inherit" />
30- </ Button >
31- ) }
32- { status != "loading" && ! session ?. user && (
33- < Link
34- href = { `/api/auth/signin` }
17+ if ( status === "loading" ) {
18+ return (
19+ < Button disabled >
20+ < SyncIcon fontSize = "inherit" />
21+ </ Button >
22+ ) ;
23+ }
24+
25+ // If not signed in, just show a sign in button (no dropdown)
26+ if ( ! session ?. user ) {
27+ return (
28+ < Link
29+ href = { `/api/auth/signin` }
30+ onClick = { ( e ) => {
31+ e . preventDefault ( ) ;
32+ signIn ( ) ;
33+ } }
34+ >
35+ < Button variant = "contained" > Sign in</ Button >
36+ </ Link >
37+ ) ;
38+ }
39+
40+ // If signed in, show dropdown with user info
41+ const items : NavItem [ ] = [
42+ {
43+ label : (
44+ < Box
45+ component = "span"
46+ sx = { { color : "text.secondary" } }
47+ onClick = { ( e ) => e . preventDefault ( ) }
48+ >
49+ Signed in as { session . user . name }
50+ </ Box >
51+ ) ,
52+ type : "item" ,
53+ route : "#" ,
54+ } ,
55+ {
56+ type : "item" ,
57+ route : "/api/auth/signout" ,
58+ label : (
59+ < Box
60+ component = "span"
3561 onClick = { ( e ) => {
3662 e . preventDefault ( ) ;
37- signIn ( ) ;
63+ signOut ( ) ;
64+ } }
65+ style = { {
66+ textDecoration : "none" ,
67+ color : "inherit" ,
68+ cursor : "pointer" ,
3869 } }
3970 >
40- < Button variant = "contained" > Sign in</ Button >
41- </ Link >
42- ) }
43- { session && (
44- < >
45- < Button onClick = { onClick } >
46- { session . user ?. image ? (
47- < img
48- style = { {
49- backgroundImage : `url('${ session . user . image } ')` ,
50- } }
51- className = { styles . avatar }
52- />
53- ) : (
54- // Hopefully shouldn't get here
55- < QuestionMarkIcon fontSize = "inherit" />
56- ) }
57- </ Button >
58- < Menu anchorEl = { anchorEl } open = { Boolean ( anchorEl ) } onClose = { onClose } >
59- { session . user ?. name && (
60- < MenuItem > Signed in as { session . user . name } </ MenuItem >
61- ) }
62- < Link
63- href = { `/api/auth/signout` }
64- onClick = { ( e ) => {
65- e . preventDefault ( ) ;
66- signOut ( ) ;
67- } }
68- >
69- < MenuItem > Sign out</ MenuItem >
70- </ Link >
71- </ Menu >
72- </ >
73- ) }
74- </ >
71+ Sign out
72+ </ Box >
73+ ) ,
74+ } ,
75+ ] ;
76+
77+ const title = session ?. user ?. image ? (
78+ < img
79+ style = { {
80+ backgroundImage : `url('${ session . user . image } ')` ,
81+ } }
82+ className = { styles . avatar }
83+ />
84+ ) : (
85+ // Hopefully shouldn't get here
86+ < QuestionMarkIcon fontSize = "inherit" />
7587 ) ;
88+
89+ return < NavBarGroupDropdown items = { items } title = { title } showCarrot = { false } /> ;
7690}
0 commit comments