import { useEffect, useState } from "react"; import { List, MoonStars, Sun } from "react-bootstrap-icons"; import Link from "next/link"; import { useRecoilState } from "recoil"; import { APIError, ErrorCode, fetchAPI, MeUser } from "../lib/api-fetch"; import { themeState, userState } from "../lib/state"; import Logo from "./logo"; import NavItem from "./NavItem"; export default function Navigation() { const [user, setUser] = useRecoilState(userState); const [darkTheme, setDarkTheme] = useRecoilState(themeState); useEffect(() => { if (user) return; fetchAPI("/users/@me").then( (res) => setUser(res), (err) => { console.log("fetching /users/@me", err); if ( (err as APIError).code == ErrorCode.InvalidToken || (err as APIError).code == ErrorCode.Forbidden ) { localStorage.removeItem("pronouns-token"); } } ); }, [user, setUser]); const [showMenu, setShowMenu] = useState(false); 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]); const storeTheme = (useDarkTheme: boolean | null) => { if (useDarkTheme === null) { localStorage.removeItem("theme"); } else { localStorage.setItem("theme", useDarkTheme ? "dark" : "light"); } }; const nav = user ? ( <> @{user.name} Settings Log out ) : ( <> Log in ); return ( <>
{ setDarkTheme(!darkTheme); storeTheme(!darkTheme); }} title={ darkTheme ? "Switch to light mode" : "Switch to dark mode" } className="cursor-pointer" > {darkTheme ? ( ) : ( )}
setShowMenu(!showMenu)} title="Show menu" className="cursor-pointer flex lg:hidden" >
); }