// Submit screens — photo flow & story-only flow // ───────────────────────────────────────────────────────────── // LocationPicker — a map you click to drop the report's pin. `focus` // changes when the pin was set from outside (a place search), and the map // flies there; clicks report back through onPick. // ───────────────────────────────────────────────────────────── function LocationPicker({ lat, lng, focus, onPick, height = 300 }) { const ref = React.useRef(null); const mapRef = React.useRef(null); const markerRef = React.useRef(null); const onPickRef = React.useRef(onPick); onPickRef.current = onPick; React.useEffect(() => { const map = L.map(ref.current, { center: [25, 10], zoom: 2, minZoom: 1, maxZoom: 18, worldCopyJump: true }); L.maplibreGL({ style: 'https://tiles.openfreemap.org/styles/dark', attribution: 'OpenFreeMap © OpenStreetMap', }).addTo(map); map.on('click', e => onPickRef.current(e.latlng.lat, ((e.latlng.lng + 540) % 360) - 180)); map.getContainer().style.cursor = 'crosshair'; mapRef.current = map; return () => { map.remove(); mapRef.current = null; markerRef.current = null; }; }, []); const la = parseFloat(lat), ln = parseFloat(lng); const has = Number.isFinite(la) && Number.isFinite(ln); React.useEffect(() => { const map = mapRef.current; if (!map) return; if (!has) { if (markerRef.current) { markerRef.current.remove(); markerRef.current = null; } return; } const icon = L.divIcon({ className: '', iconSize: [16, 16], iconAnchor: [8, 8], html: '
' }); if (markerRef.current) markerRef.current.setLatLng([la, ln]); else markerRef.current = L.marker([la, ln], { icon }).addTo(map); }, [la, ln, has]); // A searched place: fly there (clicks don't move the map). React.useEffect(() => { if (focus && has && mapRef.current) mapRef.current.flyTo([la, ln], 11, { duration: 0.8 }); }, [focus]); return ; } // Place names <-> coordinates, from OpenStreetMap's Nominatim (free, no key). // Optional: if it's unreachable the form still works with a typed place. const NOMINATIM = 'https://nominatim.openstreetmap.org'; async function findPlace(query) { const r = await fetch(`${NOMINATIM}/search?format=jsonv2&limit=1&accept-language=en&q=${encodeURIComponent(query)}`); const [hit] = await r.json(); return hit ? { lat: parseFloat(hit.lat), lng: parseFloat(hit.lon) } : null; } async function nameOfPlace(lat, lng) { const r = await fetch(`${NOMINATIM}/reverse?format=jsonv2&zoom=10&accept-language=en&lat=${lat}&lon=${lng}`); const a = (await r.json()).address || {}; return [a.city || a.town || a.village || a.municipality || a.county, a.state, a.country].filter(Boolean).join(', '); } const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; function SubmitScreen({ mode = 'photo', onNav, user, onSubmitted }) { const { ENCOUNTER_TYPES } = window.ARCHIVE; // `types` is an ordered list — first item is the primary category. // An incident may belong to several (e.g. "Missing time" + "Crop circles"). const blankForm = { types: [], title: '', summary: '', full_account: '', time: '', // The date as parts: the year is enough, month and day only if known. year: '', month: '', day: '', place: '', lat: '', lng: '', device: '', witnesses: 1, tags: '', anonymous: !user, }; // Drafts: everything typed is kept in this browser (per flow and account) // until the report is submitted, so a closed tab or a reload loses nothing. // Files can't be kept that way; the banner asks for them again. const draftKey = `openufoarchive:draft:${mode}:${user?.id || 'anon'}`; const [draft] = React.useState(() => { try { const d = JSON.parse(localStorage.getItem(draftKey)); return d && d.form ? d : null; } catch { return null; } }); const [showDraftNote, setShowDraftNote] = React.useState(!!draft); const [step, setStep] = React.useState(draft ? draft.step || 1 : 1); const [form, setForm] = React.useState(draft ? { ...blankForm, ...draft.form } : blankForm); const [pinFocus, setPinFocus] = React.useState(0); const [placeMsg, setPlaceMsg] = React.useState(''); const [dragOver, setDragOver] = React.useState(false); const [submitting, setSubmitting] = React.useState(false); // Real File objects, wrapped with display metadata. const [files, setFiles] = React.useState([]); // Supporting documents — available in BOTH flows. // Corroborating material: police reports, newspaper clippings, affidavits, // medical records, radar logs, shortwave recordings, etc. const [docs, setDocs] = React.useState([]); // The picture shown on the archive's cards (a File from files or docs). // Defaults to the first picture attached. const [thumbFile, setThumbFile] = React.useState(null); const fileInputRef = React.useRef(null); const thumbInputRef = React.useRef(null); const update = (k, v) => setForm({ ...form, [k]: v }); const hasContent = form.title || form.summary || form.full_account || form.types.length > 0 || form.place; React.useEffect(() => { try { if (hasContent) { localStorage.setItem(draftKey, JSON.stringify({ form, step, fileCount: files.length + docs.length, savedAt: Date.now(), })); } } catch { /* storage full or blocked: drafts are a convenience */ } }, [form, step, files.length, docs.length]); const clearDraft = () => { try { localStorage.removeItem(draftKey); } catch {} }; const startOver = () => { clearDraft(); setForm(blankForm); setStep(1); setFiles([]); setDocs([]); setThumbFile(null); setShowDraftNote(false); }; const humanSize = (bytes) => { if (!Number.isFinite(bytes)) return ''; if (bytes >= 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; if (bytes >= 1024) return (bytes / 1024).toFixed(0) + ' KB'; return bytes + ' B'; }; // Pictures every browser can show; only these can be the thumbnail. const isPicture = (type) => /^image\/(jpeg|png|webp|gif)$/.test(type || ''); const wrapFiles = (fileList) => Array.from(fileList || []).map(f => ({ name: f.name, size: humanSize(f.size), type: f.type, file: f, preview: isPicture(f.type) ? URL.createObjectURL(f) : null, })); // One upload area for everything: photos, video and audio show as tiles, // anything else (PDF, Word, text…) goes in the documents list. At most 10 // files per report (one upload request). const MAX_FILES = 10; const isMedia = (type) => /^(image|video|audio)\//.test(type || ''); const addAttachments = (fileList) => { const room = MAX_FILES - files.length - docs.length; const added = wrapFiles(fileList).slice(0, Math.max(0, room)); setFiles(prev => [...prev, ...added.filter(w => isMedia(w.type))]); setDocs(prev => [...prev, ...added.filter(w => !isMedia(w.type))]); }; // "1989", "1989-05" or "1989-05-14" from the parts. const pad = (n) => String(n).padStart(2, '0'); const dateValue = () => !/^\d{4}$/.test(form.year) ? '' : form.year + (form.month ? '-' + pad(form.month) + (form.day ? '-' + pad(form.day) : '') : ''); const daysIn = (y, m) => new Date(parseInt(y, 10) || 2000, m, 0).getDate(); const thisYear = new Date().getFullYear(); const yearOk = /^\d{4}$/.test(form.year) && +form.year >= 1000 && +form.year <= thisYear; // Map pin: clicking sets it; if no place was typed yet, name it too. const pickOnMap = (lat, lng) => { setForm(prev => ({ ...prev, lat: lat.toFixed(5), lng: lng.toFixed(5) })); if (!form.place.trim()) { nameOfPlace(lat, lng).then(name => name && setForm(prev => prev.place.trim() ? prev : { ...prev, place: name })).catch(() => {}); } }; const searchPlace = async () => { if (!form.place.trim()) return setPlaceMsg('Type a place first'); setPlaceMsg('Searching…'); try { const hit = await findPlace(form.place); if (!hit) return setPlaceMsg('Not found: click the map instead'); setForm(prev => ({ ...prev, lat: hit.lat.toFixed(5), lng: hit.lng.toFixed(5) })); setPinFocus(n => n + 1); setPlaceMsg('Pinned: click the map to adjust'); } catch { setPlaceMsg('Search unavailable: click the map instead'); } }; const pictures = [...files, ...docs].filter(w => w.preview); const thumb = pictures.find(w => w.file === thumbFile) || pictures[0] || null; // A picture tile: fixed height, width follows the picture. Tapping it // makes it the thumbnail. const PictureTile = ({ w, height }) => (