2023-04-11 12:48:49 +00:00
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import dynamic from "next/dynamic";
|
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
import { mutate } from "swr";
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
import { SparklesIcon } from "@heroicons/react/24/outline";
|
2023-04-10 07:03:12 +00:00
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
// react-hook-form
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
// services
|
|
|
|
import pagesService from "services/pages.service";
|
2023-04-11 17:49:47 +00:00
|
|
|
import issuesService from "services/issues.service";
|
|
|
|
import aiService from "services/ai.service";
|
2023-04-03 18:00:29 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-05-05 10:15:10 +00:00
|
|
|
// components
|
|
|
|
import { GptAssistantModal } from "components/core";
|
2023-04-03 18:00:29 +00:00
|
|
|
// ui
|
2023-05-05 10:15:10 +00:00
|
|
|
import { Input, Loader, PrimaryButton, SecondaryButton } from "components/ui";
|
2023-04-03 18:00:29 +00:00
|
|
|
// types
|
|
|
|
import { IPageBlock } from "types";
|
|
|
|
// fetch-keys
|
|
|
|
import { PAGE_BLOCKS_LIST } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
handleClose: () => void;
|
|
|
|
data?: IPageBlock;
|
2023-05-05 10:15:10 +00:00
|
|
|
handleAiAssistance?: (response: string) => void;
|
2023-04-03 18:00:29 +00:00
|
|
|
setIsSyncing?: React.Dispatch<React.SetStateAction<boolean>>;
|
2023-04-04 10:51:46 +00:00
|
|
|
focus?: keyof IPageBlock;
|
2023-04-03 18:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues = {
|
|
|
|
name: "",
|
2023-04-17 09:33:44 +00:00
|
|
|
description: { type: "doc", content: [] },
|
|
|
|
description_html: "<p></p>",
|
2023-04-03 18:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor"), {
|
|
|
|
ssr: false,
|
|
|
|
loading: () => (
|
|
|
|
<Loader className="mx-4 mt-6">
|
|
|
|
<Loader.Item height="100px" width="100%" />
|
|
|
|
</Loader>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2023-04-04 10:51:46 +00:00
|
|
|
export const CreateUpdateBlockInline: React.FC<Props> = ({
|
|
|
|
handleClose,
|
|
|
|
data,
|
2023-05-05 10:15:10 +00:00
|
|
|
handleAiAssistance,
|
2023-04-04 10:51:46 +00:00
|
|
|
setIsSyncing,
|
|
|
|
focus,
|
|
|
|
}) => {
|
2023-04-11 12:48:49 +00:00
|
|
|
const [iAmFeelingLucky, setIAmFeelingLucky] = useState(false);
|
2023-05-05 10:15:10 +00:00
|
|
|
const [gptAssistantModal, setGptAssistantModal] = useState(false);
|
2023-04-11 12:48:49 +00:00
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId, pageId } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
register,
|
|
|
|
control,
|
|
|
|
watch,
|
|
|
|
setValue,
|
2023-04-04 10:51:46 +00:00
|
|
|
setFocus,
|
2023-04-03 18:00:29 +00:00
|
|
|
reset,
|
|
|
|
formState: { isSubmitting },
|
|
|
|
} = useForm<IPageBlock>({
|
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
const onClose = useCallback(() => {
|
2023-04-04 10:51:46 +00:00
|
|
|
if (data) handleClose();
|
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
reset();
|
2023-04-04 10:51:46 +00:00
|
|
|
}, [handleClose, reset, data]);
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
const createPageBlock = useCallback(
|
|
|
|
async (formData: Partial<IPageBlock>) => {
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return;
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
await pagesService
|
|
|
|
.createPageBlock(workspaceSlug as string, projectId as string, pageId as string, {
|
|
|
|
name: formData.name,
|
|
|
|
description: formData.description ?? "",
|
|
|
|
description_html: formData.description_html ?? "<p></p>",
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
mutate<IPageBlock[]>(
|
|
|
|
PAGE_BLOCKS_LIST(pageId as string),
|
|
|
|
(prevData) => [...(prevData as IPageBlock[]), res],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be created. Please try again.",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(() => onClose());
|
|
|
|
},
|
|
|
|
[workspaceSlug, projectId, pageId, onClose, setToastAlert]
|
|
|
|
);
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
const updatePageBlock = useCallback(
|
|
|
|
async (formData: Partial<IPageBlock>) => {
|
|
|
|
if (!workspaceSlug || !projectId || !pageId || !data) return;
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
if (data.issue && data.sync && setIsSyncing) setIsSyncing(true);
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
mutate<IPageBlock[]>(
|
|
|
|
PAGE_BLOCKS_LIST(pageId as string),
|
|
|
|
(prevData) =>
|
|
|
|
prevData?.map((p) => {
|
|
|
|
if (p.id === data.id) return { ...p, ...formData };
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
await pagesService
|
|
|
|
.patchPageBlock(workspaceSlug as string, projectId as string, pageId as string, data.id, {
|
|
|
|
name: formData.name,
|
|
|
|
description: formData.description,
|
|
|
|
description_html: formData.description_html,
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
mutate(PAGE_BLOCKS_LIST(pageId as string));
|
|
|
|
if (data.issue && data.sync)
|
|
|
|
issuesService
|
|
|
|
.patchIssue(workspaceSlug as string, projectId as string, data.issue, {
|
|
|
|
name: res.name,
|
|
|
|
description: res.description,
|
|
|
|
description_html: res.description_html,
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
if (setIsSyncing) setIsSyncing(false);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(() => onClose());
|
|
|
|
},
|
|
|
|
[workspaceSlug, projectId, pageId, data, onClose, setIsSyncing]
|
|
|
|
);
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 12:48:49 +00:00
|
|
|
const handleAutoGenerateDescription = async () => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
setIAmFeelingLucky(true);
|
|
|
|
|
|
|
|
aiService
|
|
|
|
.createGptTask(workspaceSlug as string, projectId as string, {
|
|
|
|
prompt: watch("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 {
|
|
|
|
setValue("description", {});
|
2023-04-17 11:51:15 +00:00
|
|
|
setValue("description_html", `${watch("description_html") ?? ""}<p>${res.response}</p>`);
|
2023-04-11 12:48:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.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.",
|
|
|
|
});
|
2023-04-11 17:49:47 +00:00
|
|
|
})
|
|
|
|
.finally(() => setIAmFeelingLucky(false));
|
2023-04-11 12:48:49 +00:00
|
|
|
};
|
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
useEffect(() => {
|
2023-04-04 10:51:46 +00:00
|
|
|
if (focus) setFocus(focus);
|
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
|
|
|
description_html: data.description_html,
|
|
|
|
});
|
2023-04-04 10:51:46 +00:00
|
|
|
}, [reset, data, focus, setFocus]);
|
2023-04-03 18:00:29 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener("keydown", (e: KeyboardEvent) => {
|
2023-04-04 13:01:28 +00:00
|
|
|
if (e.key === "Escape") handleClose();
|
2023-04-03 18:00:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("keydown", (e: KeyboardEvent) => {
|
2023-04-04 13:01:28 +00:00
|
|
|
if (e.key === "Escape") handleClose();
|
2023-04-03 18:00:29 +00:00
|
|
|
});
|
|
|
|
};
|
2023-04-04 13:01:28 +00:00
|
|
|
}, [handleClose]);
|
2023-04-03 18:00:29 +00:00
|
|
|
|
2023-04-11 17:49:47 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const submitForm = (e: KeyboardEvent) => {
|
|
|
|
if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {
|
|
|
|
if (data) handleSubmit(updatePageBlock)();
|
|
|
|
else handleSubmit(createPageBlock)();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener("keydown", submitForm);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("keydown", submitForm);
|
|
|
|
};
|
|
|
|
}, [createPageBlock, updatePageBlock, data, handleSubmit]);
|
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
return (
|
2023-05-05 10:15:10 +00:00
|
|
|
<div className="relative">
|
|
|
|
<form
|
|
|
|
className="divide-y divide-brand-base rounded-[10px] border border-brand-base shadow"
|
|
|
|
onSubmit={data ? handleSubmit(updatePageBlock) : handleSubmit(createPageBlock)}
|
|
|
|
>
|
|
|
|
<div className="pt-2">
|
|
|
|
<div className="flex justify-between">
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
placeholder="Title"
|
|
|
|
register={register}
|
|
|
|
className="min-h-10 block w-full resize-none overflow-hidden border-none bg-transparent py-1 text-lg font-medium"
|
|
|
|
autoComplete="off"
|
|
|
|
maxLength={255}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="page-block-section relative -mt-2 text-brand-secondary">
|
|
|
|
<Controller
|
|
|
|
name="description"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<RemirrorRichTextEditor
|
|
|
|
value={
|
|
|
|
!value || (typeof value === "object" && Object.keys(value).length === 0)
|
|
|
|
? watch("description_html")
|
|
|
|
: value
|
|
|
|
}
|
|
|
|
onJSONChange={(jsonValue) => setValue("description", jsonValue)}
|
|
|
|
onHTMLChange={(htmlValue) => setValue("description_html", htmlValue)}
|
|
|
|
placeholder="Write something..."
|
|
|
|
customClassName="text-sm"
|
|
|
|
noBorder
|
|
|
|
borderOnFocus={false}
|
|
|
|
/>
|
2023-04-11 12:48:49 +00:00
|
|
|
)}
|
2023-05-05 10:15:10 +00:00
|
|
|
/>
|
|
|
|
<div className="m-2 mt-6 flex">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={`flex items-center gap-1 rounded px-1.5 py-1 text-xs hover:bg-brand-surface-1 ${
|
|
|
|
iAmFeelingLucky ? "cursor-wait bg-brand-surface-1" : ""
|
|
|
|
}`}
|
|
|
|
onClick={handleAutoGenerateDescription}
|
|
|
|
disabled={iAmFeelingLucky}
|
|
|
|
>
|
|
|
|
{iAmFeelingLucky ? (
|
|
|
|
"Generating response..."
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<SparklesIcon className="h-4 w-4" />I{"'"}m feeling lucky
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
|
2023-04-10 07:03:12 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
2023-04-20 20:45:21 +00:00
|
|
|
className="ml-2 flex items-center gap-1 rounded px-1.5 py-1 text-xs hover:bg-brand-surface-1"
|
2023-05-05 10:15:10 +00:00
|
|
|
onClick={() => setGptAssistantModal(true)}
|
2023-04-10 07:03:12 +00:00
|
|
|
>
|
2023-04-20 20:45:21 +00:00
|
|
|
<SparklesIcon className="h-4 w-4" />
|
|
|
|
AI
|
2023-04-10 07:03:12 +00:00
|
|
|
</button>
|
2023-05-05 10:15:10 +00:00
|
|
|
</div>
|
2023-04-11 12:48:49 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-05-05 10:15:10 +00:00
|
|
|
<div className="flex items-center justify-end gap-2 p-4">
|
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
|
|
|
<PrimaryButton type="submit" disabled={watch("name") === ""} loading={isSubmitting}>
|
|
|
|
{data
|
|
|
|
? isSubmitting
|
|
|
|
? "Updating..."
|
|
|
|
: "Update block"
|
|
|
|
: isSubmitting
|
|
|
|
? "Adding..."
|
|
|
|
: "Add block"}
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<GptAssistantModal
|
|
|
|
block={data ? data : undefined}
|
|
|
|
isOpen={gptAssistantModal}
|
|
|
|
handleClose={() => setGptAssistantModal(false)}
|
|
|
|
inset="top-8 left-0"
|
|
|
|
content={watch("description_html")}
|
|
|
|
htmlContent={watch("description_html")}
|
|
|
|
onResponse={(response) => {
|
|
|
|
if (data && handleAiAssistance) handleAiAssistance(response);
|
|
|
|
else {
|
|
|
|
setValue("description", {});
|
|
|
|
setValue("description_html", `${watch("description_html")}<p>${response}</p>`);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
projectId={projectId?.toString() ?? ""}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-04-03 18:00:29 +00:00
|
|
|
);
|
|
|
|
};
|