import { useState, useRef } from 'react' import type { Note, NoteCreate, NoteUrl } from '../../api/notes' interface NoteFormProps { initialValues?: Note onSubmit: (data: NoteCreate) => Promise onCancel: () => void submitLabel?: string } const CATEGORIES = ['personnel', 'travail', 'idée', 'recette', 'voyage', 'santé', 'autre'] const inputStyle: React.CSSProperties = { width: '100%', background: 'var(--bg-4)', border: '1px solid var(--bg-5)', borderRadius: 8, padding: '8px 12px', color: 'var(--ink-1)', fontFamily: 'var(--font-ui)', fontSize: 14, boxSizing: 'border-box', } const labelStyle: React.CSSProperties = { color: 'var(--ink-3)', fontSize: 11, fontFamily: 'var(--font-ui)', textTransform: 'uppercase', letterSpacing: 0.5, marginBottom: 6, } export default function NoteForm({ initialValues, onSubmit, onCancel, submitLabel = 'Créer' }: NoteFormProps) { const [title, setTitle] = useState(initialValues?.title ?? '') const [content, setContent] = useState(initialValues?.content ?? '') const [category, setCategory] = useState(initialValues?.category ?? '') const [tagInput, setTagInput] = useState(initialValues?.tags.join(', ') ?? '') const [urls, setUrls] = useState(initialValues?.urls ?? []) const [urlLabel, setUrlLabel] = useState('') const [urlHref, setUrlHref] = useState('') const [urlError, setUrlError] = useState(null) const [gpsLat, setGpsLat] = useState(initialValues?.gps_lat ?? undefined) const [gpsLon, setGpsLon] = useState(initialValues?.gps_lon ?? undefined) const [gpsLoading, setGpsLoading] = useState(false) const [gpsError, setGpsError] = useState(null) const [gpsManual, setGpsManual] = useState(false) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const contentRef = useRef(null) function parseTags(raw: string): string[] { return raw.split(',').map(t => t.trim()).filter(Boolean) } function addUrl() { const href = urlHref.trim() const label = urlLabel.trim() || href if (!href) return if (!href.startsWith('http://') && !href.startsWith('https://')) { setUrlError('URL doit commencer par http:// ou https://') return } setUrls(prev => [...prev, { label, url: href }]) setUrlLabel('') setUrlHref('') setUrlError(null) } function removeUrl(idx: number) { setUrls(prev => prev.filter((_, i) => i !== idx)) } function handleGps() { setGpsError(null) if (!navigator.geolocation) { setGpsError('GPS non disponible (HTTPS requis hors localhost)') setGpsManual(true) return } setGpsLoading(true) navigator.geolocation.getCurrentPosition( pos => { setGpsLat(pos.coords.latitude) setGpsLon(pos.coords.longitude) setGpsLoading(false) setGpsManual(false) }, err => { setGpsLoading(false) if (err.code === 1) setGpsError('Permission refusée') else if (err.code === 2) setGpsError('Position indisponible — saisie manuelle ?') else setGpsError('Délai dépassé — saisie manuelle ?') setGpsManual(true) }, { timeout: 8000 }, ) } function handleManualCoord(field: 'lat' | 'lon', val: string) { const n = parseFloat(val.replace(',', '.')) if (field === 'lat') setGpsLat(isNaN(n) ? undefined : n) else setGpsLon(isNaN(n) ? undefined : n) } async function handleSubmit() { if (!content.trim()) return setSaving(true) setError(null) try { await onSubmit({ title: title.trim() || undefined, content: content.trim(), category: category || undefined, tags: parseTags(tagInput), gps_lat: gpsLat, gps_lon: gpsLon, urls: urls.length > 0 ? urls : [], }) } catch { setError('Erreur lors de la sauvegarde') setSaving(false) } } return (
{error && (

{error}

)} setTitle(e.target.value)} />