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";
|
2023-04-11 17:49:47 +00:00
|
|
|
import { mutate } from "swr";
|
|
|
|
import { SparklesIcon } from "@heroicons/react/24/outline";
|
2023-04-03 18:00:29 +00:00
|
|
|
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-08-28 10:24:49 +00:00
|
|
|
import { TipTapEditor } from "components/tiptap";
|
2023-08-15 09:34:46 +00:00
|
|
|
import { PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
2023-04-03 18:00:29 +00:00
|
|
|
// types
|
2023-06-06 16:06:00 +00:00
|
|
|
import { ICurrentUserResponse, IPageBlock } from "types";
|
2023-04-03 18:00:29 +00:00
|
|
|
// 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-06-06 16:06:00 +00:00
|
|
|
user: ICurrentUserResponse | undefined;
|
2023-04-03 18:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues = {
|
|
|
|
name: "",
|
2023-05-10 20:45:49 +00:00
|
|
|
description: null,
|
|
|
|
description_html: null,
|
2023-04-03 18:00:29 +00:00
|
|
|
};
|
|
|
|
|
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-06-06 16:06:00 +00:00
|
|
|
user,
|
2023-04-04 10:51:46 +00:00
|
|
|
}) => {
|
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-05-10 20:45:49 +00:00
|
|
|
const editorRef = React.useRef<any>(null);
|
|
|
|
|
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
|
2023-06-06 16:06:00 +00:00
|
|
|
.createPageBlock(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
pageId as string,
|
|
|
|
{
|
|
|
|
name: formData.name,
|
|
|
|
description: formData.description ?? "",
|
|
|
|
description_html: formData.description_html ?? "<p></p>",
|
|
|
|
},
|
|
|
|
user
|
|
|
|
)
|
2023-04-11 17:49:47 +00:00
|
|
|
.then((res) => {
|
|
|
|
mutate<IPageBlock[]>(
|
|
|
|
PAGE_BLOCKS_LIST(pageId as string),
|
|
|
|
(prevData) => [...(prevData as IPageBlock[]), res],
|
|
|
|
false
|
|
|
|
);
|
2023-05-10 20:45:49 +00:00
|
|
|
editorRef.current?.clearEditor();
|
2023-04-11 17:49:47 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be created. Please try again.",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(() => onClose());
|
|
|
|
},
|
2023-06-16 13:27:17 +00:00
|
|
|
[workspaceSlug, projectId, pageId, onClose, setToastAlert, user]
|
2023-04-11 17:49:47 +00:00
|
|
|
);
|
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
|
2023-06-06 16:06:00 +00:00
|
|
|
.patchPageBlock(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
pageId as string,
|
|
|
|
data.id,
|
|
|
|
{
|
|
|
|
name: formData.name,
|
|
|
|
description: formData.description,
|
|
|
|
description_html: formData.description_html,
|
|
|
|
},
|
|
|
|
user
|
|
|
|
)
|
2023-04-11 17:49:47 +00:00
|
|
|
.then((res) => {
|
|
|
|
mutate(PAGE_BLOCKS_LIST(pageId as string));
|
2023-05-19 09:43:33 +00:00
|
|
|
editorRef.current?.setEditorValue(res.description_html);
|
2023-04-11 17:49:47 +00:00
|
|
|
if (data.issue && data.sync)
|
|
|
|
issuesService
|
2023-06-06 16:06:00 +00:00
|
|
|
.patchIssue(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
data.issue,
|
|
|
|
{
|
|
|
|
name: res.name,
|
|
|
|
description: res.description,
|
|
|
|
description_html: res.description_html,
|
|
|
|
},
|
|
|
|
user
|
|
|
|
)
|
2023-04-11 17:49:47 +00:00
|
|
|
.finally(() => {
|
|
|
|
if (setIsSyncing) setIsSyncing(false);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(() => onClose());
|
|
|
|
},
|
2023-06-16 13:27:17 +00:00
|
|
|
[workspaceSlug, projectId, pageId, data, onClose, setIsSyncing, user]
|
2023-04-11 17:49:47 +00:00
|
|
|
);
|
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
|
2023-06-06 16:06:00 +00:00
|
|
|
.createGptTask(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
{
|
|
|
|
prompt: watch("name"),
|
2023-06-23 13:59:36 +00:00
|
|
|
task: "Generate a proper description for this issue.",
|
2023-06-06 16:06:00 +00:00
|
|
|
},
|
|
|
|
user
|
|
|
|
)
|
2023-04-11 12:48:49 +00:00
|
|
|
.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-05-19 09:43:33 +00:00
|
|
|
editorRef.current?.setEditorValue(watch("description_html") ?? "");
|
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,
|
2023-05-10 20:45:49 +00:00
|
|
|
description:
|
|
|
|
!data.description || data.description === ""
|
|
|
|
? {
|
2023-08-28 10:24:49 +00:00
|
|
|
type: "doc",
|
|
|
|
content: [{ type: "paragraph" }],
|
|
|
|
}
|
2023-05-10 20:45:49 +00:00
|
|
|
: data.description,
|
|
|
|
description_html: data.description_html ?? "<p></p>",
|
2023-04-03 18:00:29 +00:00
|
|
|
});
|
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
|
2023-07-17 10:58:23 +00:00
|
|
|
className="divide-y divide-custom-border-200 rounded border border-custom-border-200 shadow"
|
2023-05-05 10:15:10 +00:00
|
|
|
onSubmit={data ? handleSubmit(updatePageBlock) : handleSubmit(createPageBlock)}
|
|
|
|
>
|
|
|
|
<div className="pt-2">
|
|
|
|
<div className="flex justify-between">
|
2023-05-11 12:33:37 +00:00
|
|
|
<TextArea
|
2023-05-05 10:15:10 +00:00
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
placeholder="Title"
|
|
|
|
register={register}
|
2023-05-11 12:33:37 +00:00
|
|
|
className="min-h-10 font medium block w-full resize-none overflow-hidden border-none bg-transparent py-1 text-base"
|
2023-05-05 10:15:10 +00:00
|
|
|
autoComplete="off"
|
|
|
|
maxLength={255}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-08-16 12:55:11 +00:00
|
|
|
<div className="page-block-section relative -mt-2 text-custom-text-200">
|
2023-05-05 10:15:10 +00:00
|
|
|
<Controller
|
2023-08-15 09:34:46 +00:00
|
|
|
name="description_html"
|
2023-05-05 10:15:10 +00:00
|
|
|
control={control}
|
2023-08-15 09:34:46 +00:00
|
|
|
render={({ field: { value, onChange } }) => {
|
2023-05-10 20:45:49 +00:00
|
|
|
if (!data)
|
|
|
|
return (
|
2023-08-28 10:24:49 +00:00
|
|
|
<TipTapEditor
|
2023-08-19 13:28:54 +00:00
|
|
|
workspaceSlug={workspaceSlug as string}
|
2023-08-15 09:34:46 +00:00
|
|
|
ref={editorRef}
|
|
|
|
value={"<p></p>"}
|
|
|
|
debouncedUpdatesEnabled={false}
|
2023-05-10 20:45:49 +00:00
|
|
|
customClassName="text-sm"
|
|
|
|
noBorder
|
|
|
|
borderOnFocus={false}
|
2023-08-15 09:34:46 +00:00
|
|
|
onChange={(description: Object, description_html: string) => {
|
|
|
|
onChange(description_html);
|
|
|
|
setValue("description", description);
|
|
|
|
}}
|
2023-05-10 20:45:49 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
else if (!value || !watch("description_html"))
|
|
|
|
return (
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="h-32 w-full flex items-center justify-center text-custom-text-200 text-sm" />
|
2023-05-10 20:45:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2023-08-28 10:24:49 +00:00
|
|
|
<TipTapEditor
|
2023-08-19 13:28:54 +00:00
|
|
|
workspaceSlug={workspaceSlug as string}
|
2023-08-15 09:34:46 +00:00
|
|
|
ref={editorRef}
|
2023-05-10 20:45:49 +00:00
|
|
|
value={
|
|
|
|
value && value !== "" && Object.keys(value).length > 0
|
|
|
|
? value
|
|
|
|
: { type: "doc", content: [{ type: "paragraph" }] }
|
|
|
|
}
|
2023-08-15 09:34:46 +00:00
|
|
|
debouncedUpdatesEnabled={false}
|
2023-05-10 20:45:49 +00:00
|
|
|
customClassName="text-sm"
|
|
|
|
noBorder
|
|
|
|
borderOnFocus={false}
|
2023-08-15 09:34:46 +00:00
|
|
|
onChange={(description: Object, description_html: string) => {
|
|
|
|
onChange(description_html);
|
|
|
|
setValue("description", description);
|
|
|
|
}}
|
2023-05-10 20:45:49 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2023-05-05 10:15:10 +00:00
|
|
|
/>
|
|
|
|
<div className="m-2 mt-6 flex">
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-08-28 10:24:49 +00:00
|
|
|
className={`flex items-center gap-1 rounded px-1.5 py-1 text-xs hover:bg-custom-background-80 ${
|
|
|
|
iAmFeelingLucky ? "cursor-wait bg-custom-background-90" : ""
|
|
|
|
}`}
|
2023-05-05 10:15:10 +00:00
|
|
|
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-07-10 07:17:00 +00:00
|
|
|
className="ml-2 flex items-center gap-1 rounded px-1.5 py-1 text-xs hover:bg-custom-background-80"
|
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
|
2023-08-28 10:24:49 +00:00
|
|
|
? "Adding..."
|
|
|
|
: "Add block"}
|
2023-05-05 10:15:10 +00:00
|
|
|
</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) => {
|
2023-05-19 15:03:35 +00:00
|
|
|
if (data && handleAiAssistance) {
|
|
|
|
handleAiAssistance(response);
|
|
|
|
editorRef.current?.setEditorValue(
|
|
|
|
`${watch("description_html")}<p>${response}</p>` ?? ""
|
|
|
|
);
|
|
|
|
} else {
|
2023-05-05 10:15:10 +00:00
|
|
|
setValue("description", {});
|
|
|
|
setValue("description_html", `${watch("description_html")}<p>${response}</p>`);
|
2023-05-19 15:03:35 +00:00
|
|
|
|
2023-05-19 13:42:34 +00:00
|
|
|
editorRef.current?.setEditorValue(watch("description_html") ?? "");
|
2023-05-05 10:15:10 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
projectId={projectId?.toString() ?? ""}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-04-03 18:00:29 +00:00
|
|
|
);
|
|
|
|
};
|