el.xwx.moe/components/Sidebar.tsx

209 lines
7.1 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-05 15:03:20 -06:00
faBox,
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-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-06-02 06:59:52 -05:00
className={`bg-gray-100 h-screen w-64 xl:w-80 overflow-y-auto border-solid border-r-sky-100 px-2 border z-20 ${className}`}
2023-05-15 13:29:51 -05:00
>
2023-06-05 04:54:43 -05:00
<p className="p-2 text-sky-500 font-bold text-2xl my-2 leading-4">
2023-03-28 02:31:50 -05:00
Linkwarden
</p>
2023-02-18 21:32:02 -06:00
2023-06-02 06:59:52 -05:00
<div className="flex flex-col gap-1">
<Link href="/dashboard">
<div
className={`${
2023-06-02 06:59:52 -05:00
active === "/dashboard"
2023-06-05 04:54:43 -05:00
? "bg-sky-200"
2023-06-02 06:59:52 -05:00
: "hover:bg-slate-200 bg-gray-100"
} outline-sky-100 outline-1 duration-100 py-1 px-2 rounded-md cursor-pointer flex items-center gap-2`}
>
2023-06-02 06:59:52 -05:00
<FontAwesomeIcon
icon={faChartSimple}
2023-06-05 04:54:43 -05:00
className={`w-6 h-6 drop-shadow text-sky-500`}
2023-06-02 06:59:52 -05:00
/>
2023-06-05 04:54:43 -05:00
<p className="text-sky-600">Dashboard</p>
2023-06-02 06:59:52 -05:00
</div>
</Link>
2023-06-12 13:23:11 -05:00
<Link href="/links">
2023-06-02 06:59:52 -05:00
<div
2023-05-31 21:45:19 -05:00
className={`${
2023-06-12 13:23:11 -05:00
active === "/links"
? "bg-sky-200"
: "hover:bg-slate-200 bg-gray-100"
} outline-sky-100 outline-1 duration-100 py-1 px-2 rounded-md cursor-pointer flex items-center gap-2`}
2023-05-01 05:07:01 -05:00
>
2023-06-02 06:59:52 -05:00
<FontAwesomeIcon
2023-06-12 13:23:11 -05:00
icon={faLink}
2023-06-05 04:54:43 -05:00
className={`w-6 h-6 drop-shadow text-sky-500`}
2023-06-02 06:59:52 -05:00
/>
2023-06-12 13:23:11 -05:00
<p className="text-sky-600">All Links</p>
2023-06-02 06:59:52 -05:00
</div>
</Link>
2023-02-24 22:22:33 -06:00
2023-06-12 13:23:11 -05:00
<Link href="/collections">
2023-06-02 06:59:52 -05:00
<div
className={`${
2023-06-12 13:23:11 -05:00
active === "/collections" ? "bg-sky-200" : "hover:bg-slate-200"
} outline-sky-100 outline-1 duration-100 py-1 px-2 rounded-md cursor-pointer flex items-center gap-2`}
2023-05-01 05:07:01 -05:00
>
2023-06-02 06:59:52 -05:00
<FontAwesomeIcon
2023-06-12 13:23:11 -05:00
icon={faBox}
2023-06-05 04:54:43 -05:00
className={`w-6 h-6 drop-shadow text-sky-500`}
2023-06-02 06:59:52 -05:00
/>
2023-06-12 13:23:11 -05:00
<p className="text-sky-600">All Collections</p>
2023-06-02 06:59:52 -05:00
</div>
</Link>
</div>
2023-06-10 17:55:18 -05:00
<Disclosure defaultOpen={collectionDisclosure}>
<Disclosure.Button
onClick={() => {
setCollectionDisclosure(!collectionDisclosure);
}}
className="flex items-center justify-between text-sm w-full text-left mb-2 pl-2 font-bold text-gray-500 mt-5"
>
<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">
{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}`
? "bg-sky-200"
: "hover:bg-slate-200 bg-gray-100"
} 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 }}
/>
<p className="text-sky-600 truncate w-4/6">{e.name}</p>
</div>
</Link>
);
})}
</Disclosure.Panel>
</Transition>
</Disclosure>
<Disclosure defaultOpen={tagDisclosure}>
<Disclosure.Button
onClick={() => {
setTagDisclosure(!tagDisclosure);
}}
className="flex items-center justify-between text-sm w-full text-left mb-2 pl-2 font-bold text-gray-500 mt-5"
>
<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">
{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}`
? "bg-sky-200"
: "hover:bg-slate-200 bg-gray-100"
} duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
>
<FontAwesomeIcon
icon={faHashtag}
className="w-4 h-4 text-sky-500 mt-1"
/>
<p className="text-sky-600 truncate w-4/6">{e.name}</p>
</div>
</Link>
);
})}
</Disclosure.Panel>
</Transition>
</Disclosure>
2023-02-08 17:58:55 -06:00
</div>
);
}