-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAnswerForm.tsx
More file actions
184 lines (158 loc) · 5.32 KB
/
Copy pathAnswerForm.tsx
File metadata and controls
184 lines (158 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { MDXEditorMethods } from "@mdxeditor/editor";
import { ReloadIcon } from "@radix-ui/react-icons";
import dynamic from "next/dynamic";
import Image from "next/image";
import { useSession } from "next-auth/react";
import { useRef, useState, useTransition } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { toast } from "@/hooks/use-toast";
import { createAnswer } from "@/lib/actions/answer.action";
import { api } from "@/lib/api";
import { AnswerSchema } from "@/lib/validations";
import { Button } from "../ui/button";
import { Form, FormField, FormItem, FormControl, FormMessage } from "../ui/form";
const Editor = dynamic(() => import("@/components/editor"), {
ssr: false,
});
interface Props {
questionId: string;
questionTitle: string;
questionContent: string;
}
const AnswerForm = ({ questionId, questionTitle, questionContent }: Props) => {
const session = useSession();
const [isAnswering, startAnsweringTransition] = useTransition();
const [aiSubmitting, setAiSubmitting] = useState(false);
const editorRef = useRef<MDXEditorMethods>(null);
const form = useForm<z.infer<typeof AnswerSchema>>({
resolver: zodResolver(AnswerSchema),
defaultValues: {
content: "",
},
});
const handleCreateAnswer = async (values: z.infer<typeof AnswerSchema>) => {
startAnsweringTransition(async () => {
const result = await createAnswer({
content: values.content,
questionId,
});
if (result.success) {
toast({
title: "Success",
description: "Your answer has been created successfully.",
});
if (editorRef.current) {
editorRef.current.setMarkdown("");
}
} else {
toast({
title: `Error (${result.status})`,
description: result.error?.message,
variant: "destructive",
});
}
});
};
const generateAIAnswer = async () => {
if (session.status !== "authenticated") {
return toast({
title: "Please log in",
description: "You must log in to generate an AI answer.",
});
}
setAiSubmitting(true);
// store answer written by user for more context to feed to ai
const userAnswer = editorRef.current?.getMarkdown();
try {
const { success, data, error } = await api.ai.getAnswer(questionTitle, questionContent, userAnswer);
if (!success) {
return toast({
title: "Error",
description: `Failed to generate AI answer. Please try again. ${JSON.stringify(error)}`,
variant: "destructive",
});
}
const formattedAnswer = data.replace(/<br>/g, " ").toString().trim();
if (editorRef.current) {
editorRef.current.setMarkdown(formattedAnswer);
// validate content field automatically
form.setValue("content", formattedAnswer);
form.trigger("content");
}
toast({
title: "AI Answer Generated",
description: "The AI has successfully generated an answer.",
});
} catch (error: Error | unknown) {
toast({
title: "Error",
description: error instanceof Error ? error.message : "There was a problem with your request.",
variant: "destructive",
});
} finally {
setAiSubmitting(false);
}
};
return (
<div>
<div className="flex flex-col justify-between gap-5 sm:flex-row sm:items-center sm:gap-2">
<h4 className="paragraph-semibold text-dark400_light800">Write your answer here</h4>
<Button
className="btn light-border-2 text-primary-500 dark:text-primary-500 gap-1.5 rounded-md border px-4 py-2.5 shadow-none"
disabled={aiSubmitting}
onClick={generateAIAnswer}
>
{aiSubmitting ? (
<>
<ReloadIcon className="mr-2 size-4 animate-spin" />
Generating...
</>
) : (
<>
<Image
src="/icons/stars.svg"
alt="Generate AI answer"
width={12}
height={12}
className="object-contain"
/>
Generate AI answer
</>
)}
</Button>
</div>
<Form {...form}>
<form className="mt-6 flex w-full flex-col gap-10" onSubmit={form.handleSubmit(handleCreateAnswer)}>
<FormField
control={form.control}
name="content"
render={({ field }) => (
<FormItem className="flex w-full flex-col gap-3">
<FormControl className="mt-3.5">
<Editor value={field.value} editorRef={editorRef} fieldChange={field.onChange} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-end">
<Button type="submit" className="primary-gradient w-fit" disabled={isAnswering}>
{isAnswering ? (
<>
<ReloadIcon className="mr-2 size-4 animate-spin" />
Posting...
</>
) : (
<>Post Answer</>
)}
</Button>
</div>
</form>
</Form>
</div>
);
};
export default AnswerForm;