el.xwx.moe/components/Sidebar.tsx

244 lines
8.9 KiB
TypeScript
Raw Normal View History

2023-03-22 18:11:54 -05:00
import useCollectionStore from "@/store/collections";
2023-02-18 21:32:02 -06:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faFolder,
2023-03-10 13:25:33 -06:00
faHashtag,
faChartSimple,
2023-06-10 17:55:18 -05:00
faChevronDown,
2023-06-11 17:28:37 -05:00
faLink,
2023-10-24 16:03:33 -05:00
faGlobe,
2023-02-18 21:32:02 -06:00
} from "@fortawesome/free-solid-svg-icons";
2023-03-22 18:11:54 -05:00
import useTagStore from "@/store/tags";
2023-03-05 15:03:20 -06:00
import Link from "next/link";
2023-05-01 05:07:01 -05:00
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
2023-06-10 17:55:18 -05:00
import { Disclosure, Transition } from "@headlessui/react";
2023-02-08 17:58:55 -06:00
export default function Sidebar({ className }: { className?: string }) {
2023-06-10 17:55:18 -05:00
const [tagDisclosure, setTagDisclosure] = useState<boolean>(() => {
const storedValue = localStorage.getItem("tagDisclosure");
return storedValue ? storedValue === "true" : true;
});
const [collectionDisclosure, setCollectionDisclosure] = useState<boolean>(
() => {
const storedValue = localStorage.getItem("collectionDisclosure");
return storedValue ? storedValue === "true" : true;
}
);
2023-04-24 16:30:40 -05:00
const { collections } = useCollectionStore();
2023-03-22 18:11:54 -05:00
const { tags } = useTagStore();
2023-05-01 05:07:01 -05:00
const router = useRouter();
const [active, setActive] = useState("");
2023-06-10 17:55:18 -05:00
useEffect(() => {
localStorage.setItem("tagDisclosure", tagDisclosure ? "true" : "false");
}, [tagDisclosure]);
useEffect(() => {
localStorage.setItem(
"collectionDisclosure",
collectionDisclosure ? "true" : "false"
);
}, [collectionDisclosure]);
2023-05-01 05:07:01 -05:00
useEffect(() => {
setActive(router.asPath);
2023-06-05 04:54:43 -05:00
}, [router, collections]);
2023-05-01 05:07:01 -05:00
2023-02-08 17:58:55 -06:00
return (
2023-05-15 13:29:51 -05:00
<div
2023-08-14 22:25:25 -05:00
className={`bg-gray-100 dark:bg-neutral-800 h-full w-64 xl:w-80 overflow-y-auto border-solid border dark:border-neutral-800 border-r-sky-100 dark:border-r-neutral-700 px-2 z-20 ${className}`}
2023-05-15 13:29:51 -05:00
>
2023-07-13 17:19:49 -05:00
<div className="flex justify-center gap-2 mt-2">
<Link
href="/dashboard"
className={`${
active === "/dashboard"
2023-08-30 23:17:27 -05:00
? "bg-sky-200 dark:bg-sky-800"
2023-08-14 22:25:25 -05:00
: "hover:bg-slate-200 hover:dark:bg-neutral-700"
} outline-sky-100 outline-1 duration-100 py-1 px-2 rounded-md cursor-pointer flex justify-center flex-col items-center gap-1 w-full`}
2023-07-13 17:19:49 -05:00
>
<FontAwesomeIcon
icon={faChartSimple}
2023-08-30 23:17:27 -05:00
className={`w-8 h-8 drop-shadow text-sky-500 dark:text-sky-500`}
2023-07-13 17:19:49 -05:00
/>
2023-08-10 23:44:44 -05:00
<p className="text-black dark:text-white text-xs xl:text-sm font-semibold">
Dashboard
</p>
2023-06-02 06:59:52 -05:00
</Link>
2023-07-13 17:19:49 -05:00
<Link
href="/links"
className={`${
active === "/links"
2023-08-30 23:17:27 -05:00
? "bg-sky-200 dark:bg-sky-800"
2023-08-14 22:25:25 -05:00
: "hover:bg-slate-200 hover:dark:bg-neutral-700"
2023-07-13 17:19:49 -05:00
} outline-sky-100 outline-1 duration-100 py-1 px-2 rounded-md cursor-pointer flex justify-center flex-col items-center gap-1 w-full`}
>
<FontAwesomeIcon
icon={faLink}
2023-08-30 23:17:27 -05:00
className={`w-8 h-8 drop-shadow text-sky-500 dark:text-sky-500`}
2023-07-13 17:19:49 -05:00
/>
2023-08-11 00:11:02 -05:00
<p className="text-black dark:text-white text-xs xl:text-sm font-semibold">
2023-08-10 23:44:44 -05:00
Links
</p>
2023-06-02 06:59:52 -05:00
</Link>
2023-02-24 22:22:33 -06:00
2023-07-13 17:19:49 -05:00
<Link
href="/collections"
className={`${
2023-08-10 23:44:44 -05:00
active === "/collections"
2023-08-30 23:17:27 -05:00
? "bg-sky-200 dark:bg-sky-800"
2023-08-14 22:25:25 -05:00
: "hover:bg-slate-200 hover:dark:bg-neutral-700"
2023-07-13 17:19:49 -05:00
} outline-sky-100 outline-1 duration-100 py-1 px-2 rounded-md cursor-pointer flex justify-center flex-col items-center gap-1 w-full`}
>
<FontAwesomeIcon
icon={faFolder}
2023-08-30 23:17:27 -05:00
className={`w-8 h-8 drop-shadow text-sky-500 dark:text-sky-500`}
2023-07-13 17:19:49 -05:00
/>
2023-08-10 23:44:44 -05:00
2023-08-11 00:11:02 -05:00
<p className="text-black dark:text-white text-xs xl:text-sm font-semibold">
Collections
2023-07-13 17:19:49 -05:00
</p>
2023-06-02 06:59:52 -05:00
</Link>
</div>
2023-06-10 17:55:18 -05:00
<Disclosure defaultOpen={collectionDisclosure}>
<Disclosure.Button
onClick={() => {
setCollectionDisclosure(!collectionDisclosure);
}}
2023-08-22 17:34:46 -05:00
className="flex items-center justify-between text-sm w-full text-left mb-2 pl-2 font-bold text-gray-500 dark:text-gray-300 mt-5"
2023-06-10 17:55:18 -05:00
>
<p>Collections</p>
<FontAwesomeIcon
icon={faChevronDown}
className={`w-3 h-3 ${
collectionDisclosure ? "rotate-reverse" : "rotate"
}`}
/>
</Disclosure.Button>
<Transition
enter="transition duration-100 ease-out"
enterFrom="transform opacity-0 -translate-y-3"
enterTo="transform opacity-100 translate-y-0"
leave="transition duration-100 ease-out"
leaveFrom="transform opacity-100 translate-y-0"
leaveTo="transform opacity-0 -translate-y-3"
>
<Disclosure.Panel className="flex flex-col gap-1">
2023-07-19 16:49:54 -05:00
{collections[0] ? (
collections
.sort((a, b) => a.name.localeCompare(b.name))
.map((e, i) => {
return (
<Link key={i} href={`/collections/${e.id}`}>
<div
className={`${
active === `/collections/${e.id}`
2023-08-30 23:17:27 -05:00
? "bg-sky-200 dark:bg-sky-800"
2023-08-14 22:25:25 -05:00
: "hover:bg-slate-200 hover:dark:bg-neutral-700"
2023-07-19 16:49:54 -05:00
} duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
<FontAwesomeIcon
icon={faFolder}
className="w-6 h-6 drop-shadow"
style={{ color: e.color }}
/>
2023-10-24 16:03:33 -05:00
<p className="text-black dark:text-white truncate w-full">
2023-07-19 16:49:54 -05:00
{e.name}
</p>
2023-10-24 16:03:33 -05:00
{e.isPublic ? (
<FontAwesomeIcon
icon={faGlobe}
title="This collection is being shared publicly."
className="w-4 h-4 drop-shadow text-gray-500 dark:text-gray-300"
/>
) : undefined}
2023-07-19 16:49:54 -05:00
</div>
</Link>
);
})
) : (
<div
className={`duration-100 py-1 px-2 flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
2023-08-22 17:34:46 -05:00
<p className="text-gray-500 dark:text-gray-300 text-xs font-semibold truncate w-full pr-7">
2023-07-19 16:49:54 -05:00
You Have No Collections...
</p>
</div>
)}
2023-06-10 17:55:18 -05:00
</Disclosure.Panel>
</Transition>
</Disclosure>
<Disclosure defaultOpen={tagDisclosure}>
<Disclosure.Button
onClick={() => {
setTagDisclosure(!tagDisclosure);
}}
2023-08-22 17:34:46 -05:00
className="flex items-center justify-between text-sm w-full text-left mb-2 pl-2 font-bold text-gray-500 dark:text-gray-300 mt-5"
2023-06-10 17:55:18 -05:00
>
<p>Tags</p>
<FontAwesomeIcon
icon={faChevronDown}
className={`w-3 h-3 ${tagDisclosure ? "rotate-reverse" : "rotate"}`}
/>
</Disclosure.Button>
<Transition
enter="transition duration-100 ease-out"
enterFrom="transform opacity-0 -translate-y-3"
enterTo="transform opacity-100 translate-y-0"
leave="transition duration-100 ease-out"
leaveFrom="transform opacity-100 translate-y-0"
leaveTo="transform opacity-0 -translate-y-3"
>
<Disclosure.Panel className="flex flex-col gap-1">
2023-07-19 16:49:54 -05:00
{tags[0] ? (
tags
.sort((a, b) => a.name.localeCompare(b.name))
.map((e, i) => {
return (
<Link key={i} href={`/tags/${e.id}`}>
<div
className={`${
active === `/tags/${e.id}`
2023-08-30 23:17:27 -05:00
? "bg-sky-200 dark:bg-sky-800"
2023-08-14 22:25:25 -05:00
: "hover:bg-slate-200 hover:dark:bg-neutral-700"
2023-07-19 16:49:54 -05:00
} duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
>
<FontAwesomeIcon
icon={faHashtag}
2023-08-30 23:17:27 -05:00
className="w-4 h-4 text-sky-500 dark:text-sky-500 mt-1"
2023-07-19 16:49:54 -05:00
/>
2023-06-10 17:55:18 -05:00
2023-08-11 00:11:02 -05:00
<p className="text-black dark:text-white truncate w-full pr-7">
2023-07-19 16:49:54 -05:00
{e.name}
</p>
</div>
</Link>
);
})
) : (
<div
className={`duration-100 py-1 px-2 flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
2023-08-22 17:34:46 -05:00
<p className="text-gray-500 dark:text-gray-300 text-xs font-semibold truncate w-full pr-7">
2023-07-19 16:49:54 -05:00
You Have No Tags...
</p>
</div>
)}
2023-06-10 17:55:18 -05:00
</Disclosure.Panel>
</Transition>
</Disclosure>
2023-02-08 17:58:55 -06:00
</div>
);
}