Files
swiss-datashare/frontend/src/components/navBar/NavBar.tsx

132 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-04-25 15:15:17 +02:00
import {
Burger,
Container,
Group,
Header as MantineHeader,
Paper,
Text,
Transition,
} from "@mantine/core";
import { useBooleanToggle } from "@mantine/hooks";
import { NextLink } from "@mantine/next";
import getConfig from "next/config";
2022-04-25 15:15:17 +02:00
import Image from "next/image";
import { ReactNode, useEffect, useState } from "react";
import useUser from "../../hooks/user.hook";
2022-04-25 15:15:17 +02:00
import headerStyle from "../../styles/header.style";
2022-05-11 13:50:28 +02:00
import ActionAvatar from "./ActionAvatar";
2022-04-25 15:15:17 +02:00
const { publicRuntimeConfig } = getConfig();
2022-04-25 15:15:17 +02:00
type Link = {
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>;
};
const Header = () => {
const [opened, toggleOpened] = useBooleanToggle(false);
const [active, setActive] = useState<string>();
const user = useUser();
2022-04-25 15:15:17 +02:00
const { classes, cx } = headerStyle();
2022-05-02 11:19:24 +02:00
const authenticatedLinks: Link[] = [
{
link: "/upload",
label: "Upload",
},
2022-05-13 19:27:12 +02:00
{
component: <ActionAvatar />,
},
2022-05-02 11:19:24 +02:00
];
const unauthenticatedLinks: Link[] | undefined = [
{
link: "/auth/signIn",
label: "Sign in",
},
];
if (publicRuntimeConfig.SHOW_HOME_PAGE == "true")
2022-05-11 13:50:28 +02:00
unauthenticatedLinks.unshift({
link: "/",
label: "Home",
});
if (publicRuntimeConfig.ALLOW_REGISTRATION == "true")
2022-05-02 11:19:24 +02:00
unauthenticatedLinks.push({
link: "/auth/signUp",
label: "Sign up",
});
const links = user ? authenticatedLinks : unauthenticatedLinks;
2022-04-25 15:15:17 +02:00
const items = links.map((link, i) => {
2022-05-13 19:27:12 +02:00
if (link.component) {
return (
<Container key={i} pl={5} py={15}>
{link.component}
</Container>
2022-05-13 19:27:12 +02:00
);
}
2022-05-02 11:19:24 +02:00
if (link) {
// eslint-disable-next-line react-hooks/rules-of-hooks
2022-05-11 13:50:28 +02:00
useEffect(() => {
if (window.location.pathname == link.link) {
setActive(link.link);
}
}, []);
2022-05-02 11:19:24 +02:00
return (
<NextLink
key={link.label}
href={link.link ?? ""}
onClick={link.action}
className={cx(classes.link, {
[classes.linkActive]: link.link && active === link.link,
})}
>
{link.label}
</NextLink>
);
}
2022-04-25 15:15:17 +02:00
});
return (
<MantineHeader height={60} mb={20} className={classes.root}>
<Container className={classes.header}>
<NextLink href="/">
<Group>
<Image
2022-04-28 16:28:37 +02:00
src="/img/logo.svg"
2022-04-25 15:15:17 +02:00
alt="Pinvgin Share Logo"
height={40}
width={40}
/>
<Text weight={600}>Pingvin Share</Text>
</Group>
</NextLink>
<Group spacing={5} className={classes.links}>
{items}
</Group>
<Burger
opened={opened}
onClick={() => toggleOpened()}
className={classes.burger}
size="sm"
/>
<Transition transition="pop-top-right" duration={200} mounted={opened}>
{(styles) => (
<Paper className={classes.dropdown} withBorder style={styles}>
{items}
</Paper>
)}
</Transition>
</Container>
</MantineHeader>
);
};
export default Header;