Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions torchci/components/benchmark_v3/pages/BenchmarkListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ export function getBenchmarkMainRouteById(id: string): string | undefined {
export function benchmarkCategoryCardToNavGroup(
categories: BenchmarkCategoryGroup[]
): (NavCategory | NavItem | NavDivider)[] {
const items: (NavCategory | NavItem)[] = categories
const items = categories
.map((c: BenchmarkCategoryGroup) => {
if (c.items.length === 1) {
const item: NavItem = {
const item = {
label: c.items[0].name,
route: c.items[0].route,
type: "item",
type: "item" as const,
};
return item;
}
const group: NavCategory = {
const group = {
label: c.title,
items: c.items
.sort((a, b) => a.name.localeCompare(b.name))
.map((i) => ({ label: i.name, route: i.route, type: "item" })),
type: "group",
.map((i) => ({
label: i.name,
route: i.route,
type: "item" as const,
})),
type: "group" as const,
};
return group;
})
Expand All @@ -53,10 +57,10 @@ export function benchmarkCategoryCardToNavGroup(

return [
...items,
{ type: "divider" },
{ type: "divider" as const },
{
label: "View All Benchmarks",
type: "item",
type: "item" as const,
route: "/benchmark/benchmark_list",
},
];
Expand Down
128 changes: 71 additions & 57 deletions torchci/components/layout/LoginSection.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import QuestionMarkIcon from "@mui/icons-material/QuestionMark";
import SyncIcon from "@mui/icons-material/Sync";
import { Button as _Button, Menu, MenuItem } from "@mui/material";
import { Button as _Button, Link } from "@mui/material";
import { Box } from "@mui/system";
import { signIn, signOut, useSession } from "next-auth/react";
import Link from "next/link";
import React from "react";
import styles from "./LoginSection.module.css";
import { NavBarGroupDropdown, NavItem } from "./NavBarGroupDropdown";

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

export default function LoginSection() {
const { data: session, status } = useSession();
const [anchorEl, setAnchorEl] = React.useState(null);
const onClick = (event: any) => {
setAnchorEl(event.currentTarget);
};
const onClose = () => {
setAnchorEl(null);
};

return (
<>
{status == "loading" && (
// Shows up very briefly while api responds
<Button disabled>
<SyncIcon fontSize="inherit" />
</Button>
)}
{status != "loading" && !session?.user && (
<Link
href={`/api/auth/signin`}
if (status === "loading") {
return (
<Button disabled>
<SyncIcon fontSize="inherit" />
</Button>
);
}

// If not signed in, just show a sign in button (no dropdown)
if (!session?.user) {
return (
<Link
href={`/api/auth/signin`}
onClick={(e) => {
e.preventDefault();
signIn();
}}
>
<Button variant="contained">Sign in</Button>
</Link>
);
}

// If signed in, show dropdown with user info
const items: NavItem[] = [
{
label: (
<Box
component="span"
sx={{ color: "text.secondary" }}
onClick={(e) => e.preventDefault()}
>
Signed in as {session.user.name}
</Box>
),
type: "item",
route: "#",
},
{
type: "item",
route: "/api/auth/signout",
label: (
<Box
component="span"
onClick={(e) => {
e.preventDefault();
signIn();
signOut();
}}
style={{
textDecoration: "none",
color: "inherit",
cursor: "pointer",
}}
>
<Button variant="contained">Sign in</Button>
</Link>
)}
{session && (
<>
<Button onClick={onClick}>
{session.user?.image ? (
<img
style={{
backgroundImage: `url('${session.user.image}')`,
}}
className={styles.avatar}
/>
) : (
// Hopefully shouldn't get here
<QuestionMarkIcon fontSize="inherit" />
)}
</Button>
<Menu anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={onClose}>
{session.user?.name && (
<MenuItem>Signed in as {session.user.name}</MenuItem>
)}
<Link
href={`/api/auth/signout`}
onClick={(e) => {
e.preventDefault();
signOut();
}}
>
<MenuItem>Sign out</MenuItem>
</Link>
</Menu>
</>
)}
</>
Sign out
</Box>
),
},
];

const title = session?.user?.image ? (
<img
style={{
backgroundImage: `url('${session.user.image}')`,
}}
className={styles.avatar}
/>
) : (
// Hopefully shouldn't get here
<QuestionMarkIcon fontSize="inherit" />
);

return <NavBarGroupDropdown items={items} title={title} showCarrot={false} />;
}
4 changes: 1 addition & 3 deletions torchci/components/layout/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ function NavBar() {
<li>
<ThemeModePicker />
</li>
<li>
<LoginSection></LoginSection>
</li>
<LoginSection></LoginSection>
</ul>
</div>
</div>
Expand Down
14 changes: 11 additions & 3 deletions torchci/components/layout/NavBarGroupDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
import { Box } from "@mui/system";
import { Fragment, useEffect, useMemo, useRef, useState } from "react";

export type NavItem = { label: string; route: string; type: "item" };
export type NavItem = {
label: string | JSX.Element;
route: string;
type: "item";
};
export type NavCategory = { label: string; items: NavItem[]; type: "group" };
export type NavDivider = { type: "divider" };

Expand All @@ -23,9 +27,11 @@ export type NavDivider = { type: "divider" };
export function NavBarGroupDropdown({
title,
items,
showCarrot = true,
}: {
title: string;
title: string | JSX.Element;
items: (NavCategory | NavItem | NavDivider)[];
showCarrot?: boolean;
}) {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
Expand Down Expand Up @@ -86,9 +92,11 @@ export function NavBarGroupDropdown({
fontFamily: "inherit",
fontSize: "inherit",
fontWeight: 400,
minWidth: 0,
}}
>
{title} ▾
{title}
{showCarrot && " ▾"}
</Button>
<Popper
open={open}
Expand Down