import { useState } from "react"; import { CollectionIncludingMembersAndLinkCount, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import usePermissions from "@/hooks/usePermissions"; import DeleteLinkModal from "@/components/ModalContent/DeleteLinkModal"; import { dropdownTriggerer } from "@/lib/client/utils"; import { useTranslation } from "next-i18next"; import { useUser } from "@/hooks/store/user"; import { useDeleteLink, useGetLink, useUpdateLink } from "@/hooks/store/links"; import toast from "react-hot-toast"; import LinkModal from "@/components/ModalContent/LinkModal"; import { useRouter } from "next/router"; type Props = { link: LinkIncludingShortenedCollectionAndTags; collection: CollectionIncludingMembersAndLinkCount; className?: string; }; export default function LinkActions({ link, className }: Props) { const { t } = useTranslation(); const permissions = usePermissions(link.collection.id as number); const getLink = useGetLink(); const [editLinkModal, setEditLinkModal] = useState(false); const [linkModal, setLinkModal] = useState(false); const [deleteLinkModal, setDeleteLinkModal] = useState(false); const { data: user = {} } = useUser(); const updateLink = useUpdateLink(); const deleteLink = useDeleteLink(); const pinLink = async () => { const isAlreadyPinned = link?.pinnedBy && link.pinnedBy[0] ? true : false; const load = toast.loading(t("updating")); await updateLink.mutateAsync( { ...link, pinnedBy: isAlreadyPinned ? [{ id: undefined }] : [{ id: user.id }], }, { onSettled: (data, error) => { toast.dismiss(load); if (error) { toast.error(error.message); } else { toast.success( isAlreadyPinned ? t("link_unpinned") : t("link_pinned") ); } }, } ); }; const updateArchive = async () => { const load = toast.loading(t("sending_request")); const response = await fetch(`/api/v1/links/${link?.id}/archive`, { method: "PUT", }); const data = await response.json(); toast.dismiss(load); if (response.ok) { await getLink.mutateAsync({ id: link.id as number }); toast.success(t("link_being_archived")); } else toast.error(data.response); }; const router = useRouter(); const isPublicRoute = router.pathname.startsWith("/public") ? true : false; return ( <> {isPublicRoute ? (