el.xwx.moe/pages/collections/[id].tsx

260 lines
9.5 KiB
TypeScript
Raw Normal View History

2023-06-05 15:47:01 -05:00
import LinkCard from "@/components/LinkCard";
2023-04-07 20:10:55 -05:00
import useCollectionStore from "@/store/collections";
2023-03-22 18:11:54 -05:00
import useLinkStore from "@/store/links";
2023-06-16 07:55:21 -05:00
import { CollectionIncludingMembersAndLinkCount, Sort } from "@/types/global";
2023-11-29 14:17:51 -06:00
import { faEllipsis, faFolder } from "@fortawesome/free-solid-svg-icons";
2023-04-07 20:10:55 -05:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useRouter } from "next/router";
2023-06-13 21:44:50 -05:00
import { useEffect, useState } from "react";
import MainLayout from "@/layouts/MainLayout";
2023-05-27 14:05:07 -05:00
import ProfilePhoto from "@/components/ProfilePhoto";
2023-06-13 23:40:23 -05:00
import SortDropdown from "@/components/SortDropdown";
import useLinks from "@/hooks/useLinks";
import usePermissions from "@/hooks/usePermissions";
2023-07-19 16:49:54 -05:00
import NoLinksFound from "@/components/NoLinksFound";
2023-11-24 06:50:16 -06:00
import useLocalSettingsStore from "@/store/localSettings";
2023-11-30 05:13:42 -06:00
import useAccountStore from "@/store/account";
import getPublicUserData from "@/lib/client/getPublicUserData";
2023-12-01 16:44:34 -06:00
import EditCollectionModal from "@/components/ModalContent/EditCollectionModal";
import EditCollectionSharingModal from "@/components/ModalContent/EditCollectionSharingModal";
import DeleteCollectionModal from "@/components/ModalContent/DeleteCollectionModal";
export default function Index() {
2023-11-24 06:50:16 -06:00
const { settings } = useLocalSettingsStore();
const router = useRouter();
2023-04-07 20:10:55 -05:00
2023-03-22 18:11:54 -05:00
const { links } = useLinkStore();
2023-04-07 20:10:55 -05:00
const { collections } = useCollectionStore();
const [sortBy, setSortBy] = useState<Sort>(Sort.DateNewestFirst);
2023-04-27 18:13:21 -05:00
const [activeCollection, setActiveCollection] =
2023-06-16 07:55:21 -05:00
useState<CollectionIncludingMembersAndLinkCount>();
const permissions = usePermissions(activeCollection?.id as number);
useLinks({ collectionId: Number(router.query.id), sort: sortBy });
2023-05-31 21:43:42 -05:00
useEffect(() => {
2023-04-07 20:10:55 -05:00
setActiveCollection(
collections.find((e) => e.id === Number(router.query.id))
);
2023-06-13 23:40:23 -05:00
}, [router, collections]);
2023-11-30 05:13:42 -06:00
const { account } = useAccountStore();
const [collectionOwner, setCollectionOwner] = useState({
id: null as unknown as number,
name: "",
username: "",
image: "",
});
useEffect(() => {
const fetchOwner = async () => {
if (activeCollection && activeCollection.ownerId !== account.id) {
const owner = await getPublicUserData(
activeCollection.ownerId as number
);
setCollectionOwner(owner);
} else if (activeCollection && activeCollection.ownerId === account.id) {
setCollectionOwner({
id: account.id as number,
name: account.name,
username: account.username as string,
image: account.image as string,
});
}
};
fetchOwner();
}, [activeCollection]);
const [editCollectionModal, setEditCollectionModal] = useState(false);
const [editCollectionSharingModal, setEditCollectionSharingModal] =
useState(false);
const [deleteCollectionModal, setDeleteCollectionModal] = useState(false);
2023-03-05 15:03:20 -06:00
return (
<MainLayout>
2023-11-30 05:55:37 -06:00
<div
style={{
backgroundImage: `linear-gradient(${activeCollection?.color}20 10%, ${
settings.theme === "dark" ? "#262626" : "#f3f4f6"
} 14rem, ${settings.theme === "dark" ? "#171717" : "#ffffff"} 100%)`,
2023-11-30 05:55:37 -06:00
}}
className="h-full p-5 flex gap-3 flex-col"
>
<div className="flex flex-col sm:flex-row gap-3 justify-between sm:items-start">
{activeCollection && (
<div className="flex gap-3 items-center">
<div className="flex gap-2">
<FontAwesomeIcon
icon={faFolder}
style={{ color: activeCollection?.color }}
className="sm:w-8 sm:h-8 w-6 h-6 mt-3 drop-shadow"
/>
<p className="sm:text-4xl text-3xl capitalize w-full py-1 break-words hyphens-auto font-thin">
{activeCollection?.name}
</p>
</div>
2023-11-30 05:55:37 -06:00
</div>
)}
</div>
2023-11-30 05:55:37 -06:00
{activeCollection ? (
<div className={`min-w-[15rem]`}>
<div className="flex gap-1 justify-center sm:justify-end items-center w-fit">
2023-05-27 14:05:07 -05:00
<div
2023-11-30 05:13:42 -06:00
className="flex items-center btn px-2 btn-ghost rounded-full w-fit"
onClick={() => setEditCollectionSharingModal(true)}
2023-05-27 14:05:07 -05:00
>
2023-11-30 05:13:42 -06:00
{collectionOwner.id ? (
2023-12-05 03:39:01 -06:00
<ProfilePhoto
src={collectionOwner.image || undefined}
name={collectionOwner.name}
/>
2023-11-30 05:13:42 -06:00
) : undefined}
{activeCollection.members
.sort((a, b) => (a.userId as number) - (b.userId as number))
.map((e, i) => {
return (
<ProfilePhoto
key={i}
src={e.user.image ? e.user.image : undefined}
className="-ml-3"
2023-12-05 03:39:01 -06:00
name={e.user.name}
2023-11-30 05:13:42 -06:00
/>
);
})
.slice(0, 3)}
{activeCollection.members.length - 3 > 0 ? (
2023-12-05 03:39:01 -06:00
<div className={`avatar drop-shadow-md placeholder -ml-3`}>
2023-11-30 05:13:42 -06:00
<div className="bg-base-100 text-neutral rounded-full w-8 h-8 ring-2 ring-neutral-content">
<span>+{activeCollection.members.length - 3}</span>
2023-05-25 13:45:54 -05:00
</div>
2023-11-30 05:13:42 -06:00
</div>
) : null}
2023-05-25 13:44:08 -05:00
</div>
2023-12-04 09:24:45 -06:00
<p className="text-neutral text-sm font-semibold">
2023-11-30 05:55:37 -06:00
By {collectionOwner.name}
{activeCollection.members.length > 0
? ` and ${activeCollection.members.length} others`
: undefined}
.
</p>
</div>
2023-05-25 13:44:08 -05:00
</div>
2023-11-30 05:55:37 -06:00
) : undefined}
2023-11-30 05:55:37 -06:00
{activeCollection?.description ? (
<p>{activeCollection?.description}</p>
) : undefined}
<div className="divider my-0"></div>
2023-11-30 05:55:37 -06:00
<div className="flex justify-between items-end gap-5">
2023-11-30 10:47:24 -06:00
<p>Showing {activeCollection?._count?.links} results</p>
2023-11-30 05:55:37 -06:00
<div className="flex items-center gap-2">
<SortDropdown sortBy={sortBy} setSort={setSortBy} />
<div className="relative">
<div className="dropdown dropdown-bottom dropdown-end">
<div
tabIndex={0}
role="button"
className="btn btn-ghost btn-sm btn-square text-neutral"
>
<FontAwesomeIcon
icon={faEllipsis}
title="More"
className="w-5 h-5"
/>
</div>
<ul className="dropdown-content z-[30] menu shadow bg-base-200 border border-neutral-content rounded-box w-52 mt-1">
2023-11-30 05:55:37 -06:00
{permissions === true ? (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setEditCollectionModal(true);
}}
>
2023-11-30 05:55:37 -06:00
Edit Collection Info
</div>
</li>
2023-11-30 05:55:37 -06:00
) : undefined}
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setEditCollectionSharingModal(true);
2023-11-30 05:55:37 -06:00
}}
>
{permissions === true
? "Share and Collaborate"
: "View Team"}
</div>
</li>
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setDeleteCollectionModal(true);
2023-11-30 05:55:37 -06:00
}}
>
{permissions === true
? "Delete Collection"
: "Leave Collection"}
</div>
</li>
</ul>
2023-05-25 13:44:08 -05:00
</div>
</div>
2023-04-30 15:54:40 -05:00
</div>
2023-04-07 20:10:55 -05:00
</div>
2023-11-30 05:55:37 -06:00
{links.some((e) => e.collectionId === Number(router.query.id)) ? (
2023-07-24 06:25:15 -05:00
<div className="grid grid-cols-1 2xl:grid-cols-3 xl:grid-cols-2 gap-5">
{links
.filter((e) => e.collection.id === activeCollection?.id)
.map((e, i) => {
return <LinkCard key={i} link={e} count={i} />;
})}
2023-07-19 16:49:54 -05:00
</div>
) : (
<NoLinksFound />
)}
2023-04-07 20:10:55 -05:00
</div>
{activeCollection ? (
<>
2023-12-01 16:42:45 -06:00
{editCollectionModal ? (
<EditCollectionModal
onClose={() => setEditCollectionModal(false)}
activeCollection={activeCollection}
/>
) : undefined}
{editCollectionSharingModal ? (
<EditCollectionSharingModal
onClose={() => setEditCollectionSharingModal(false)}
activeCollection={activeCollection}
/>
) : undefined}
{deleteCollectionModal ? (
<DeleteCollectionModal
onClose={() => setDeleteCollectionModal(false)}
activeCollection={activeCollection}
/>
) : undefined}
</>
) : undefined}
</MainLayout>
2023-03-05 15:03:20 -06:00
);
}