Skip to content

Commit 458247c

Browse files
authored
Merge pull request #1868 from mfts/feat/annotations
feat: add annotations
2 parents aefe99a + 290db03 commit 458247c

29 files changed

Lines changed: 3621 additions & 515 deletions

File tree

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
"use client";
2+
3+
import { useEffect, useState } from "react";
4+
5+
import { zodResolver } from "@hookform/resolvers/zod";
6+
import { useForm } from "react-hook-form";
7+
import { toast } from "sonner";
8+
import { z } from "zod";
9+
10+
import { uploadImage } from "@/lib/utils";
11+
12+
import { Button } from "@/components/ui/button";
13+
import { Checkbox } from "@/components/ui/checkbox";
14+
import {
15+
Form,
16+
FormControl,
17+
FormDescription,
18+
FormField,
19+
FormItem,
20+
FormLabel,
21+
FormMessage,
22+
} from "@/components/ui/form-hook";
23+
import { Input } from "@/components/ui/input";
24+
import LoadingSpinner from "@/components/ui/loading-spinner";
25+
import { RichTextEditor } from "@/components/ui/rich-text-editor";
26+
import { Switch } from "@/components/ui/switch";
27+
28+
const formSchema = z.object({
29+
title: z
30+
.string()
31+
.min(1, "Title is required")
32+
.max(100, "Title must be less than 100 characters"),
33+
content: z.any().optional(),
34+
pages: z.array(z.number()).min(1, "At least one page must be selected"),
35+
isVisible: z.boolean(),
36+
});
37+
38+
type FormValues = z.infer<typeof formSchema>;
39+
40+
interface AnnotationFormProps {
41+
documentId: string;
42+
teamId: string;
43+
numPages: number;
44+
annotation?: any;
45+
onSuccess: () => void;
46+
}
47+
48+
export function AnnotationForm({
49+
documentId,
50+
teamId,
51+
numPages,
52+
annotation,
53+
onSuccess,
54+
}: AnnotationFormProps) {
55+
const [isLoading, setIsLoading] = useState(false);
56+
const [editorContent, setEditorContent] = useState(
57+
annotation?.content || { type: "doc", content: [] },
58+
);
59+
60+
const form = useForm<FormValues>({
61+
resolver: zodResolver(formSchema),
62+
defaultValues: {
63+
title: annotation?.title || "",
64+
content: annotation?.content || null,
65+
pages: annotation?.pages || [],
66+
isVisible:
67+
annotation?.isVisible !== undefined ? annotation.isVisible : true,
68+
},
69+
});
70+
71+
const pageOptions = Array.from({ length: numPages }, (_, i) => i + 1);
72+
73+
const handleImageUpload = async (file: File): Promise<string> => {
74+
try {
75+
// Upload the image using the existing uploadImage utility
76+
const imageUrl = await uploadImage(file, "assets");
77+
78+
// Don't save to database here - let the form submission handle it
79+
// Images will be embedded in the rich text content and parsed when saving
80+
81+
return imageUrl;
82+
} catch (error) {
83+
console.error("Failed to upload image:", error);
84+
throw new Error("Failed to upload image");
85+
}
86+
};
87+
88+
const onSubmit = async (values: FormValues) => {
89+
setIsLoading(true);
90+
try {
91+
const url = annotation
92+
? `/api/teams/${teamId}/documents/${documentId}/annotations/${annotation.id}`
93+
: `/api/teams/${teamId}/documents/${documentId}/annotations`;
94+
95+
const method = annotation ? "PUT" : "POST";
96+
97+
const response = await fetch(url, {
98+
method,
99+
headers: {
100+
"Content-Type": "application/json",
101+
},
102+
body: JSON.stringify({
103+
...values,
104+
content: editorContent,
105+
}),
106+
});
107+
108+
if (!response.ok) {
109+
const errorData = await response.json();
110+
throw new Error(errorData.error || "Failed to save annotation");
111+
}
112+
113+
toast.success(
114+
annotation
115+
? "Annotation updated successfully"
116+
: "Annotation created successfully",
117+
);
118+
onSuccess();
119+
} catch (error) {
120+
console.error("Error saving annotation:", error);
121+
toast.error(
122+
error instanceof Error ? error.message : "Failed to save annotation",
123+
);
124+
} finally {
125+
setIsLoading(false);
126+
}
127+
};
128+
129+
return (
130+
<Form {...form}>
131+
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
132+
<FormField
133+
control={form.control}
134+
name="title"
135+
render={({ field }) => (
136+
<FormItem>
137+
<FormLabel>Title</FormLabel>
138+
<FormControl>
139+
<Input placeholder="Enter annotation title" {...field} />
140+
</FormControl>
141+
<FormDescription>
142+
A brief title for this annotation that viewers will see.
143+
</FormDescription>
144+
<FormMessage />
145+
</FormItem>
146+
)}
147+
/>
148+
149+
<FormField
150+
control={form.control}
151+
name="pages"
152+
render={({ field }) => (
153+
<FormItem>
154+
<FormLabel>Pages</FormLabel>
155+
<FormDescription>
156+
Select which pages this annotation should appear on.
157+
</FormDescription>
158+
<div className="mt-2 grid grid-cols-10 gap-2">
159+
{pageOptions.map((page) => (
160+
<div key={page} className="flex items-center space-x-2">
161+
<Checkbox
162+
id={`page-${page}`}
163+
checked={field.value.includes(page)}
164+
onCheckedChange={(checked) => {
165+
if (checked) {
166+
field.onChange([...field.value, page]);
167+
} else {
168+
field.onChange(field.value.filter((p) => p !== page));
169+
}
170+
}}
171+
/>
172+
<label
173+
htmlFor={`page-${page}`}
174+
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
175+
>
176+
{page}
177+
</label>
178+
</div>
179+
))}
180+
</div>
181+
<FormMessage />
182+
</FormItem>
183+
)}
184+
/>
185+
186+
<div className="space-y-2">
187+
<FormLabel>Content</FormLabel>
188+
<FormDescription>
189+
Add rich text content and images to help explain this part of the
190+
document.
191+
</FormDescription>
192+
<RichTextEditor
193+
content={editorContent}
194+
onChange={setEditorContent}
195+
placeholder="Add your annotation content here..."
196+
onImageUpload={handleImageUpload}
197+
/>
198+
</div>
199+
200+
<FormField
201+
control={form.control}
202+
name="isVisible"
203+
render={({ field }) => (
204+
<FormItem className="flex flex-row items-center justify-between rounded-lg border p-4">
205+
<div className="space-y-0.5">
206+
<FormLabel className="text-base">Visible to viewers</FormLabel>
207+
<FormDescription>
208+
When enabled, viewers will be able to see this annotation when
209+
viewing the document.
210+
</FormDescription>
211+
</div>
212+
<FormControl>
213+
<Switch
214+
checked={field.value}
215+
onCheckedChange={field.onChange}
216+
/>
217+
</FormControl>
218+
</FormItem>
219+
)}
220+
/>
221+
222+
<div className="flex justify-end space-x-2">
223+
<Button type="submit" disabled={isLoading}>
224+
{isLoading && <LoadingSpinner className="mr-2 h-4 w-4" />}
225+
{annotation ? "Update Annotation" : "Create Annotation"}
226+
</Button>
227+
</div>
228+
</form>
229+
</Form>
230+
);
231+
}

0 commit comments

Comments
 (0)