'use client' import { useSession } from 'next-auth/react' import React, { useEffect, useRef, useState } from 'react' import ReactMarkdown from 'react-markdown' import remarkGfm from 'remark-gfm' import InfoIcon from './icons/InfoIcon' interface Props { eventId: string initialNotes: string canEdit: boolean } export default function EventNotesEditor({ eventId, initialNotes, canEdit }: Props) { const [notes, setNotes] = useState(initialNotes || ''); const [isEditing, setIsEditing] = useState(false) const [saving, setSaving] = useState(false); const textareaRef = useRef(null) const { data: session } = useSession() const isAuthorized = session?.user?.role === 'COUPLE' || session?.user?.role === 'PLANNER' useEffect(() => { if (isEditing && textareaRef.current) { textareaRef.current.focus() } }, [isEditing]) async function saveNotes() { setSaving(true) try { const res = await fetch(`/api/events/${eventId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ notes }), }) if (!res.ok) throw new Error('Failed to save notes') setIsEditing(false) } catch (err) { console.error(err) } finally { setSaving(false) } } if (!canEdit) { return (

Event Notes

{notes || '_No notes yet._'}
) } function handleBlur() { if (notes.trim() !== initialNotes?.trim()) { saveNotes() } else { setIsEditing(false) } } return (

Notes

{ if (isAuthorized) setIsEditing(true) }} > {isEditing ? (