import React, { useState } from "react"; import { useAnnotations } from "@/context/AnnotationsContext"; interface AddAnnotationFormProps { bookId: string; page: number; locationText?: string; onClose?: () => void; } export function AddAnnotationForm({ bookId, page, locationText, onClose, }: AddAnnotationFormProps): React.ReactElement { const { addBookmark, addNote } = useAnnotations(); const [mode, setMode] = useState<"bookmark" | "note" | null>(null); const [noteContent, setNoteContent] = useState(""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const handleSubmit = async (): Promise => { setSubmitting(true); setError(null); try { if (mode === "bookmark") { await addBookmark({ book: bookId, page, location_text: locationText }); } else if (mode === "note") { if (!noteContent.trim()) { setError("Note content cannot be empty."); setSubmitting(false); return; } await addNote({ book: bookId, page, location_text: locationText, content: noteContent.trim(), }); } onClose?.(); } catch (err) { setError( err instanceof Error ? err.message : "Failed to save annotation." ); } finally { setSubmitting(false); } }; return (

Add to Page {page}

{locationText && (
“{locationText}”
)} {!mode ? (
) : (
{mode === "note" && (