el.xwx.moe/components/Sidebar.tsx

227 lines
8.5 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-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-16 08:16:25 -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();
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",
2023-12-16 08:16:25 -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"
2023-11-26 04:17:08 -06:00
className={`bg-base-200 h-full w-64 xl:w-80 overflow-y-auto border-solid border border-base-200 border-r-neutral-content px-2 z-20 ${
2023-10-28 11:50:11 -05:00
className || ""
}`}
2023-05-15 13:29:51 -05:00
>
2023-12-16 08:16:25 -06:00
<div className="flex flex-col gap-1 mt-2">
<Link href={`/dashboard`}>
<div
className={`${
active === `/dashboard` ? "bg-primary/20" : "hover:bg-neutral/20"
} duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
2023-12-16 08:16:25 -06:00
<i className="bi-house text-primary text-2xl drop-shadow"></i>
2023-11-24 07:39:55 -06:00
<p className="truncate w-full">Dashboard</p>
</div>
2023-06-02 06:59:52 -05:00
</Link>
<Link href={`/links`}>
<div
className={`${
active === `/links` ? "bg-primary/20" : "hover:bg-neutral/20"
} duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
2023-12-16 08:16:25 -06:00
<i className="bi-link-45deg text-primary text-2xl drop-shadow"></i>
2023-11-24 07:39:55 -06:00
<p className="truncate w-full">All Links</p>
</div>
2023-06-02 06:59:52 -05:00
</Link>
2023-02-24 22:22:33 -06:00
<Link href={`/collections`}>
<div
className={`${
active === `/collections`
? "bg-primary/20"
2023-11-27 15:38:38 -06:00
: "hover:bg-neutral/20"
} duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
2023-12-16 08:16:25 -06:00
<i className="bi-folder2 text-primary text-2xl drop-shadow"></i>
2023-11-24 07:39:55 -06:00
<p className="truncate w-full">All Collections</p>
</div>
</Link>
2023-08-10 23:44:44 -05:00
<Link href={`/links/pinned`}>
<div
className={`${
active === `/links/pinned`
? "bg-primary/20"
2023-11-27 15:38:38 -06:00
: "hover:bg-neutral/20"
} duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
2023-12-16 08:16:25 -06:00
<i className="bi-pin-angle text-primary text-2xl drop-shadow"></i>
2023-11-24 07:39:55 -06:00
<p className="truncate w-full">Pinned Links</p>
</div>
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-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 ">Collections</p>
<i
className={`bi-chevron-down text-primary drop-shadow ${
2023-06-10 17:55:18 -05:00
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 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}`
? "bg-primary/20"
2023-11-27 15:38:38 -06:00
: "hover:bg-neutral/20"
} duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
2023-07-19 16:49:54 -05:00
>
2023-12-16 08:16:25 -06:00
<i
className="bi-folder2 text-2xl drop-shadow"
2023-07-19 16:49:54 -05:00
style={{ color: e.color }}
2023-12-16 08:16:25 -06:00
></i>
2023-11-24 07:39:55 -06:00
<p className="truncate w-full">{e.name}</p>
2023-10-24 16:03:33 -05:00
{e.isPublic ? (
2023-12-16 08:16:25 -06:00
<i
className="bi-globe-americas text-sm text-black/50 dark:text-white/50 drop-shadow"
2023-10-24 16:03:33 -05:00
title="This collection is being shared publicly."
2023-12-16 08:16:25 -06:00
></i>
2023-10-24 16:03:33 -05:00
) : undefined}
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 Collections...
</p>
</div>
)}
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
className={`bi-chevron-down text-primary drop-shadow ${
collectionDisclosure ? "rotate-reverse" : "rotate"
}`}
></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
className={`${
active === `/tags/${e.id}`
? "bg-primary/20"
2023-11-27 15:38:38 -06:00
: "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>
);
}