import { FC, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { mutate } from "swr"; // headless ui import { Disclosure, Transition } from "@headlessui/react"; // services import issuesService from "services/issues.service"; // hooks import useToast from "hooks/use-toast"; // components import { LinkModal } from "components/core"; // ui import { CustomMenu } from "components/ui"; // icons import { ChevronRightIcon, LinkIcon, PlusIcon } from "@heroicons/react/24/outline"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; import { timeAgo } from "helpers/date-time.helper"; // types import { IIssue, IIssueLink, UserAuth } from "types"; // fetch-keys import { ISSUE_DETAILS } from "constants/fetch-keys"; type Props = { parentIssue: IIssue; userAuth: UserAuth; }; export const LinksList: FC = ({ parentIssue, userAuth }) => { const [linkModal, setLinkModal] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const handleCreateLink = async (formData: IIssueLink) => { if (!workspaceSlug || !projectId || !parentIssue) return; const previousLinks = parentIssue?.issue_link.map((l) => ({ title: l.title, url: l.url })); const payload: Partial = { links_list: [...(previousLinks ?? []), formData], }; await issuesService .patchIssue(workspaceSlug as string, projectId as string, parentIssue.id, payload) .then((res) => { mutate(ISSUE_DETAILS(parentIssue.id as string)); }) .catch((err) => { console.log(err); }); }; const handleDeleteLink = async (linkId: string) => { if (!workspaceSlug || !projectId || !parentIssue) return; const updatedLinks = parentIssue.issue_link.filter((l) => l.id !== linkId); mutate( ISSUE_DETAILS(parentIssue.id as string), (prevData) => ({ ...(prevData as IIssue), issue_link: updatedLinks }), false ); await issuesService .patchIssue(workspaceSlug as string, projectId as string, parentIssue.id, { links_list: updatedLinks, }) .then((res) => { mutate(ISSUE_DETAILS(parentIssue.id as string)); }) .catch((err) => { console.log(err); }); }; const isNotAllowed = userAuth.isGuest || userAuth.isViewer; return ( <> setLinkModal(false)} onFormSubmit={handleCreateLink} /> {parentIssue.issue_link && parentIssue.issue_link.length > 0 ? ( {({ open }) => ( <>
Links {parentIssue.issue_link.length} {open && !isNotAllowed ? (
) : null}
{parentIssue.issue_link.map((link) => (
{link.title} {timeAgo(link.created_at)} {!isNotAllowed && (
copyTextToClipboard(link.url).then(() => { setToastAlert({ type: "success", title: "Link copied to clipboard", }); }) } > Copy link handleDeleteLink(link.id)}> Remove link
)}
))}
)}
) : ( !isNotAllowed && ( ) )} ); };