import { useEffect, useRef, useState } from 'react'; import type { CloudImport, ImportProgress } from '../lib/import'; import { importFiles, isSupportedFile } from '../lib/import'; import { isRawFile, RAW_ACCEPT } from '../lib/raw'; import type { EventRecord, PhotoRecord } from '../types'; interface Props { events: EventRecord[]; initialEventId: string; initialFiles: File[]; onClose: () => void; onImported: (photos: PhotoRecord[], eventId: string) => void; onCreateEvent: (name: string) => Promise; cloud?: CloudImport | null; onNeedServer: () => void; } export default function ImportModal({ events, initialEventId, initialFiles, onClose, onImported, onCreateEvent, cloud, onNeedServer, }: Props) { const startId = initialEventId !== 'all' && initialEventId !== 'unsorted' ? initialEventId : events[0]?.id ?? '__new'; const [eventId, setEventId] = useState(startId); const [newName, setNewName] = useState(''); const [files, setFiles] = useState(initialFiles); const [dragOver, setDragOver] = useState(false); const [busy, setBusy] = useState(false); const [progress, setProgress] = useState(null); const [failures, setFailures] = useState([]); const inputRef = useRef(null); const rawCount = files.filter(isRawFile).length; useEffect(() => { // Capture phase: Escape closes this dialog even while a photo is open behind it. const onKey = (e: KeyboardEvent) => { if (e.key !== 'Escape') return; e.preventDefault(); e.stopImmediatePropagation(); if (!busy) onClose(); }; window.addEventListener('keydown', onKey, true); return () => window.removeEventListener('keydown', onKey, true); }, [busy, onClose]); const addFiles = (list: FileList | null) => { if (!list) return; const incoming = Array.from(list).filter(isSupportedFile); setFiles((prev) => { const seen = new Set(prev.map((f) => `${f.name}:${f.size}`)); return [...prev, ...incoming.filter((f) => !seen.has(`${f.name}:${f.size}`))]; }); }; const run = async () => { if (!files.length || busy) return; setBusy(true); setFailures([]); try { let targetId = eventId; if (targetId === '__new') { const created = await onCreateEvent(newName || `Event ${new Date().toLocaleDateString()}`); targetId = created.id; } const result = await importFiles(files, targetId, [], setProgress, cloud ?? null); if (result.photos.length) onImported(result.photos, targetId); if (result.failed.length) { setFailures(result.failed.map((f) => `${f.name} — ${f.reason}`)); setBusy(false); setFiles([]); setProgress(null); if (result.photos.length) setTimeout(onClose, 1200); } else { onClose(); } } catch (err) { setFailures([err instanceof Error ? err.message : 'Import failed']); setBusy(false); } }; return (
e.target === e.currentTarget && !busy && onClose()}>

Import photos

{eventId === '__new' && ( )}
{ e.preventDefault(); setDragOver(true); }} onDragLeave={() => setDragOver(false)} onDrop={(e) => { e.preventDefault(); setDragOver(false); addFiles(e.dataTransfer.files); }} onClick={() => inputRef.current?.click()} >
⤒
Drop photos here

or click to choose files. JPEG, PNG, WebP, AVIF, TIFF and camera raw (CR2, CR3, NEF, ARW, ORF, RW2, RAF, DNG, PEF…) — raw needs the team server.

addFiles(e.target.files)} />
{files.length > 0 && (
{files.length} file{files.length === 1 ? '' : 's'} ready ·{' '} {(files.reduce((sum, f) => sum + f.size, 0) / (1024 * 1024)).toFixed(1)} MB {rawCount > 0 ? ` · ${rawCount} raw` : ''}
)} {rawCount > 0 && !cloud && (
{rawCount} raw file{rawCount === 1 ? '' : 's'} cannot be read by the browser. {' '} They are decoded by the team server, so sign in before importing — or import them later from the library.
)} {rawCount > 0 && cloud && (
Raw files are demosaiced by the server (LibRaw), a few at a time in parallel. Each file usually takes 2–4 seconds.
)} {progress && (
{progress.done} / {progress.total} {progress.current ? `· ${progress.current}` : ''}
)} {failures.length > 0 && (
Some files could not be imported
    {failures.map((f) => (
  • {f}
  • ))}
)}
); }