import { useState } from "react"; import { CollectionIncludingMembersAndLinkCount, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import usePermissions from "@/hooks/usePermissions"; import EditLinkModal from "@/components/ModalContent/EditLinkModal"; import DeleteLinkModal from "@/components/ModalContent/DeleteLinkModal"; import PreservedFormatsModal from "@/components/ModalContent/PreservedFormatsModal"; import useLinkStore from "@/store/links"; import { toast } from "react-hot-toast"; import useAccountStore from "@/store/account"; type Props = { link: LinkIncludingShortenedCollectionAndTags; collection: CollectionIncludingMembersAndLinkCount; position?: string; }; export default function LinkActions({ link, collection, position }: Props) { const permissions = usePermissions(link.collection.id as number); const [editLinkModal, setEditLinkModal] = useState(false); const [deleteLinkModal, setDeleteLinkModal] = useState(false); const [preservedFormatsModal, setPreservedFormatsModal] = useState(false); const { account } = useAccountStore(); const { removeLink, updateLink } = useLinkStore(); const pinLink = async () => { const isAlreadyPinned = link?.pinnedBy && link.pinnedBy[0]; const load = toast.loading("Applying..."); const response = await updateLink({ ...link, pinnedBy: isAlreadyPinned ? undefined : [{ id: account.id }], }); toast.dismiss(load); response.ok && toast.success(`Link ${isAlreadyPinned ? "Unpinned!" : "Pinned!"}`); }; const deleteLink = async () => { const load = toast.loading("Deleting..."); const response = await removeLink(link.id as number); toast.dismiss(load); response.ok && toast.success(`Link Deleted.`); }; return (
    {permissions === true ? (
  • { (document?.activeElement as HTMLElement)?.blur(); pinLink(); }} > {link?.pinnedBy && link.pinnedBy[0] ? "Unpin" : "Pin to Dashboard"}
  • ) : undefined} {permissions === true || permissions?.canUpdate ? (
  • { (document?.activeElement as HTMLElement)?.blur(); setEditLinkModal(true); }} > Edit
  • ) : undefined}
  • { (document?.activeElement as HTMLElement)?.blur(); setPreservedFormatsModal(true); // updateArchive(); }} > Preserved Formats
  • {permissions === true || permissions?.canDelete ? (
  • { (document?.activeElement as HTMLElement)?.blur(); e.shiftKey ? deleteLink() : setDeleteLinkModal(true); }} > Delete
  • ) : undefined}
{editLinkModal ? ( setEditLinkModal(false)} activeLink={link} /> ) : undefined} {deleteLinkModal ? ( setDeleteLinkModal(false)} activeLink={link} /> ) : undefined} {preservedFormatsModal ? ( setPreservedFormatsModal(false)} activeLink={link} /> ) : undefined} {/* {expandedLink ? ( setExpandedLink(false)} link={link} /> ) : undefined} */}
); }