1
0
Fork 0
pingvin-share/frontend/src/components/header/Header.tsx

223 lines
5.0 KiB
TypeScript
Raw Normal View History

2022-04-25 15:15:17 +02:00
import {
2022-10-10 22:30:04 +02:00
Box,
2022-04-25 15:15:17 +02:00
Burger,
Container,
2022-10-10 22:14:23 +02:00
createStyles,
2022-04-25 15:15:17 +02:00
Group,
Header as MantineHeader,
2022-04-25 15:15:17 +02:00
Paper,
2022-10-10 22:14:23 +02:00
Stack,
2022-04-25 15:15:17 +02:00
Text,
Transition,
} from "@mantine/core";
2022-10-10 22:14:23 +02:00
import { useDisclosure } from "@mantine/hooks";
2022-10-31 11:20:54 +01:00
import Link from "next/link";
2023-02-09 18:17:53 +01:00
import { useRouter } from "next/router";
import { ReactNode, useEffect, useState } from "react";
import useConfig from "../../hooks/config.hook";
import useUser from "../../hooks/user.hook";
import useTranslate from "../../hooks/useTranslate.hook";
2022-10-14 14:19:32 +02:00
import Logo from "../Logo";
2022-05-11 13:50:28 +02:00
import ActionAvatar from "./ActionAvatar";
import NavbarShareMenu from "./NavbarShareMenu";
2022-04-25 15:15:17 +02:00
2022-10-10 22:14:23 +02:00
const HEADER_HEIGHT = 60;
2024-03-22 01:35:55 +01:00
const webroot = process.env.WEBROOT || "";
2022-10-10 22:14:23 +02:00
2022-10-31 11:20:54 +01:00
type NavLink = {
2022-04-25 15:15:17 +02:00
link?: string;
2022-05-13 19:27:12 +02:00
label?: string;
component?: ReactNode;
2022-04-25 15:15:17 +02:00
action?: () => Promise<void>;
};
2022-10-10 22:14:23 +02:00
const useStyles = createStyles((theme) => ({
root: {
position: "relative",
zIndex: 1,
},
dropdown: {
position: "absolute",
top: HEADER_HEIGHT,
left: 0,
right: 0,
zIndex: 0,
borderTopRightRadius: 0,
borderTopLeftRadius: 0,
borderTopWidth: 0,
overflow: "hidden",
[theme.fn.largerThan("sm")]: {
display: "none",
},
},
header: {
display: "flex",
justifyContent: "space-between",
alignItems: "center",
height: "100%",
},
links: {
[theme.fn.smallerThan("sm")]: {
display: "none",
},
},
burger: {
[theme.fn.largerThan("sm")]: {
display: "none",
},
},
link: {
display: "block",
lineHeight: 1,
padding: "8px 12px",
borderRadius: theme.radius.sm,
textDecoration: "none",
color:
theme.colorScheme === "dark"
? theme.colors.dark[0]
: theme.colors.gray[7],
fontSize: theme.fontSizes.sm,
fontWeight: 500,
"&:hover": {
backgroundColor:
theme.colorScheme === "dark"
? theme.colors.dark[6]
: theme.colors.gray[0],
},
2022-10-10 22:14:23 +02:00
[theme.fn.smallerThan("sm")]: {
borderRadius: 0,
padding: theme.spacing.md,
},
},
linkActive: {
"&, &:hover": {
backgroundColor:
theme.colorScheme === "dark"
? theme.fn.rgba(theme.colors[theme.primaryColor][9], 0.25)
: theme.colors[theme.primaryColor][0],
color:
theme.colors[theme.primaryColor][theme.colorScheme === "dark" ? 3 : 7],
},
},
}));
2022-04-25 15:15:17 +02:00
const Header = () => {
const { user } = useUser();
2023-02-09 18:17:53 +01:00
const router = useRouter();
const config = useConfig();
const t = useTranslate();
2022-10-10 22:14:23 +02:00
const [opened, toggleOpened] = useDisclosure(false);
2023-02-09 18:17:53 +01:00
const [currentRoute, setCurrentRoute] = useState("");
useEffect(() => {
setCurrentRoute(router.pathname);
}, [router.pathname]);
const authenticatedLinks: NavLink[] = [
2022-05-02 11:19:24 +02:00
{
2024-03-22 01:35:55 +01:00
link: webroot + "/upload",
label: t("navbar.upload"),
2022-05-02 11:19:24 +02:00
},
{
component: <NavbarShareMenu />,
},
2022-05-13 19:27:12 +02:00
{
component: <ActionAvatar />,
},
2022-10-10 22:30:04 +02:00
];
2022-05-02 11:19:24 +02:00
2023-02-09 18:17:53 +01:00
let unauthenticatedLinks: NavLink[] = [
2022-05-02 11:19:24 +02:00
{
2024-03-22 01:35:55 +01:00
link: webroot + "/auth/signIn",
label: t("navbar.signin"),
2022-05-02 11:19:24 +02:00
},
2023-02-09 18:17:53 +01:00
];
2022-04-25 15:15:17 +02:00
if (config.get("share.allowUnauthenticatedShares")) {
2023-02-09 18:17:53 +01:00
unauthenticatedLinks.unshift({
2024-03-22 01:35:55 +01:00
link: webroot + "/upload",
label: t("navbar.upload"),
2023-02-09 18:17:53 +01:00
});
}
if (config.get("general.showHomePage"))
2023-02-09 18:17:53 +01:00
unauthenticatedLinks.unshift({
2024-03-22 01:35:55 +01:00
link: webroot + "/",
label: t("navbar.home"),
2023-02-09 18:17:53 +01:00
});
if (config.get("share.allowRegistration"))
2023-02-09 18:17:53 +01:00
unauthenticatedLinks.push({
2024-03-22 01:35:55 +01:00
link: webroot + "/auth/signUp",
label: t("navbar.signup"),
2023-02-09 18:17:53 +01:00
});
2022-10-10 22:14:23 +02:00
const { classes, cx } = useStyles();
const items = (
<>
{(user ? authenticatedLinks : unauthenticatedLinks).map((link, i) => {
2022-10-10 22:14:23 +02:00
if (link.component) {
return (
<Box pl={5} py={15} key={i}>
{link.component}
</Box>
2022-10-10 22:14:23 +02:00
);
}
return (
2022-10-31 11:20:54 +01:00
<Link
2022-10-10 22:14:23 +02:00
key={link.label}
href={link.link ?? ""}
onClick={() => toggleOpened.toggle()}
className={cx(classes.link, {
2023-02-09 18:17:53 +01:00
[classes.linkActive]: currentRoute == link.link,
2022-10-10 22:14:23 +02:00
})}
>
{link.label}
2022-10-31 11:20:54 +01:00
</Link>
2022-10-10 22:14:23 +02:00
);
})}
</>
);
2022-04-25 15:15:17 +02:00
return (
<MantineHeader height={HEADER_HEIGHT} mb={40} className={classes.root}>
2022-04-25 15:15:17 +02:00
<Container className={classes.header}>
2024-03-22 01:35:55 +01:00
<Link href={webroot + "/"} passHref>
2022-04-25 15:15:17 +02:00
<Group>
2022-10-14 14:19:32 +02:00
<Logo height={35} width={35} />
<Text weight={600}>{config.get("general.appName")}</Text>
2022-04-25 15:15:17 +02:00
</Group>
2022-10-31 11:20:54 +01:00
</Link>
2022-04-25 15:15:17 +02:00
<Group spacing={5} className={classes.links}>
2022-10-10 22:14:23 +02:00
<Group>{items} </Group>
2022-04-25 15:15:17 +02:00
</Group>
<Burger
opened={opened}
2022-10-10 22:14:23 +02:00
onClick={() => toggleOpened.toggle()}
2022-04-25 15:15:17 +02:00
className={classes.burger}
size="sm"
/>
<Transition transition="pop-top-right" duration={200} mounted={opened}>
{(styles) => (
<Paper className={classes.dropdown} withBorder style={styles}>
2022-10-10 22:14:23 +02:00
<Stack spacing={0}> {items}</Stack>
2022-04-25 15:15:17 +02:00
</Paper>
)}
</Transition>
</Container>
</MantineHeader>
2022-04-25 15:15:17 +02:00
);
};
2022-10-10 22:14:23 +02:00
export default Header;