el.xwx.moe/components/Sidebar.tsx

165 lines
5.6 KiB
TypeScript
Raw Normal View History

2023-03-22 18:11:54 -05:00
import useCollectionStore from "@/store/collections";
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-12-17 17:54:33 -06:00
import SidebarHighlightLink from "@/components/SidebarHighlightLink";
2024-02-19 13:37:07 -06:00
import CollectionListing from "@/components/CollectionListing";
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-12-17 22:32:33 -06:00
}
2023-06-10 17:55:18 -05:00
);
2023-04-24 16:30:40 -05:00
const { collections } = useCollectionStore();
2023-03-22 18:11:54 -05:00
const { tags } = useTagStore();
2024-02-22 01:51:51 -06:00
const [active, setActive] = useState("");
2023-05-01 05:07:01 -05:00
const router = useRouter();
2023-06-10 17:55:18 -05:00
useEffect(() => {
localStorage.setItem("tagDisclosure", tagDisclosure ? "true" : "false");
}, [tagDisclosure]);
useEffect(() => {
localStorage.setItem(
"collectionDisclosure",
2023-12-17 22:32:33 -06:00
collectionDisclosure ? "true" : "false"
2023-06-10 17:55:18 -05:00
);
}, [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-12-16 08:16:25 -06:00
id="sidebar"
2024-02-22 02:51:24 -06:00
className={`bg-base-200 h-full w-80 overflow-y-auto border-solid border border-base-200 border-r-neutral-content p-2 z-20 ${
className || ""
}`}
2023-05-15 13:29:51 -05:00
>
2023-12-17 22:55:38 -06:00
<div className="grid grid-cols-2 gap-2">
<SidebarHighlightLink
title={"Dashboard"}
href={`/dashboard`}
icon={"bi-house"}
active={active === `/dashboard`}
2023-12-17 17:54:33 -06:00
/>
2023-12-17 22:55:38 -06:00
<SidebarHighlightLink
title={"Pinned"}
href={`/links/pinned`}
icon={"bi-pin-angle"}
active={active === `/links/pinned`}
2023-12-17 17:54:33 -06:00
/>
2023-12-17 22:55:38 -06:00
<SidebarHighlightLink
title={"All Links"}
href={`/links`}
icon={"bi-link-45deg"}
active={active === `/links`}
2023-12-17 17:54:33 -06:00
/>
2023-12-17 22:55:38 -06:00
<SidebarHighlightLink
title={"All Collections"}
href={`/collections`}
icon={"bi-folder"}
2023-12-17 22:55:38 -06:00
active={active === `/collections`}
2023-12-17 17:54:33 -06:00
/>
2023-06-02 06:59:52 -05:00
</div>
2023-06-10 17:55:18 -05:00
<Disclosure defaultOpen={collectionDisclosure}>
<Disclosure.Button
onClick={() => {
setCollectionDisclosure(!collectionDisclosure);
}}
2023-12-16 08:16:25 -06:00
className="flex items-center justify-between w-full text-left mb-2 pl-2 font-bold text-neutral mt-5"
2023-06-10 17:55:18 -05:00
>
2023-12-17 22:32:33 -06:00
<p className="text-sm">Collections</p>
2023-12-16 08:16:25 -06:00
<i
2024-02-22 02:51:24 -06:00
className={`bi-chevron-down ${
collectionDisclosure ? "rotate-reverse" : "rotate"
}`}
2023-12-16 08:16:25 -06:00
></i>
2023-06-10 17:55:18 -05:00
</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>
2024-02-19 13:37:07 -06:00
<CollectionListing links={true} />
2023-06-10 17:55:18 -05:00
</Disclosure.Panel>
</Transition>
</Disclosure>
<Disclosure defaultOpen={tagDisclosure}>
<Disclosure.Button
onClick={() => {
setTagDisclosure(!tagDisclosure);
}}
2023-12-16 08:16:25 -06:00
className="flex items-center justify-between w-full text-left mb-2 pl-2 font-bold text-neutral mt-5"
2023-06-10 17:55:18 -05:00
>
2023-12-16 08:16:25 -06:00
<p className="text-sm">Tags</p>
<i
2024-02-22 02:51:24 -06:00
className={`bi-chevron-down ${
tagDisclosure ? "rotate-reverse" : "rotate"
}`}
2023-12-16 08:16:25 -06:00
></i>
2023-06-10 17:55:18 -05:00
</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
2024-02-22 02:51:24 -06:00
className={`${
active === `/tags/${e.id}`
? "bg-primary/20"
: "hover:bg-neutral/20"
} duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
2023-07-19 16:49:54 -05:00
>
2023-12-16 08:16:25 -06:00
<i className="bi-hash text-2xl text-primary drop-shadow"></i>
2023-11-24 07:39:55 -06:00
<p className="truncate w-full pr-7">{e.name}</p>
2023-11-25 04:39:56 -06:00
<div className="drop-shadow text-neutral text-xs">
2023-11-15 12:12:06 -06:00
{e._count?.links}
</div>
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-11-25 04:39:56 -06:00
<p className="text-neutral 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>
);
}