-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathNavUserProfile.tsx
More file actions
148 lines (138 loc) · 3.98 KB
/
NavUserProfile.tsx
File metadata and controls
148 lines (138 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { useState } from "react";
import {
Alert,
Avatar,
Button,
Divider,
IconButton,
ListItemIcon,
Menu,
MenuItem,
Tooltip,
type AlertColor,
} from "@mui/material";
import AccountCircleIcon from "@mui/icons-material/AccountCircle";
import LogoutIcon from "@mui/icons-material/Logout";
import LoginIcon from "@mui/icons-material/Login";
import AlertBar from "../alert/AlertBar";
import { useAuthStore } from "../../lib/store/authStore";
// Optional: reduce re-renders
const styles = {
loginButoon: {
borderRadius: 300,
padding: "5px 10px", // top/bottom, left/right
},
};
export default function NavUserProfile() {
const me = useAuthStore((s) => s.me);
const loading = useAuthStore((s) => s.loading);
const logoutAndRefresh = useAuthStore((s) => s.logoutAndRefresh);
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const [notification, setNotification] = useState<{
open: boolean;
message: string;
title?: string;
severity?: AlertColor;
}>({ open: false, message: "", title: "", severity: "info" });
const menuOpen = Boolean(anchorEl);
const handleOpen = (e: React.MouseEvent<HTMLElement>) =>
setAnchorEl(e.currentTarget);
const closeMenu = () => setAnchorEl(null);
const onLogout = async () => {
closeMenu();
const result = await logoutAndRefresh();
if (result.ok) {
setNotification({
open: true,
title: "Success",
message: "Successfully logged out",
severity: "success",
});
} else {
setNotification({
open: true,
title: "Logout failed",
message: result.error?.message ?? "Unknown error, please try again",
severity: "error",
});
}
};
const defaultUser = () => (
<Tooltip title="Login">
<Button
variant="outlined"
href={"/v2/login"}
size="small"
sx={styles.loginButoon}
startIcon={<LoginIcon />}
data-testid="login-btn"
>
Login
</Button>
</Tooltip>
);
if (loading) return <div />;
const isAuthed = !!(me && me.authenticated);
const name = me?.user?.display_name ?? "unknown";
const avatarUrl = me?.user?.avatar_url ?? undefined;
return (
<>
{isAuthed ? (
<>
<Tooltip title={name}>
<IconButton
onClick={handleOpen}
size="small"
sx={{ ml: 1 }}
aria-controls={menuOpen ? "user-menu" : undefined}
aria-haspopup="true"
aria-expanded={menuOpen ? "true" : undefined}
data-testid="profile-avatar"
>
{avatarUrl ? (
<Avatar
src={avatarUrl}
alt={name}
sx={{ width: 32, height: 32 }}
>
{name?.[0]}
</Avatar>
) : (
<AccountCircleIcon />
)}
</IconButton>
</Tooltip>
<Menu
anchorEl={anchorEl}
id="user-menu"
open={menuOpen}
onClose={closeMenu}
transformOrigin={{ horizontal: "right", vertical: "top" }}
anchorOrigin={{ horizontal: "right", vertical: "bottom" }}
>
<MenuItem disabled data-testid="profile-btn">
<ListItemIcon>
<AccountCircleIcon fontSize="small" />
</ListItemIcon>
{name} (Coming soon)
</MenuItem>
<Divider />
<MenuItem onClick={onLogout} data-testid="logout-btn">
<ListItemIcon>
<LogoutIcon fontSize="small" />
</ListItemIcon>
Logout
</MenuItem>
</Menu>
</>
) : (
defaultUser()
)}
{/* Always render snackbar so it shows even after auth state changes */}
<AlertBar
notice={notification}
onClose={() => setNotification({ ...notification, open: false })}
/>
</>
);
}