import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faClose, faCrown, faPenToSquare, faPlus, faUserPlus, } from "@fortawesome/free-solid-svg-icons"; import useCollectionStore from "@/store/collections"; import { CollectionIncludingMembersAndLinkCount, Member } from "@/types/global"; import addMemberToCollection from "@/lib/client/addMemberToCollection"; import Checkbox from "../../Checkbox"; import SubmitButton from "@/components/SubmitButton"; import ProfilePhoto from "@/components/ProfilePhoto"; import usePermissions from "@/hooks/usePermissions"; import { toast } from "react-hot-toast"; import getPublicUserData from "@/lib/client/getPublicUserData"; import TextInput from "@/components/TextInput"; import useAccountStore from "@/store/account"; type Props = { toggleCollectionModal: Function; setCollection: Dispatch< SetStateAction >; collection: CollectionIncludingMembersAndLinkCount; method: "CREATE" | "UPDATE"; }; export default function TeamManagement({ toggleCollectionModal, setCollection, collection, method, }: Props) { const { account } = useAccountStore(); const permissions = usePermissions(collection.id as number); const currentURL = new URL(document.URL); const publicCollectionURL = `${currentURL.origin}/public/collections/${collection.id}`; const [memberUsername, setMemberUsername] = useState(""); const [collectionOwner, setCollectionOwner] = useState({ id: null, name: "", username: "", image: "", }); useEffect(() => { const fetchOwner = async () => { const owner = await getPublicUserData(collection.ownerId as number); setCollectionOwner(owner); }; fetchOwner(); }, []); const { addCollection, updateCollection } = useCollectionStore(); const setMemberState = (newMember: Member) => { if (!collection) return null; setCollection({ ...collection, members: [...collection.members, newMember], }); setMemberUsername(""); }; const [submitLoader, setSubmitLoader] = useState(false); const submit = async () => { if (!collection) return null; setSubmitLoader(true); const load = toast.loading( method === "UPDATE" ? "Applying..." : "Creating..." ); let response; if (method === "CREATE") response = await addCollection(collection); else response = await updateCollection(collection); toast.dismiss(load); if (response.ok) { toast.success("Collection Saved!"); toggleCollectionModal(); } else toast.error(response.data as string); setSubmitLoader(false); }; return (
{permissions === true && ( <>

Make Public

setCollection({ ...collection, isPublic: !collection.isPublic }) } />

This will let Anyone to view this collection.

)} {collection.isPublic ? (

Public Link (Click to copy)

{ try { navigator.clipboard .writeText(publicCollectionURL) .then(() => toast.success("Copied!")); } catch (err) { console.log(err); } }} className="w-full hide-scrollbar overflow-x-auto whitespace-nowrap rounded-md p-2 dark:bg-neutral-950 border-neutral-content border-solid border outline-none hover:border-sky-300 dark:hover:border-sky-600 duration-100 cursor-text" > {publicCollectionURL}
) : null} {permissions !== true && collection.isPublic && (
)} {permissions === true && ( <>

Member Management

setMemberUsername(e.target.value)} onKeyDown={(e) => e.key === "Enter" && addMemberToCollection( account.username as string, memberUsername || "", collection, setMemberState ) } />
addMemberToCollection( account.username as string, memberUsername || "", collection, setMemberState ) } className="flex items-center justify-center bg-sky-700 hover:bg-sky-600 duration-100 text-white w-10 h-10 p-2 rounded-md cursor-pointer" >
)} {collection?.members[0]?.user && ( <>

(All Members have Read access to this collection.)

{collection.members .sort((a, b) => (a.userId as number) - (b.userId as number)) .map((e, i) => { return (
{permissions === true && ( { const updatedMembers = collection.members.filter( (member) => { return member.user.username !== e.user.username; } ); setCollection({ ...collection, members: updatedMembers, }); }} /> )}

{e.user.name}

@{e.user.username}

Permissions

{permissions === true && (

(Click to toggle.)

)}
{permissions !== true && !e.canCreate && !e.canUpdate && !e.canDelete ? (

Has no permissions.

) : (
)}
); })}
)}

{collectionOwner.name}

@{collectionOwner.username}

Permissions

Full Access (Owner)

{permissions === true && ( )}
); }