2023-03-25 18:09:46 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
2023-03-27 19:06:20 +00:00
|
|
|
import Link from "next/link";
|
2023-03-25 18:09:46 +00:00
|
|
|
import dynamic from "next/dynamic";
|
|
|
|
|
|
|
|
import { mutate } from "swr";
|
|
|
|
|
|
|
|
// react-hook-form
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2023-04-04 08:37:17 +00:00
|
|
|
// react-beautiful-dnd
|
|
|
|
import { Draggable } from "react-beautiful-dnd";
|
2023-03-25 18:09:46 +00:00
|
|
|
// services
|
|
|
|
import pagesService from "services/pages.service";
|
2023-03-27 20:22:04 +00:00
|
|
|
import issuesService from "services/issues.service";
|
2023-04-04 08:37:17 +00:00
|
|
|
import aiService from "services/ai.service";
|
2023-03-25 18:09:46 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-03-26 06:06:10 +00:00
|
|
|
// components
|
|
|
|
import { GptAssistantModal } from "components/core";
|
2023-04-03 18:00:29 +00:00
|
|
|
import { CreateUpdateBlockInline } from "components/pages";
|
2023-03-25 18:09:46 +00:00
|
|
|
// ui
|
2023-04-04 08:37:17 +00:00
|
|
|
import { CustomMenu, Loader } from "components/ui";
|
2023-03-25 18:09:46 +00:00
|
|
|
// icons
|
2023-03-28 18:50:00 +00:00
|
|
|
import { LayerDiagonalIcon } from "components/icons";
|
2023-04-11 12:48:49 +00:00
|
|
|
import { ArrowPathIcon, LinkIcon } from "@heroicons/react/20/solid";
|
2023-04-03 18:00:29 +00:00
|
|
|
import {
|
|
|
|
BoltIcon,
|
|
|
|
CheckIcon,
|
|
|
|
EllipsisVerticalIcon,
|
|
|
|
PencilIcon,
|
|
|
|
SparklesIcon,
|
2023-04-11 12:48:49 +00:00
|
|
|
TrashIcon,
|
2023-04-03 18:00:29 +00:00
|
|
|
} from "@heroicons/react/24/outline";
|
2023-03-25 18:09:46 +00:00
|
|
|
// helpers
|
|
|
|
import { copyTextToClipboard } from "helpers/string.helper";
|
|
|
|
// types
|
2023-03-27 19:06:20 +00:00
|
|
|
import { IIssue, IPageBlock, IProject } from "types";
|
2023-03-25 18:09:46 +00:00
|
|
|
// fetch-keys
|
|
|
|
import { PAGE_BLOCKS_LIST } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
block: IPageBlock;
|
2023-03-26 06:06:10 +00:00
|
|
|
projectDetails: IProject | undefined;
|
2023-04-03 18:00:29 +00:00
|
|
|
index: number;
|
2023-03-25 18:09:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor"), {
|
|
|
|
ssr: false,
|
|
|
|
loading: () => (
|
2023-03-27 17:49:05 +00:00
|
|
|
<Loader className="mx-4 mt-6">
|
2023-03-25 18:09:46 +00:00
|
|
|
<Loader.Item height="100px" width="100%" />
|
|
|
|
</Loader>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
export const SinglePageBlock: React.FC<Props> = ({ block, projectDetails, index }) => {
|
2023-03-27 19:06:20 +00:00
|
|
|
const [isSyncing, setIsSyncing] = useState(false);
|
2023-04-03 18:00:29 +00:00
|
|
|
const [createBlockForm, setCreateBlockForm] = useState(false);
|
2023-04-04 08:37:17 +00:00
|
|
|
const [iAmFeelingLucky, setIAmFeelingLucky] = useState(false);
|
2023-03-25 18:09:46 +00:00
|
|
|
|
2023-03-26 06:06:10 +00:00
|
|
|
const [gptAssistantModal, setGptAssistantModal] = useState(false);
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId, pageId } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2023-04-04 10:51:46 +00:00
|
|
|
const { handleSubmit, watch, reset, setValue, register } = useForm<IPageBlock>({
|
2023-03-25 18:09:46 +00:00
|
|
|
defaultValues: {
|
|
|
|
name: "",
|
|
|
|
description: {},
|
|
|
|
description_html: "<p></p>",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const updatePageBlock = async (formData: Partial<IPageBlock>) => {
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return;
|
|
|
|
|
|
|
|
if (!formData.name || formData.name.length === 0 || formData.name === "") return;
|
|
|
|
|
2023-03-27 19:06:20 +00:00
|
|
|
if (block.issue && block.sync) setIsSyncing(true);
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
mutate<IPageBlock[]>(
|
|
|
|
PAGE_BLOCKS_LIST(pageId as string),
|
|
|
|
(prevData) =>
|
|
|
|
prevData?.map((p) => {
|
|
|
|
if (p.id === block.id) return { ...p, ...formData };
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-03-26 06:06:10 +00:00
|
|
|
await pagesService
|
|
|
|
.patchPageBlock(workspaceSlug as string, projectId as string, pageId as string, block.id, {
|
2023-03-25 18:09:46 +00:00
|
|
|
name: formData.name,
|
|
|
|
description: formData.description,
|
|
|
|
description_html: formData.description_html,
|
2023-03-26 06:06:10 +00:00
|
|
|
})
|
2023-03-27 19:06:20 +00:00
|
|
|
.then((res) => {
|
2023-03-26 06:06:10 +00:00
|
|
|
mutate(PAGE_BLOCKS_LIST(pageId as string));
|
2023-03-27 19:06:20 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
.finally(() => setIsSyncing(false));
|
2023-03-26 06:06:10 +00:00
|
|
|
});
|
2023-03-25 18:09:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const pushBlockIntoIssues = async () => {
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return;
|
|
|
|
|
|
|
|
await pagesService
|
|
|
|
.convertPageBlockToIssue(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
pageId as string,
|
|
|
|
block.id
|
|
|
|
)
|
2023-03-27 19:06:20 +00:00
|
|
|
.then((res: IIssue) => {
|
2023-03-25 18:09:46 +00:00
|
|
|
mutate<IPageBlock[]>(
|
|
|
|
PAGE_BLOCKS_LIST(pageId as string),
|
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((p) => {
|
2023-03-27 19:06:20 +00:00
|
|
|
if (p.id === block.id) return { ...p, issue: res.id, issue_detail: res };
|
2023-03-25 18:09:46 +00:00
|
|
|
|
|
|
|
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<IPageBlock[]>(
|
|
|
|
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)
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be deleted. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-11 12:48:49 +00:00
|
|
|
const handleAutoGenerateDescription = async () => {
|
2023-04-04 08:37:17 +00:00
|
|
|
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 in context of a project management software.",
|
|
|
|
})
|
|
|
|
.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));
|
|
|
|
};
|
|
|
|
|
2023-03-26 06:06:10 +00:00
|
|
|
const handleAiAssistance = async (response: string) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
2023-03-28 18:50:00 +00:00
|
|
|
setValue("description", {});
|
|
|
|
setValue("description_html", `${watch("description_html")}<p>${response}</p>`);
|
2023-03-26 06:06:10 +00:00
|
|
|
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.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-27 19:06:20 +00:00
|
|
|
const handleBlockSync = () => {
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return;
|
|
|
|
|
|
|
|
mutate<IPageBlock[]>(
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
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]);
|
|
|
|
|
|
|
|
return (
|
2023-04-04 10:51:46 +00:00
|
|
|
<Draggable draggableId={block.id} index={index} isDragDisabled={createBlockForm}>
|
2023-04-03 18:00:29 +00:00
|
|
|
{(provided, snapshot) => (
|
|
|
|
<>
|
|
|
|
{createBlockForm ? (
|
2023-04-04 10:51:46 +00:00
|
|
|
<div
|
|
|
|
className="mb-4 pt-4"
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
>
|
2023-04-03 18:00:29 +00:00
|
|
|
<CreateUpdateBlockInline
|
2023-04-11 12:48:49 +00:00
|
|
|
setGptAssistantModal={() => setGptAssistantModal((prev) => !prev)}
|
2023-04-03 18:00:29 +00:00
|
|
|
handleClose={() => setCreateBlockForm(false)}
|
|
|
|
data={block}
|
|
|
|
setIsSyncing={setIsSyncing}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div
|
2023-04-11 12:48:49 +00:00
|
|
|
className={`group relative ${
|
2023-04-04 10:51:46 +00:00
|
|
|
snapshot.isDragging ? "border-2 bg-white border-theme shadow-lg rounded-md p-6" : ""
|
2023-04-03 18:00:29 +00:00
|
|
|
}`}
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.draggableProps}
|
|
|
|
>
|
2023-04-04 10:51:46 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
2023-04-11 12:48:49 +00:00
|
|
|
className="absolute top-4 -left-4 p-0.5 hover:bg-gray-100 rounded hidden group-hover:!flex"
|
2023-04-04 10:51:46 +00:00
|
|
|
{...provided.dragHandleProps}
|
|
|
|
>
|
|
|
|
<EllipsisVerticalIcon className="h-[18px]" />
|
|
|
|
<EllipsisVerticalIcon className="h-[18px] -ml-3" />
|
|
|
|
</button>
|
2023-04-11 12:48:49 +00:00
|
|
|
<div className="absolute top-4 right-0 items-center gap-2 hidden group-hover:!flex bg-white pl-4">
|
2023-04-04 10:51:46 +00:00
|
|
|
{block.issue && block.sync && (
|
|
|
|
<div className="flex flex-shrink-0 cursor-default items-center gap-1 rounded bg-gray-100 py-1 px-1.5 text-xs">
|
|
|
|
{isSyncing ? (
|
|
|
|
<ArrowPathIcon className="h-3 w-3 animate-spin" />
|
2023-04-04 08:37:17 +00:00
|
|
|
) : (
|
2023-04-04 10:51:46 +00:00
|
|
|
<CheckIcon className="h-3 w-3" />
|
2023-04-04 08:37:17 +00:00
|
|
|
)}
|
2023-04-04 10:51:46 +00:00
|
|
|
{isSyncing ? "Syncing..." : "Synced"}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={`flex items-center gap-1 rounded px-1.5 py-1 text-xs hover:bg-gray-100 ${
|
|
|
|
iAmFeelingLucky ? "cursor-wait bg-gray-100" : ""
|
|
|
|
}`}
|
2023-04-11 12:48:49 +00:00
|
|
|
onClick={handleAutoGenerateDescription}
|
2023-04-04 10:51:46 +00:00
|
|
|
disabled={iAmFeelingLucky}
|
|
|
|
>
|
|
|
|
{iAmFeelingLucky ? (
|
|
|
|
"Generating response..."
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<SparklesIcon className="h-4 w-4" />I{"'"}m feeling lucky
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="-mr-2 flex items-center gap-1 rounded px-1.5 py-1 text-xs hover:bg-gray-100"
|
|
|
|
onClick={() => setGptAssistantModal((prevData) => !prevData)}
|
|
|
|
>
|
|
|
|
<SparklesIcon className="h-4 w-4" />
|
|
|
|
AI
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="-mr-2 flex items-center gap-1 rounded px-1.5 py-1 text-xs hover:bg-gray-100"
|
|
|
|
onClick={() => setCreateBlockForm(true)}
|
|
|
|
>
|
|
|
|
<PencilIcon className="h-3.5 w-3.5" />
|
|
|
|
</button>
|
|
|
|
<CustomMenu label={<BoltIcon className="h-4.5 w-3.5" />} noBorder noChevron>
|
|
|
|
{block.issue ? (
|
|
|
|
<>
|
|
|
|
<CustomMenu.MenuItem onClick={handleBlockSync}>
|
2023-04-11 12:48:49 +00:00
|
|
|
<span className="flex items-center gap-1">
|
|
|
|
<ArrowPathIcon className="h-4 w-4" />
|
|
|
|
<span>Turn sync {block.sync ? "off" : "on"}</span>
|
|
|
|
</span>
|
2023-04-03 18:00:29 +00:00
|
|
|
</CustomMenu.MenuItem>
|
2023-04-04 10:51:46 +00:00
|
|
|
<CustomMenu.MenuItem onClick={handleCopyText}>
|
2023-04-11 12:48:49 +00:00
|
|
|
<span className="flex items-center gap-1">
|
|
|
|
<LinkIcon className="h-4 w-4" />
|
|
|
|
Copy issue link
|
|
|
|
</span>
|
2023-04-04 10:51:46 +00:00
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<CustomMenu.MenuItem onClick={pushBlockIntoIssues}>
|
2023-04-11 12:48:49 +00:00
|
|
|
<span className="flex items-center gap-1">
|
|
|
|
<LayerDiagonalIcon className="h-4 w-4" />
|
|
|
|
Push into issues
|
|
|
|
</span>
|
2023-04-03 18:00:29 +00:00
|
|
|
</CustomMenu.MenuItem>
|
2023-04-04 10:51:46 +00:00
|
|
|
)}
|
2023-04-11 12:48:49 +00:00
|
|
|
<CustomMenu.MenuItem onClick={deletePageBlock}>
|
|
|
|
<span className="flex items-center gap-1">
|
|
|
|
<TrashIcon className="h-4 w-4" />
|
|
|
|
Delete block
|
|
|
|
</span>
|
|
|
|
</CustomMenu.MenuItem>
|
2023-04-04 10:51:46 +00:00
|
|
|
</CustomMenu>
|
2023-04-03 18:00:29 +00:00
|
|
|
</div>
|
2023-04-04 10:51:46 +00:00
|
|
|
<div
|
|
|
|
className={`flex items-start gap-2 ${
|
|
|
|
snapshot.isDragging ? "" : "py-4 [&:not(:last-child)]:border-b"
|
|
|
|
}`}
|
|
|
|
>
|
2023-04-11 12:48:49 +00:00
|
|
|
<div
|
|
|
|
className="px-4 w-full overflow-hidden break-all cursor-pointer"
|
2023-04-04 10:51:46 +00:00
|
|
|
onClick={() => setCreateBlockForm(true)}
|
|
|
|
>
|
2023-04-11 12:48:49 +00:00
|
|
|
<div className="flex">
|
|
|
|
{block.issue && (
|
|
|
|
<div className="flex mr-1.5">
|
|
|
|
<Link
|
|
|
|
href={`/${workspaceSlug}/projects/${projectId}/issues/${block.issue}`}
|
|
|
|
>
|
|
|
|
<a className="flex flex-shrink-0 items-center gap-1 rounded h-6 bg-gray-100 px-1.5 py-1 text-xs">
|
|
|
|
<LayerDiagonalIcon height="16" width="16" color="black" />
|
|
|
|
{projectDetails?.identifier}-{block.issue_detail?.sequence_id}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<h3 className="font-medium text-base overflow-hidden max-w-[1000px]">
|
|
|
|
{block.name}
|
|
|
|
</h3>
|
|
|
|
</div>
|
|
|
|
{block?.description_stripped.length > 0 && (
|
|
|
|
<p className="mt-3 text-sm text-gray-500 font-normal h-5 truncate">
|
|
|
|
{block.description_stripped}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-04-03 18:00:29 +00:00
|
|
|
</div>
|
2023-04-04 10:51:46 +00:00
|
|
|
<GptAssistantModal
|
|
|
|
block={block}
|
|
|
|
isOpen={gptAssistantModal}
|
|
|
|
handleClose={() => setGptAssistantModal(false)}
|
|
|
|
inset="top-8 left-0"
|
|
|
|
content={block.description_stripped}
|
|
|
|
htmlContent={block.description_html}
|
|
|
|
onResponse={handleAiAssistance}
|
|
|
|
projectId={projectId as string}
|
|
|
|
/>
|
2023-03-27 19:06:20 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2023-04-03 18:00:29 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Draggable>
|
2023-03-25 18:09:46 +00:00
|
|
|
);
|
|
|
|
};
|