el.xwx.moe/components/CollectionCard.tsx

155 lines
5.5 KiB
TypeScript
Raw Normal View History

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2023-05-27 22:21:35 -05:00
import { faEllipsis } from "@fortawesome/free-solid-svg-icons";
2023-03-05 15:03:20 -06:00
import Link from "next/link";
2023-05-26 23:11:29 -05:00
import { CollectionIncludingMembers } from "@/types/global";
import useLinkStore from "@/store/links";
2023-05-25 13:44:08 -05:00
import Dropdown from "./Dropdown";
import { useState } from "react";
import Modal from "@/components/Modal";
2023-05-28 00:55:49 -05:00
import CollectionInfo from "@/components/Modal/Collection/CollectionInfo";
import DeleteCollection from "@/components/Modal/Collection/DeleteCollection";
2023-05-27 14:05:07 -05:00
import ProfilePhoto from "./ProfilePhoto";
2023-05-28 17:06:49 -05:00
import TeamManagement from "./Modal/Collection/TeamManagement";
2023-05-26 23:11:29 -05:00
export default function ({
collection,
}: {
collection: CollectionIncludingMembers;
}) {
const { links } = useLinkStore();
2023-05-26 23:29:45 -05:00
const formattedDate = new Date(collection.createdAt as string).toLocaleString(
"en-US",
{
year: "numeric",
month: "short",
day: "numeric",
}
);
2023-05-25 13:44:08 -05:00
const [expandDropdown, setExpandDropdown] = useState(false);
const [editCollectionModal, setEditCollectionModal] = useState(false);
2023-05-28 17:06:49 -05:00
const [collectionMembersModal, setCollectionMembersModal] = useState(false);
2023-05-25 13:44:08 -05:00
const [deleteCollectionModal, setDeleteCollectionModal] = useState(false);
const toggleEditCollectionModal = () => {
setEditCollectionModal(!editCollectionModal);
};
2023-05-28 17:06:49 -05:00
const toggleCollectionMembersModal = () => {
setCollectionMembersModal(!collectionMembersModal);
};
2023-05-25 13:44:08 -05:00
const toggleDeleteCollectionModal = () => {
setDeleteCollectionModal(!deleteCollectionModal);
};
return (
2023-05-26 14:52:18 -05:00
<div className="bg-gradient-to-tr from-sky-100 from-10% via-gray-100 via-20% self-stretch min-h-[12rem] rounded-md shadow duration-100 hover:shadow-none group relative">
2023-05-25 13:44:08 -05:00
<div
onClick={() => setExpandDropdown(!expandDropdown)}
2023-05-27 22:21:35 -05:00
id={"expand-dropdown" + collection.id}
2023-05-25 13:44:08 -05:00
className="inline-flex absolute top-5 right-5 rounded-md cursor-pointer hover:bg-white hover:border-sky-500 border-sky-100 border duration-100 p-1"
>
<FontAwesomeIcon
icon={faEllipsis}
2023-05-27 22:21:35 -05:00
id={"expand-dropdown" + collection.id}
2023-05-25 13:44:08 -05:00
className="w-5 h-5 text-gray-500"
/>
</div>
2023-05-25 09:17:20 -05:00
<Link href={`/collections/${collection.id}`}>
2023-05-26 14:52:18 -05:00
<div className="flex flex-col gap-2 justify-between h-full select-none p-5 cursor-pointer">
2023-05-31 09:30:45 -05:00
<p className="text-2xl font-bold capitalize bg-gradient-to-tr from-sky-500 to-slate-400 bg-clip-text text-transparent break-words w-4/5">
2023-05-25 21:18:02 -05:00
{collection.name}
</p>
2023-05-25 09:17:20 -05:00
<div className="flex justify-between items-center">
<div className="text-sky-400 flex items-center w-full">
{collection.members
2023-05-28 00:55:49 -05:00
.sort((a, b) => (a.userId as number) - (b.userId as number))
2023-05-25 09:17:20 -05:00
.map((e, i) => {
return (
2023-05-27 14:05:07 -05:00
<ProfilePhoto
2023-05-25 09:17:20 -05:00
key={i}
src={`/api/avatar/${e.userId}`}
2023-05-27 14:05:07 -05:00
className="-mr-3"
/>
2023-05-25 09:17:20 -05:00
);
})
2023-05-25 13:44:08 -05:00
.slice(0, 4)}
{collection.members.length - 4 > 0 ? (
2023-05-25 09:17:20 -05:00
<div className="h-10 w-10 text-white flex items-center justify-center rounded-full border-[3px] bg-sky-500 border-sky-100 -mr-3">
2023-05-27 10:07:30 -05:00
+{collection.members.length - 4}
2023-05-25 09:17:20 -05:00
</div>
) : null}
</div>
2023-05-25 13:44:08 -05:00
<div className="text-right w-40">
2023-05-25 09:17:20 -05:00
<p className="text-sky-500 font-bold text-sm">
{links.filter((e) => e.collectionId === collection.id).length}{" "}
Links
</p>
<p className="text-gray-500 font-bold text-xs">{formattedDate}</p>
</div>
2023-05-24 20:47:18 -05:00
</div>
2023-03-05 15:03:20 -06:00
</div>
2023-05-25 09:17:20 -05:00
</Link>
2023-05-25 13:44:08 -05:00
{expandDropdown ? (
<Dropdown
items={[
{
name: "Edit Collection",
onClick: () => {
toggleEditCollectionModal();
setExpandDropdown(false);
},
},
2023-05-28 17:06:49 -05:00
{
name: "Share/Collaborate",
onClick: () => {
toggleCollectionMembersModal();
setExpandDropdown(false);
},
},
2023-05-25 13:44:08 -05:00
{
name: "Delete Collection",
onClick: () => {
toggleDeleteCollectionModal();
setExpandDropdown(false);
},
},
]}
onClickOutside={(e: Event) => {
const target = e.target as HTMLInputElement;
2023-05-27 22:21:35 -05:00
if (target.id !== "expand-dropdown" + collection.id)
setExpandDropdown(false);
2023-05-25 13:44:08 -05:00
}}
2023-05-27 22:21:35 -05:00
className="absolute top-[3.2rem] right-5 z-10 w-36"
2023-05-25 13:44:08 -05:00
/>
) : null}
{editCollectionModal ? (
<Modal toggleModal={toggleEditCollectionModal}>
2023-05-28 00:55:49 -05:00
<CollectionInfo
2023-05-25 13:44:08 -05:00
toggleCollectionModal={toggleEditCollectionModal}
2023-05-26 23:11:29 -05:00
activeCollection={collection}
2023-05-26 23:29:45 -05:00
method="UPDATE"
2023-05-25 13:44:08 -05:00
/>
</Modal>
) : null}
2023-05-28 17:06:49 -05:00
{collectionMembersModal ? (
<Modal toggleModal={toggleCollectionMembersModal}>
<TeamManagement
toggleCollectionModal={toggleCollectionMembersModal}
activeCollection={collection}
/>
</Modal>
) : null}
2023-05-25 13:44:08 -05:00
{deleteCollectionModal ? (
<Modal toggleModal={toggleDeleteCollectionModal}>
<DeleteCollection
collection={collection}
toggleDeleteCollectionModal={toggleDeleteCollectionModal}
/>
</Modal>
) : null}
2023-05-25 09:17:20 -05:00
</div>
);
}