Files
blitzkit/src/components/Navbar/index.tsx
T

263 lines
7.8 KiB
TypeScript
Raw Normal View History

2024-04-18 18:42:57 -05:00
import {
2024-05-14 14:07:44 -05:00
Cross1Icon,
2024-04-19 09:03:45 -05:00
DiscordLogoIcon,
2024-04-18 18:42:57 -05:00
GearIcon,
HamburgerMenuIcon,
2024-05-01 13:04:31 -05:00
PersonIcon,
2024-04-18 18:42:57 -05:00
} from '@radix-ui/react-icons';
2024-04-18 20:26:35 -05:00
import {
2024-05-14 14:07:44 -05:00
Box,
2024-05-01 13:04:31 -05:00
Button,
2024-05-01 16:33:22 -05:00
Dialog,
2024-04-18 20:26:35 -05:00
Flex,
Heading,
IconButton,
Link as LinkRadix,
Popover,
2024-05-01 13:04:31 -05:00
Spinner,
2024-05-14 14:07:44 -05:00
Text,
2024-04-18 20:26:35 -05:00
} from '@radix-ui/themes';
2023-09-12 02:38:50 +00:00
import Link from 'next/link';
2024-05-20 12:30:22 -05:00
import { Suspense, useEffect, useRef, useState } from 'react';
2024-05-14 14:07:44 -05:00
import * as styles from '../../app/page.css';
2024-05-01 20:54:27 -05:00
import { REGIONS, UNLOCALIZED_REGION_NAMES } from '../../constants/regions';
2024-04-18 20:26:35 -05:00
import { TOOLS } from '../../constants/tools';
2024-05-01 13:04:31 -05:00
import { authURL } from '../../core/blitz/authURL';
import { extendAuth } from '../../core/blitz/extendAuth';
import { logout } from '../../core/blitz/logout';
2024-05-01 16:33:22 -05:00
import { useWideFormat } from '../../hooks/useWideFormat';
2024-04-23 10:19:32 -05:00
import { BlitzkitWide } from '../../icons/BlitzkitWide';
2024-04-19 09:03:45 -05:00
import { PatreonIcon } from '../../icons/Patreon';
2024-03-12 23:36:56 -05:00
import { theme } from '../../stitches.config';
2024-05-01 13:04:31 -05:00
import { useApp } from '../../stores/app';
import { LoggedIn } from './components/LoggedIn';
2023-09-07 21:25:37 +00:00
2024-05-14 14:07:44 -05:00
export const NAVBAR_HEIGHT = 64;
2023-09-07 21:25:37 +00:00
export default function Navbar() {
2024-05-01 16:33:22 -05:00
const wideFormat = useWideFormat(480);
2024-05-01 13:04:31 -05:00
const login = useApp((state) => state.login);
2024-05-20 12:30:22 -05:00
const [showHamburgerMenu, setShowHamburgerMenu] = useState(false);
const navbar = useRef<HTMLDivElement>(null);
2024-05-01 13:04:31 -05:00
useEffect(() => {
if (!login) return;
const expiresIn = login.expiresAt - Date.now() / 1000;
const expiresInDays = expiresIn / 60 / 60 / 24;
if (expiresInDays < 0) {
logout();
} else if (expiresInDays < 7) {
extendAuth();
}
});
2024-04-01 21:33:47 -05:00
2024-05-01 16:33:22 -05:00
const plugs = (
<>
<Link
2024-05-19 21:09:49 -05:00
onClick={() => setShowHamburgerMenu(false)}
2024-05-01 16:33:22 -05:00
href="https://discord.gg/nDt7AjGJQH"
target="_blank"
style={{
color: 'inherit',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<DiscordLogoIcon width={16} height={16} />
</Link>
<Link
2024-05-19 21:09:49 -05:00
onClick={() => setShowHamburgerMenu(false)}
2024-05-01 16:33:22 -05:00
href="https://www.patreon.com/tresabhi"
target="_blank"
style={{
color: 'inherit',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<PatreonIcon width={14} height={14} />
</Link>
</>
);
2023-09-07 21:25:37 +00:00
return (
2024-05-20 12:30:22 -05:00
<Box
ref={navbar}
style={{
position: 'fixed',
top: 0,
left: 0,
width: '100%',
maxHeight: showHamburgerMenu ? '100%' : NAVBAR_HEIGHT + 1, // extra pixel for frost bleeding
zIndex: 2,
padding: '1rem',
background: theme.colors.appBackground1_alpha,
borderBottom: theme.borderStyles.nonInteractive,
backdropFilter: 'blur(4rem)',
2024-05-20 12:31:43 -05:00
WebkitBackdropFilter: 'blur(4rem)',
2024-05-20 12:30:22 -05:00
boxSizing: 'border-box',
transition: 'max-height 0.5s ease',
overflow: 'hidden',
}}
onPointerEnter={() => setShowHamburgerMenu(true)}
onPointerLeave={(event) => {
if (
navbar.current ===
document.elementFromPoint(event.clientX, event.clientY)
) {
// false positive bug: https://stackoverflow.com/a/45267167/12294756
return;
}
setShowHamburgerMenu(false);
}}
onFocus={() => setShowHamburgerMenu(true)}
onBlur={() => setShowHamburgerMenu(false)}
>
<Flex direction="column" align="center" pt="2">
<Flex
justify="between"
align="center"
style={{ maxWidth: 1600, width: '100%', padding: '0 16px' }}
gap="4"
>
<IconButton
variant="ghost"
color="gray"
onClick={() => setShowHamburgerMenu((state) => !state)}
2023-12-04 19:27:23 -06:00
>
2024-05-20 12:30:22 -05:00
{showHamburgerMenu ? <Cross1Icon /> : <HamburgerMenuIcon />}
</IconButton>
2023-12-04 19:27:23 -06:00
2024-05-20 12:30:22 -05:00
<Link
onClick={() => setShowHamburgerMenu(false)}
href="/"
style={{
color: 'inherit',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<BlitzkitWide />
</Link>
2024-04-18 18:42:57 -05:00
2024-05-20 12:30:22 -05:00
<div style={{ flex: 1 }} />
2024-05-01 16:33:22 -05:00
2024-05-20 12:30:22 -05:00
{wideFormat && plugs}
2024-04-01 21:46:17 -05:00
2024-05-20 12:30:22 -05:00
<Link href="/settings" onClick={() => setShowHamburgerMenu(false)}>
<Flex style={{ width: '100%', height: '100%' }} justify="center">
<IconButton variant="ghost" color="gray">
<GearIcon />
</IconButton>
</Flex>
</Link>
{login && (
<Popover.Root>
<Popover.Trigger>
2024-05-01 16:33:22 -05:00
<IconButton variant="ghost" color="gray">
2024-05-20 12:30:22 -05:00
<PersonIcon />
2024-05-01 16:33:22 -05:00
</IconButton>
2024-05-20 12:30:22 -05:00
</Popover.Trigger>
2024-04-18 18:42:57 -05:00
2024-05-20 12:30:22 -05:00
<Popover.Content align="center">
<Flex direction="column" gap="2">
<Suspense fallback={<Spinner />}>
<LoggedIn />
</Suspense>
</Flex>
</Popover.Content>
</Popover.Root>
)}
2024-05-01 13:04:31 -05:00
2024-05-20 12:30:22 -05:00
{/* TODO: re-enable in the future */}
{!login && false && (
<Dialog.Root>
<Dialog.Trigger>
<Button variant="ghost">Log in</Button>
</Dialog.Trigger>
2024-05-14 14:07:44 -05:00
2024-05-20 12:30:22 -05:00
<Dialog.Content
2024-05-20 12:07:00 -05:00
style={{
2024-05-20 12:30:22 -05:00
maxWidth: 360,
2024-05-20 12:07:00 -05:00
}}
>
2024-05-20 12:30:22 -05:00
<Flex direction="column" gap="2" align="center">
<Heading size="3">Choose your region</Heading>
<Flex gap="4" align="center" justify="center" wrap="wrap">
{REGIONS.map((region) => (
<LinkRadix
onClick={() => setShowHamburgerMenu(false)}
key={region}
href={authURL(
region,
typeof window === 'undefined'
? undefined
: location.href,
)}
>
{UNLOCALIZED_REGION_NAMES[region]}
</LinkRadix>
))}
</Flex>
</Flex>
</Dialog.Content>
</Dialog.Root>
)}
2023-12-04 19:27:23 -06:00
</Flex>
2024-05-20 12:30:22 -05:00
<Flex mt="6" gap="3" justify="center" wrap="wrap">
{TOOLS.filter((tool) => !tool.href).map((tool) => (
<Link
href={tool.href ?? `/tools/${tool.id}`}
target={tool.href ? '_blank' : undefined}
className={
tool.disabled ? styles.tool.disabled : styles.tool.enabled
}
style={{
backgroundImage: `url(/assets/banners/${tool.id}.webp)`,
cursor: tool.disabled ? 'default' : 'pointer',
opacity: tool.disabled ? 0.25 : 1,
height: 64,
minWidth: 144,
maxWidth: 192,
flex: 1,
borderRadius: 8,
textDecoration: 'none',
backgroundSize: 'cover',
backgroundPosition: 'center',
overflow: 'hidden',
display: 'flex',
flexDirection: 'column',
alignItems: 'flex-start',
justifyContent: 'flex-end',
padding: 8,
transition: `box-shadow ${theme.durations.regular}`,
}}
onClick={() => setShowHamburgerMenu(false)}
>
<Text
style={{
color: theme.colors.textHighContrast,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
{tool.title}
</Text>
</Link>
))}
</Flex>
</Flex>
</Box>
2023-09-07 21:25:37 +00:00
);
}