import React, { useEffect, useState, useRef } from "react"; import { useRouter } from "next/router"; import Link from "next/link"; import { mutate } from "swr"; // react-hook-form import { useForm } from "react-hook-form"; // react-beautiful-dnd import { Draggable } from "react-beautiful-dnd"; // services import pagesService from "services/pages.service"; import issuesService from "services/issues.service"; import aiService from "services/ai.service"; // hooks import useToast from "hooks/use-toast"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // components import { GptAssistantModal } from "components/core"; import { CreateUpdateBlockInline } from "components/pages"; // ui import { CustomMenu, TextArea } from "components/ui"; // icons import { LayerDiagonalIcon } from "components/icons"; import { ArrowPathIcon, LinkIcon } from "@heroicons/react/20/solid"; import { BoltIcon, CheckIcon, EllipsisVerticalIcon, PencilIcon, SparklesIcon, TrashIcon, } from "@heroicons/react/24/outline"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; // types import { ICurrentUserResponse, IIssue, IPageBlock, IProject } from "types"; // fetch-keys import { PAGE_BLOCKS_LIST } from "constants/fetch-keys"; import Tiptap, { ITiptapRichTextEditor } from "components/tiptap"; type Props = { block: IPageBlock; projectDetails: IProject | undefined; showBlockDetails: boolean; index: number; user: ICurrentUserResponse | undefined; }; const TiptapEditor = React.forwardRef< ITiptapRichTextEditor, ITiptapRichTextEditor >((props, ref) => ); TiptapEditor.displayName = "TiptapEditor"; export const SinglePageBlock: React.FC = ({ block, projectDetails, showBlockDetails, index, user, }) => { const [isSyncing, setIsSyncing] = useState(false); const [createBlockForm, setCreateBlockForm] = useState(false); const [iAmFeelingLucky, setIAmFeelingLucky] = useState(false); const [gptAssistantModal, setGptAssistantModal] = useState(false); const [isMenuActive, setIsMenuActive] = useState(false); const actionSectionRef = useRef(null); const router = useRouter(); const { workspaceSlug, projectId, pageId } = router.query; const { setToastAlert } = useToast(); const { handleSubmit, watch, reset, setValue, register } = useForm({ defaultValues: { name: "", description: {}, description_html: "

", }, }); const updatePageBlock = async (formData: Partial) => { if (!workspaceSlug || !projectId || !pageId) return; if (!formData.name || formData.name.length === 0 || formData.name === "") return; if (block.issue && block.sync) setIsSyncing(true); mutate( PAGE_BLOCKS_LIST(pageId as string), (prevData) => prevData?.map((p) => { if (p.id === block.id) return { ...p, ...formData }; return p; }), false ); await pagesService .patchPageBlock( workspaceSlug as string, projectId as string, pageId as string, block.id, { name: formData.name, description: formData.description, description_html: formData.description_html, }, user ) .then((res) => { mutate(PAGE_BLOCKS_LIST(pageId as string)); if (block.issue && block.sync) issuesService .patchIssue( workspaceSlug as string, projectId as string, block.issue, { name: res.name, description: res.description, description_html: res.description_html, }, user ) .finally(() => setIsSyncing(false)); }); }; const pushBlockIntoIssues = async () => { if (!workspaceSlug || !projectId || !pageId) return; await pagesService .convertPageBlockToIssue( workspaceSlug as string, projectId as string, pageId as string, block.id, user ) .then((res: IIssue) => { mutate( PAGE_BLOCKS_LIST(pageId as string), (prevData) => (prevData ?? []).map((p) => { if (p.id === block.id) return { ...p, issue: res.id, issue_detail: res }; return p; }), false ); setToastAlert({ type: "success", title: "Success!", message: "Page block converted to issue successfully.", }); }) .catch((res) => { setToastAlert({ type: "error", title: "Error!", message: "Page block could not be converted to issue. Please try again.", }); }); }; const deletePageBlock = async () => { if (!workspaceSlug || !projectId || !pageId) return; mutate( PAGE_BLOCKS_LIST(pageId as string), (prevData) => (prevData ?? []).filter((p) => p.id !== block.id), false ); await pagesService .deletePageBlock( workspaceSlug as string, projectId as string, pageId as string, block.id, user ) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Page could not be deleted. Please try again.", }); }); }; const handleAutoGenerateDescription = async () => { if (!workspaceSlug || !projectId) return; setIAmFeelingLucky(true); aiService .createGptTask( workspaceSlug as string, projectId as string, { prompt: block.name, task: "Generate a proper description for this issue.", }, user ) .then((res) => { if (res.response === "") setToastAlert({ type: "error", title: "Error!", message: "Block title isn't informative enough to generate the description. Please try with a different title.", }); else handleAiAssistance(res.response_html); }) .catch((err) => { if (err.status === 429) setToastAlert({ type: "error", title: "Error!", message: "You have reached the maximum number of requests of 50 requests per month per user.", }); else setToastAlert({ type: "error", title: "Error!", message: "Some error occurred. Please try again.", }); }) .finally(() => setIAmFeelingLucky(false)); }; const handleAiAssistance = async (response: string) => { if (!workspaceSlug || !projectId) return; setValue("description", {}); setValue("description_html", `${watch("description_html")}

${response}

`); handleSubmit(updatePageBlock)() .then(() => { setToastAlert({ type: "success", title: "Success!", message: "Block description updated successfully.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Block description could not be updated. Please try again.", }); }); }; const handleBlockSync = () => { if (!workspaceSlug || !projectId || !pageId) return; mutate( PAGE_BLOCKS_LIST(pageId as string), (prevData) => (prevData ?? []).map((p) => { if (p.id === block.id) return { ...p, sync: !block.sync }; return p; }), false ); pagesService.patchPageBlock( workspaceSlug as string, projectId as string, pageId as string, block.id, { sync: !block.sync, }, user ); }; const handleCopyText = () => { const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard( `${originURL}/${workspaceSlug}/projects/${projectId}/issues/${block.issue}` ).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Issue link copied to clipboard.", }); }); }; useEffect(() => { if (!block) return; reset({ ...block }); }, [reset, block]); useOutsideClickDetector(actionSectionRef, () => setIsMenuActive(false)); return ( {(provided, snapshot) => ( <> {createBlockForm ? (
setCreateBlockForm(false)} data={block} setIsSyncing={setIsSyncing} focus="name" user={user} />
) : (
setCreateBlockForm(true)} >
{block.issue && ( )}