2022-05-12 16:41:32 +02:00
|
|
|
import { useEffect, useState } from "react";
|
2022-12-22 15:49:08 +01:00
|
|
|
import { List, MoonStars, Sun } from "react-bootstrap-icons";
|
2022-05-10 16:33:29 +02:00
|
|
|
|
2022-08-16 00:01:54 +02:00
|
|
|
import Link from "next/link";
|
2022-05-12 16:41:32 +02:00
|
|
|
import { useRecoilState } from "recoil";
|
2022-12-22 15:49:08 +01:00
|
|
|
|
|
|
|
import { APIError, ErrorCode, fetchAPI, MeUser } from "../lib/api-fetch";
|
2022-12-22 15:43:10 +01:00
|
|
|
import { themeState, userState } from "../lib/state";
|
2022-12-22 15:49:08 +01:00
|
|
|
import Logo from "./logo";
|
|
|
|
import NavItem from "./NavItem";
|
2022-05-10 22:24:20 +02:00
|
|
|
|
2022-08-16 00:01:54 +02:00
|
|
|
export default function Navigation() {
|
2022-05-12 16:41:32 +02:00
|
|
|
const [user, setUser] = useRecoilState(userState);
|
2022-12-22 15:43:10 +01:00
|
|
|
const [darkTheme, setDarkTheme] = useRecoilState(themeState);
|
2022-05-12 16:41:32 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) return;
|
|
|
|
|
|
|
|
fetchAPI<MeUser>("/users/@me").then(
|
|
|
|
(res) => setUser(res),
|
2022-06-17 00:00:52 +02:00
|
|
|
(err) => {
|
|
|
|
console.log("fetching /users/@me", err);
|
|
|
|
if (
|
|
|
|
(err as APIError).code == ErrorCode.InvalidToken ||
|
|
|
|
(err as APIError).code == ErrorCode.Forbidden
|
|
|
|
) {
|
|
|
|
localStorage.removeItem("pronouns-token");
|
|
|
|
}
|
|
|
|
}
|
2022-05-12 16:41:32 +02:00
|
|
|
);
|
2022-08-16 00:01:54 +02:00
|
|
|
}, [user, setUser]);
|
2022-05-10 16:33:29 +02:00
|
|
|
|
|
|
|
const [showMenu, setShowMenu] = useState(false);
|
|
|
|
|
2022-08-16 00:01:54 +02:00
|
|
|
useEffect(() => {
|
|
|
|
setDarkTheme(
|
|
|
|
localStorage.theme === "dark" ||
|
|
|
|
(!("theme" in localStorage) &&
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (darkTheme) {
|
|
|
|
document.documentElement.classList.add("dark");
|
|
|
|
} else {
|
|
|
|
document.documentElement.classList.remove("dark");
|
|
|
|
}
|
|
|
|
}, [darkTheme]);
|
2022-05-10 16:33:29 +02:00
|
|
|
|
2022-05-10 22:24:20 +02:00
|
|
|
const storeTheme = (useDarkTheme: boolean | null) => {
|
|
|
|
if (useDarkTheme === null) {
|
2022-05-10 16:33:29 +02:00
|
|
|
localStorage.removeItem("theme");
|
|
|
|
} else {
|
2022-05-10 22:24:20 +02:00
|
|
|
localStorage.setItem("theme", useDarkTheme ? "dark" : "light");
|
2022-05-10 16:33:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-12 16:41:32 +02:00
|
|
|
const nav = user ? (
|
|
|
|
<>
|
2022-11-21 02:21:54 +01:00
|
|
|
<NavItem href={`/u/${user.name}`}>@{user.name}</NavItem>
|
2022-08-16 00:01:54 +02:00
|
|
|
<NavItem href="/settings">Settings</NavItem>
|
|
|
|
<NavItem href="/logout">Log out</NavItem>
|
2022-05-12 16:41:32 +02:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2022-08-16 00:01:54 +02:00
|
|
|
<NavItem href="/login">Log in</NavItem>
|
2022-05-12 16:41:32 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2022-05-10 16:33:29 +02:00
|
|
|
return (
|
2022-05-10 22:24:20 +02:00
|
|
|
<>
|
|
|
|
<div className="bg-white/75 dark:bg-slate-800/75 w-full backdrop-blur border-slate-200 dark:border-slate-700 border-b">
|
|
|
|
<div className="max-w-8xl mx-auto">
|
|
|
|
<div className="py-4 mx-4">
|
|
|
|
<div className="flex items-center">
|
2022-08-17 03:04:06 +02:00
|
|
|
<Link href="/" passHref>
|
2022-11-19 16:52:52 +01:00
|
|
|
<Logo />
|
2022-05-10 22:24:20 +02:00
|
|
|
</Link>
|
|
|
|
<div className="ml-auto flex items-center">
|
|
|
|
<nav className="hidden lg:flex">
|
2022-05-12 16:41:32 +02:00
|
|
|
<ul className="flex space-x-4 font-bold">{nav}</ul>
|
2022-05-10 22:24:20 +02:00
|
|
|
</nav>
|
|
|
|
<div className="flex border-l border-slate-200 ml-4 pl-4 lg:ml-6 lg:pl-6 lg:mr-2 dark:border-slate-700 space-x-2 lg:space-x-4">
|
|
|
|
<div
|
|
|
|
onClick={() => {
|
|
|
|
setDarkTheme(!darkTheme);
|
|
|
|
storeTheme(!darkTheme);
|
|
|
|
}}
|
|
|
|
title={
|
|
|
|
darkTheme ? "Switch to light mode" : "Switch to dark mode"
|
|
|
|
}
|
|
|
|
className="cursor-pointer"
|
|
|
|
>
|
|
|
|
{darkTheme ? (
|
|
|
|
<Sun className="hover:text-sky-400" size={24} />
|
|
|
|
) : (
|
|
|
|
<MoonStars size={24} className="hover:text-sky-500" />
|
|
|
|
)}
|
|
|
|
</div>
|
2022-05-12 16:41:32 +02:00
|
|
|
<div
|
|
|
|
onClick={() => setShowMenu(!showMenu)}
|
|
|
|
title="Show menu"
|
|
|
|
className="cursor-pointer flex lg:hidden"
|
|
|
|
>
|
|
|
|
<List
|
|
|
|
className="dark:hover:text-sky-400 hover:text-sky-500"
|
|
|
|
size={24}
|
|
|
|
/>
|
2022-05-10 22:24:20 +02:00
|
|
|
</div>
|
2022-05-10 16:33:29 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-05-12 16:41:32 +02:00
|
|
|
<nav
|
|
|
|
className={`lg:hidden p-4 border-slate-200 dark:border-slate-700 border-b ${
|
|
|
|
showMenu ? "flex" : "hidden"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<ul className="flex flex-col space-y-4 font-bold">{nav}</ul>
|
2022-05-10 22:24:20 +02:00
|
|
|
</nav>
|
|
|
|
</>
|
2022-05-10 16:33:29 +02:00
|
|
|
);
|
|
|
|
}
|