el.xwx.moe/components/CollectionCard.tsx

227 lines
7.9 KiB
TypeScript
Raw Normal View History

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-12-17 02:46:21 -06:00
import React, { useEffect, useState } from "react";
2023-05-27 14:05:07 -05:00
import ProfilePhoto from "./ProfilePhoto";
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";
2023-12-01 16:44:34 -06:00
import EditCollectionModal from "./ModalContent/EditCollectionModal";
import EditCollectionSharingModal from "./ModalContent/EditCollectionSharingModal";
import DeleteCollectionModal from "./ModalContent/DeleteCollectionModal";
2024-01-14 09:09:09 -06:00
import { dropdownTriggerer } from "@/lib/client/utils";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
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) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
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: "",
archiveAsScreenshot: undefined as unknown as boolean,
2024-03-15 13:41:41 -05:00
archiveAsSinglefile: undefined as unknown as boolean,
archiveAsPDF: undefined as unknown as boolean,
2023-11-27 15:38:38 -06:00
});
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,
archiveAsScreenshot: account.archiveAsScreenshot as boolean,
2024-03-15 13:41:41 -05:00
archiveAsSinglefile: account.archiveAsSinglefile as boolean,
archiveAsPDF: account.archiveAsPDF as boolean,
2023-11-27 15:38:38 -06:00
});
}
};
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-12-05 14:17:36 -06:00
<div className="dropdown dropdown-bottom dropdown-end absolute top-3 right-3 z-20">
2023-10-28 23:57:24 -05:00
<div
2023-11-27 15:38:38 -06:00
tabIndex={0}
role="button"
2024-01-14 09:09:09 -06:00
onMouseDown={dropdownTriggerer}
2023-11-27 15:38:38 -06:00
className="btn btn-ghost btn-sm btn-square text-neutral"
2023-10-28 23:57:24 -05:00
>
2023-12-17 02:46:21 -06:00
<i className="bi-three-dots text-xl" title="More"></i>
2023-10-28 23:57:24 -05:00
</div>
2024-06-09 08:27:16 -05:00
<ul className="dropdown-content z-[30] menu shadow bg-base-200 border border-neutral-content rounded-box w-52 mt-1">
{permissions === true && (
2023-11-27 15:38:38 -06:00
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setEditCollectionModal(true);
}}
2023-11-27 15:38:38 -06:00
>
2024-06-09 08:27:16 -05:00
{t("edit_collection_info")}
2023-11-27 15:38:38 -06:00
</div>
</li>
2024-06-09 08:27:16 -05:00
)}
2023-11-27 15:38:38 -06:00
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setEditCollectionSharingModal(true);
}}
2023-11-27 15:38:38 -06:00
>
2024-06-09 08:27:16 -05:00
{permissions === true
? t("share_and_collaborate")
: t("view_team")}
2023-11-27 15:38:38 -06:00
</div>
</li>
<li>
<div
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
>
2024-06-09 08:27:16 -05:00
{permissions === true
? t("delete_collection")
: t("leave_collection")}
2023-11-27 15:38:38 -06:00
</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-05 03:39:01 -06:00
name={collectionOwner.name}
/>
) : 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}
2023-12-05 03:39:01 -06:00
name={e.user.name}
className="-ml-3"
/>
);
})
.slice(0, 3)}
{collection.members.length - 3 > 0 ? (
2023-12-05 03:39:01 -06:00
<div className={`avatar drop-shadow-md 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 ? (
<i
2024-01-17 09:25:24 -06:00
className="bi-globe2 drop-shadow text-neutral"
title="This collection is being shared publicly."
></i>
2023-10-28 23:57:24 -05:00
) : undefined}
<i
className="bi-link-45deg text-lg text-neutral"
title="This collection is being shared publicly."
></i>
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">
<i
className="bi-calendar3 text-neutral"
title="This collection is being shared publicly."
></i>
{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>
2023-12-01 16:42:45 -06:00
{editCollectionModal ? (
<EditCollectionModal
onClose={() => setEditCollectionModal(false)}
activeCollection={collection}
/>
) : undefined}
{editCollectionSharingModal ? (
<EditCollectionSharingModal
onClose={() => setEditCollectionSharingModal(false)}
activeCollection={collection}
/>
) : undefined}
{deleteCollectionModal ? (
<DeleteCollectionModal
onClose={() => setDeleteCollectionModal(false)}
activeCollection={collection}
/>
) : undefined}
2023-11-27 15:38:38 -06:00
</div>
);
}