el.xwx.moe/components/CollectionCard.tsx

179 lines
6.4 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-05-25 13:44:08 -05:00
import Dropdown from "./Dropdown";
import { 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";
2023-06-12 23:46:32 -05:00
import useModalStore from "@/store/modals";
import usePermissions from "@/hooks/usePermissions";
2023-11-24 06:50:16 -06:00
import useLocalSettingsStore from "@/store/localSettings";
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;
};
2023-10-28 23:57:24 -05:00
type DropdownTrigger =
| {
x: number;
y: number;
}
| false;
2023-06-12 13:23:11 -05:00
export default function CollectionCard({ collection, className }: Props) {
2023-06-12 23:46:32 -05:00
const { setModal } = useModalStore();
2023-11-24 06:50:16 -06:00
const { settings } = useLocalSettingsStore();
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",
}
);
2023-10-28 23:57:24 -05:00
const [expandDropdown, setExpandDropdown] = useState<DropdownTrigger>(false);
2023-05-25 13:44:08 -05:00
const permissions = usePermissions(collection.id as number);
return (
2023-10-28 23:57:24 -05:00
<>
2023-05-25 13:44:08 -05:00
<div
2023-10-28 23:57:24 -05:00
style={{
backgroundImage: `linear-gradient(45deg, ${collection.color}30 10%, ${
2023-11-24 06:50:16 -06:00
settings.theme === "dark" ? "#262626" : "#f3f4f6"
} 50%, ${settings.theme === "dark" ? "#262626" : "#f9fafb"} 100%)`,
2023-10-28 23:57:24 -05:00
}}
className={`border border-solid border-neutral self-stretch min-h-[12rem] rounded-2xl shadow duration-100 hover:shadow-none hover:opacity-80 group relative ${
2023-10-28 23:57:24 -05:00
className || ""
}`}
2023-05-25 13:44:08 -05:00
>
2023-10-28 23:57:24 -05:00
<div
onClick={(e) => setExpandDropdown({ x: e.clientX, y: e.clientY })}
2023-05-27 22:21:35 -05:00
id={"expand-dropdown" + collection.id}
2023-10-28 23:57:24 -05:00
className="inline-flex absolute top-5 right-5 rounded-md cursor-pointer hover:bg-slate-200 hover:dark:bg-neutral-700 duration-100 p-1"
>
<FontAwesomeIcon
icon={faEllipsis}
id={"expand-dropdown" + collection.id}
className="w-5 h-5 text-gray-500 dark:text-gray-300"
/>
</div>
<Link
href={`/collections/${collection.id}`}
className="flex flex-col gap-2 justify-between min-h-[12rem] h-full select-none p-5"
>
2023-11-24 07:39:55 -06:00
<p className="text-2xl capitalize break-words line-clamp-3 w-4/5">
2023-10-28 23:57:24 -05:00
{collection.name}
</p>
<div className="flex justify-between items-center">
<div className="flex items-center w-full">
{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="-mr-3 border-[3px]"
/>
);
})
.slice(0, 4)}
{collection.members.length - 4 > 0 ? (
<div className="h-10 w-10 text-white flex items-center justify-center rounded-full border-[3px] bg-sky-600 dark:bg-sky-600 border-slate-200 -mr-3">
2023-10-28 23:57:24 -05:00
+{collection.members.length - 4}
</div>
) : null}
</div>
<div className="text-right w-40">
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."
className="w-4 h-4 drop-shadow text-gray-500 dark:text-gray-300"
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}
className="w-5 h-5 text-gray-500 dark:text-gray-300"
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-gray-500 dark:text-gray-300">
<FontAwesomeIcon icon={faCalendarDays} className="w-4 h-4" />
<p className="font-bold text-xs">{formattedDate}</p>
</div>
2023-05-25 09:17:20 -05:00
</div>
2023-05-24 20:47:18 -05:00
</div>
2023-10-28 23:57:24 -05:00
</Link>
</div>
2023-05-25 13:44:08 -05:00
{expandDropdown ? (
<Dropdown
2023-10-28 23:57:24 -05:00
points={{ x: expandDropdown.x, y: expandDropdown.y }}
2023-05-25 13:44:08 -05:00
items={[
2023-06-24 08:17:24 -05:00
permissions === true
? {
name: "Edit Collection Info",
onClick: () => {
collection &&
setModal({
modal: "COLLECTION",
state: true,
method: "UPDATE",
isOwner: permissions === true,
active: collection,
});
setExpandDropdown(false);
},
}
: undefined,
2023-05-25 13:44:08 -05:00
{
2023-06-24 08:17:24 -05:00
name: permissions === true ? "Share/Collaborate" : "View Team",
2023-05-25 13:44:08 -05:00
onClick: () => {
2023-06-24 08:17:24 -05:00
collection &&
setModal({
modal: "COLLECTION",
state: true,
method: "UPDATE",
isOwner: permissions === true,
active: collection,
defaultIndex: permissions === true ? 1 : 0,
});
2023-05-28 17:06:49 -05:00
setExpandDropdown(false);
},
},
2023-06-24 08:17:24 -05:00
2023-05-25 13:44:08 -05:00
{
2023-06-24 08:17:24 -05:00
name:
permissions === true ? "Delete Collection" : "Leave Collection",
2023-05-25 13:44:08 -05:00
onClick: () => {
2023-06-24 08:17:24 -05:00
collection &&
setModal({
modal: "COLLECTION",
state: true,
method: "UPDATE",
isOwner: permissions === true,
active: collection,
defaultIndex: permissions === true ? 2 : 1,
});
2023-05-25 13:44:08 -05:00
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-10-28 23:57:24 -05:00
className="w-fit"
2023-05-25 13:44:08 -05:00
/>
) : null}
2023-10-28 23:57:24 -05:00
</>
);
}