import { useId, useState, useRef } from 'react' import { Plus, StickyNote, X } from 'lucide-react' import { DialogButton, DialogFooter, DialogHeader, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT, fs } from '../shared/DialogShell' import { EditorField, INPUT, TEXTAREA } from '../shared/dialogParts' import { Tooltip } from '../shared/Tooltip' import { useCanDo } from '../../store/permissionsStore' import { useTripStore } from '../../store/tripStore' import { AuthedImg } from './CollabNotesAuthedImg' import type { CollabNote } from './CollabNotes.types' // ── New Note Modal (portal to body) ───────────────────────────────────────── interface NoteFormModalProps { onClose: () => void onSubmit: (data: { title: string; content: string; category: string | null; website: string | null; color?: string | null; _pendingFiles?: File[]; files?: File[] }) => Promise onDeleteFile?: (noteId: number, fileId: number) => Promise existingCategories: string[] categoryColors: Record getCategoryColor: (category: string) => string note: CollabNote | null tripId: number t: (key: string) => string } export function NoteFormModal({ onClose, onSubmit, onDeleteFile, existingCategories, categoryColors, getCategoryColor, note, tripId, t }: NoteFormModalProps) { const can = useCanDo() const tripObj = useTripStore((s) => s.trip) const canUploadFiles = can('file_upload', tripObj) const isEdit = !!note const allCategories = [...new Set([...existingCategories, ...Object.keys(categoryColors || {})])].filter(Boolean) const [title, setTitle] = useState(note?.title || '') const [content, setContent] = useState(note?.content || '') const [category, setCategory] = useState(note?.category || '') const [website, setWebsite] = useState(note?.website || '') const [pendingFiles, setPendingFiles] = useState([]) const [existingAttachments, setExistingAttachments] = useState(note?.attachments || []) // Files taken off in edit mode go with Save, so Cancel leaves the note as it was. const [removedFileIds, setRemovedFileIds] = useState([]) const [submitting, setSubmitting] = useState(false) const fileRef = useRef(null) const finalCategory = category const labelId = useId() const handleSubmit = async (e?: React.FormEvent) => { e?.preventDefault() if (!title.trim()) return setSubmitting(true) try { await onSubmit({ title: title.trim(), content: content.trim(), category: finalCategory || null, color: getCategoryColor(finalCategory), website: website.trim() || null, _pendingFiles: pendingFiles, }) if (onDeleteFile && note) { for (const fileId of removedFileIds) await onDeleteFile(note.id, fileId) } onClose() } catch { } finally { setSubmitting(false) } } const handleDeleteAttachment = (fileId: number) => { if (!onDeleteFile || !note) return setExistingAttachments(prev => prev.filter(a => a.id !== fileId)) setRemovedFileIds(prev => [...prev, fileId]) } const canSubmit = title.trim() && !submitting const tint = category ? `color-mix(in srgb, ${getCategoryColor(category)} 12%, transparent)` : NEUTRAL_TINT const chip = 'flex items-center gap-1 rounded-full border border-edge-faint bg-surface-secondary px-2 py-[3px] text-content-muted' return ( { if (!canUploadFiles) return const items = e.clipboardData?.items if (!items) return for (const item of Array.from(items)) { if (item.type.startsWith('image/') || item.type === 'application/pdf') { e.preventDefault() const file = item.getAsFile() if (file) setPendingFiles(prev => [...prev, file]) return } } }} header={( } tint={tint} labelId={labelId} onClose={onClose} eyebrow={isEdit ? t('collab.notes.edit') : t('collab.notes.new')} titleInput={{ value: title, onChange: setTitle, label: t('collab.notes.titlePlaceholder'), placeholder: t('collab.notes.titlePlaceholder'), autoFocus: true, onKeyDown: e => { if (e.key === 'Enter') void handleSubmit() }, }} /> )} footer={( {t('common.cancel')} void handleSubmit()} disabled={!canSubmit}> {submitting ? '...' : isEdit ? t('collab.notes.save') : t('collab.notes.create')} )} >