el.xwx.moe/components/Navbar.tsx

107 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-02-08 18:32:20 -06:00
import { signOut } from "next-auth/react";
import { useRouter } from "next/router";
2023-03-05 15:03:20 -06:00
import useCollectionSlice from "@/store/collections";
import { Collection, Tag } from "@prisma/client";
import ClickAwayHandler from "./ClickAwayHandler";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2023-03-05 15:03:20 -06:00
import {
faPlus,
faFolder,
faBox,
2023-03-10 13:25:33 -06:00
faHashtag,
2023-03-05 15:03:20 -06:00
faBookmark,
faMagnifyingGlass,
IconDefinition,
} from "@fortawesome/free-solid-svg-icons";
import { useEffect, useState } from "react";
import AddLinkModal from "./AddLinkModal";
2023-03-05 15:03:20 -06:00
import useTagSlice from "@/store/tags";
export default function () {
const router = useRouter();
2023-03-05 15:03:20 -06:00
const [pageName, setPageName] = useState<string | null>("");
const [pageIcon, setPageIcon] = useState<IconDefinition | null>(null);
const { collections } = useCollectionSlice();
2023-03-05 15:03:20 -06:00
const { tags } = useTagSlice();
2023-03-05 15:03:20 -06:00
useEffect(() => {
if (router.route === "/collections/[id]") {
const collectionId = router.query.id;
const activeCollection: Collection | undefined = collections.find(
(e) => e.id.toString() == collectionId
);
if (activeCollection) {
setPageName(activeCollection?.name);
}
setPageIcon(faFolder);
} else if (router.route === "/tags/[id]") {
const tagId = router.query.id;
const activeTag: Tag | undefined = tags.find(
(e) => e.id.toString() == tagId
);
if (activeTag) {
setPageName(activeTag?.name);
}
2023-03-10 13:25:33 -06:00
setPageIcon(faHashtag);
2023-03-05 15:03:20 -06:00
} else if (router.route === "/collections") {
setPageName("All Collections");
setPageIcon(faBox);
} else if (router.route === "/links") {
setPageName("All Links");
setPageIcon(faBookmark);
}
}, [router, collections, tags]);
const [linkModal, setLinkModal] = useState(false);
const toggleLinkModal = () => {
setLinkModal(!linkModal);
};
2023-02-08 18:32:20 -06:00
return (
<div className="flex justify-between items-center p-5 border-solid border-b-sky-100 border border-l-white">
2023-03-05 15:03:20 -06:00
<div className="text-sky-900 rounded my-1 flex items-center gap-2 font-bold">
{pageIcon ? (
<FontAwesomeIcon icon={pageIcon} className="w-4 text-sky-300" />
) : null}
<p>{pageName}</p>
</div>
<div className="flex items-center gap-3">
<FontAwesomeIcon
icon={faPlus}
onClick={toggleLinkModal}
className="select-none cursor-pointer w-5 h-5 text-white bg-sky-500 p-2 rounded hover:bg-sky-400 duration-100"
/>
<FontAwesomeIcon
icon={faMagnifyingGlass}
className="select-none cursor-pointer w-5 h-5 text-white bg-sky-500 p-2 rounded hover:bg-sky-400 duration-100"
/>
<div
onClick={() => signOut()}
className="cursor-pointer w-max text-sky-900"
>
Sign Out
</div>
{linkModal ? (
2023-03-10 23:10:10 -06:00
<div className="fixed top-0 bottom-0 right-0 left-0 bg-gray-500 bg-opacity-10 flex items-center fade-in z-10">
<ClickAwayHandler
onClickOutside={toggleLinkModal}
className="w-fit mx-auto"
>
<AddLinkModal toggleLinkModal={toggleLinkModal} />
</ClickAwayHandler>
</div>
) : null}
2023-02-08 18:32:20 -06:00
</div>
</div>
);
2023-02-08 17:58:55 -06:00
}