import { useEffect, useState } from "react"; import { useRouter } from "next/router"; // react-hook-form import { useForm } from "react-hook-form"; // services import aiService from "services/ai.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Input, PrimaryButton, SecondaryButton } from "components/ui"; type Props = { isOpen: boolean; handleClose: () => void; inset?: string; content: string; onResponse: (response: string) => void; }; type FormData = { prompt: string; task: string; }; export const GptAssistantModal: React.FC = ({ isOpen, handleClose, inset = "top-0 left-0", content, onResponse, }) => { const [response, setResponse] = useState(""); const [invalidResponse, setInvalidResponse] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { handleSubmit, register, reset, setFocus, formState: { isSubmitting }, } = useForm({ defaultValues: { prompt: content, task: "", }, }); const onClose = () => { handleClose(); setResponse(""); setInvalidResponse(false); reset(); }; const handleResponse = async (formData: FormData) => { if (!workspaceSlug || !projectId) return; if (!content || content === "") { setToastAlert({ type: "error", title: "Error!", message: "Please enter some description to get AI assistance.", }); return; } if (formData.task === "") { setToastAlert({ type: "error", title: "Error!", message: "Please enter some task to get AI assistance.", }); return; } await aiService .createGptTask(workspaceSlug as string, projectId as string, { prompt: content, task: formData.task, }) .then((res) => { setResponse(res.response); setFocus("task"); if (res.response === "") setInvalidResponse(true); else setInvalidResponse(false); }); }; useEffect(() => { if (isOpen) setFocus("task"); }, [isOpen, setFocus]); return (
Content:

{content}

{response !== "" && (
Response:

{response}

)} {invalidResponse && (
No response could be generated. This may be due to insufficient content or task information. Please try again.
)}
{response !== "" && ( { onResponse(response); onClose(); }} > Use this response )}
Close {isSubmitting ? "Generating response..." : response === "" ? "Generate response" : "Generate again"}
); };