el.xwx.moe/components/CollectionCard.tsx

216 lines
7.6 KiB
TypeScript
Raw Normal View History

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2023-10-24 16:03:33 -05:00
import { faEllipsis, faGlobe, faLink } from "@fortawesome/free-solid-svg-icons";
2023-03-05 15:03:20 -06:00
import Link from "next/link";
2023-06-16 07:55:21 -05:00
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
2023-11-27 15:38:38 -06:00
import { useEffect, useState } from "react";
2023-05-27 14:05:07 -05:00
import ProfilePhoto from "./ProfilePhoto";
2023-06-11 17:28:37 -05:00
import { faCalendarDays } from "@fortawesome/free-regular-svg-icons";
import usePermissions from "@/hooks/usePermissions";
2023-11-24 06:50:16 -06:00
import useLocalSettingsStore from "@/store/localSettings";
2023-11-27 15:38:38 -06:00
import getPublicUserData from "@/lib/client/getPublicUserData";
import useAccountStore from "@/store/account";
import EditCollectionModal from "./Modals/EditCollectionModal";
import EditCollectionSharingModal from "./Modals/EditCollectionSharingModal";
2023-12-01 13:00:52 -06:00
import DeleteCollectionModal from "./Modals/DeleteCollectionModal";
2023-06-12 13:23:11 -05:00
type Props = {
2023-06-16 07:55:21 -05:00
collection: CollectionIncludingMembersAndLinkCount;
2023-06-12 13:23:11 -05:00
className?: string;
};
export default function CollectionCard({ collection, className }: Props) {
2023-11-24 06:50:16 -06:00
const { settings } = useLocalSettingsStore();
2023-11-27 15:38:38 -06:00
const { account } = useAccountStore();
2023-06-12 23:46:32 -05:00
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",
}
);
const permissions = usePermissions(collection.id as number);
2023-11-27 15:38:38 -06:00
const [collectionOwner, setCollectionOwner] = useState({
id: null as unknown as number,
name: "",
username: "",
image: "",
});
useEffect(() => {
const fetchOwner = async () => {
if (collection && collection.ownerId !== account.id) {
const owner = await getPublicUserData(collection.ownerId as number);
setCollectionOwner(owner);
} else if (collection && collection.ownerId === account.id) {
setCollectionOwner({
id: account.id as number,
name: account.name,
username: account.username as string,
image: account.image as string,
});
}
};
fetchOwner();
}, [collection]);
const [editCollectionModal, setEditCollectionModal] = useState(false);
const [editCollectionSharingModal, setEditCollectionSharingModal] =
useState(false);
2023-12-01 13:00:52 -06:00
const [deleteCollectionModal, setDeleteCollectionModal] = useState(false);
return (
2023-11-27 15:38:38 -06:00
<div className="relative">
2023-11-29 14:17:51 -06:00
<div className="dropdown dropdown-bottom dropdown-end absolute top-3 right-3 z-10">
2023-10-28 23:57:24 -05:00
<div
2023-11-27 15:38:38 -06:00
tabIndex={0}
role="button"
className="btn btn-ghost btn-sm btn-square text-neutral"
2023-10-28 23:57:24 -05:00
>
<FontAwesomeIcon icon={faEllipsis} title="More" className="w-5 h-5" />
2023-10-28 23:57:24 -05:00
</div>
<ul className="dropdown-content z-[1] menu p-1 shadow bg-base-200 border border-neutral-content rounded-xl w-44 mt-1">
2023-11-27 15:38:38 -06:00
{permissions === true ? (
<li>
<div
role="button"
className="px-2 py-1 rounded-lg"
2023-11-27 15:38:38 -06:00
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setEditCollectionModal(true);
}}
2023-11-27 15:38:38 -06:00
>
Edit Collection Info
</div>
</li>
) : undefined}
<li>
<div
className="px-2 py-1 rounded-lg"
2023-11-27 15:38:38 -06:00
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setEditCollectionSharingModal(true);
}}
2023-11-27 15:38:38 -06:00
>
{permissions === true ? "Share and Collaborate" : "View Team"}
</div>
</li>
<li>
<div
className="px-2 py-1 rounded-lg"
2023-11-27 15:38:38 -06:00
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
2023-12-01 13:00:52 -06:00
setDeleteCollectionModal(true);
}}
2023-11-27 15:38:38 -06:00
>
{permissions === true ? "Delete Collection" : "Leave Collection"}
</div>
</li>
</ul>
</div>
<div
className="flex items-center absolute bottom-3 left-3 z-10 btn px-2 btn-ghost rounded-full"
onClick={() => setEditCollectionSharingModal(true)}
>
{collectionOwner.id ? (
<ProfilePhoto
src={collectionOwner.image || undefined}
2023-12-01 13:00:52 -06:00
dimensionClass="w-7 h-7"
/>
) : undefined}
{collection.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"
/>
);
})
.slice(0, 3)}
{collection.members.length - 3 > 0 ? (
<div className={`avatar placeholder -ml-3`}>
<div className="bg-base-100 text-neutral rounded-full w-8 h-8 ring-2 ring-neutral-content">
<span>+{collection.members.length - 3}</span>
</div>
</div>
) : null}
</div>
2023-11-27 15:38:38 -06:00
<Link
href={`/collections/${collection.id}`}
style={{
backgroundImage: `linear-gradient(45deg, ${collection.color}30 10%, ${
settings.theme === "dark" ? "oklch(var(--b2))" : "oklch(var(--b2))"
} 50%, ${
settings.theme === "dark" ? "oklch(var(--b2))" : "oklch(var(--b2))"
} 100%)`,
}}
className="card card-compact shadow-md hover:shadow-none duration-200 border border-neutral-content"
2023-11-27 15:38:38 -06:00
>
<div className="card-body flex flex-col justify-between min-h-[12rem]">
<div className="flex justify-between">
<p className="card-title break-words line-clamp-2 w-full">
{collection.name}
</p>
<div className="w-8 h-8 ml-10"></div>
</div>
<div className="flex justify-end items-center">
<div className="text-right">
2023-11-24 07:39:55 -06:00
<div className="font-bold text-sm flex justify-end gap-1 items-center">
2023-10-28 23:57:24 -05:00
{collection.isPublic ? (
<FontAwesomeIcon
icon={faGlobe}
title="This collection is being shared publicly."
2023-11-25 04:39:56 -06:00
className="w-4 h-4 drop-shadow text-neutral"
2023-06-12 13:23:11 -05:00
/>
2023-10-28 23:57:24 -05:00
) : undefined}
2023-10-24 16:03:33 -05:00
<FontAwesomeIcon
2023-10-28 23:57:24 -05:00
icon={faLink}
2023-11-25 04:39:56 -06:00
className="w-5 h-5 text-neutral"
2023-10-24 16:03:33 -05:00
/>
2023-10-28 23:57:24 -05:00
{collection._count && collection._count.links}
</div>
<div className="flex items-center justify-end gap-1 text-neutral">
<p className="font-bold text-xs flex gap-1 items-center">
<FontAwesomeIcon icon={faCalendarDays} className="w-4 h-4" />{" "}
{formattedDate}
</p>
2023-10-28 23:57:24 -05:00
</div>
2023-05-25 09:17:20 -05:00
</div>
2023-05-24 20:47:18 -05:00
</div>
2023-11-27 15:38:38 -06:00
</div>
</Link>
<EditCollectionModal
isOpen={editCollectionModal}
onClose={() => setEditCollectionModal(false)}
modalId={"edit-collection-modal" + collection.id}
activeCollection={collection}
/>
<EditCollectionSharingModal
isOpen={editCollectionSharingModal}
onClose={() => setEditCollectionSharingModal(false)}
modalId={"edit-collection-sharing-modal" + collection.id}
activeCollection={collection}
/>
2023-12-01 13:00:52 -06:00
<DeleteCollectionModal
isOpen={deleteCollectionModal}
onClose={() => setDeleteCollectionModal(false)}
modalId={"delete-collection-modal" + collection.id}
activeCollection={collection}
/>
2023-11-27 15:38:38 -06:00
</div>
);
}