Skip to content

Commit 82789f4

Browse files
committed
login dropdown
1 parent 2ae496f commit 82789f4

File tree

3 files changed

+83
-63
lines changed

3 files changed

+83
-63
lines changed
Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import QuestionMarkIcon from "@mui/icons-material/QuestionMark";
22
import 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";
45
import { signIn, signOut, useSession } from "next-auth/react";
5-
import Link from "next/link";
6-
import React from "react";
76
import styles from "./LoginSection.module.css";
7+
import { NavBarGroupDropdown, NavItem } from "./NavBarGroupDropdown";
88

99
const Button = (props: any) => {
1010
// Make button as small as possible
@@ -13,64 +13,78 @@ const Button = (props: any) => {
1313

1414
export 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
}

torchci/components/layout/NavBar.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ function NavBar() {
268268
<li>
269269
<ThemeModePicker />
270270
</li>
271-
<li>
272-
<LoginSection></LoginSection>
273-
</li>
271+
<LoginSection></LoginSection>
274272
</ul>
275273
</div>
276274
</div>

torchci/components/layout/NavBarGroupDropdown.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
import { Box } from "@mui/system";
1111
import { Fragment, useEffect, useMemo, useRef, useState } from "react";
1212

13-
export type NavItem = { label: string; route: string; type: "item" };
13+
export type NavItem = {
14+
label: string | JSX.Element;
15+
route: string;
16+
type: "item";
17+
};
1418
export type NavCategory = { label: string; items: NavItem[]; type: "group" };
1519
export type NavDivider = { type: "divider" };
1620

@@ -23,9 +27,11 @@ export type NavDivider = { type: "divider" };
2327
export function NavBarGroupDropdown({
2428
title,
2529
items,
30+
showCarrot = true,
2631
}: {
27-
title: string;
32+
title: string | JSX.Element;
2833
items: (NavCategory | NavItem | NavDivider)[];
34+
showCarrot?: boolean;
2935
}) {
3036
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
3137
const open = Boolean(anchorEl);
@@ -86,9 +92,11 @@ export function NavBarGroupDropdown({
8692
fontFamily: "inherit",
8793
fontSize: "inherit",
8894
fontWeight: 400,
95+
minWidth: 0,
8996
}}
9097
>
91-
{title}
98+
{title}
99+
{showCarrot && " ▾"}
92100
</Button>
93101
<Popper
94102
open={open}

0 commit comments

Comments
 (0)