import { useCallback, useEffect, useRef, useState } from 'react'; import { listTether, removeTetherFile, tetherFileUrl } from '../lib/api'; import type { TetherFile } from '../lib/api'; interface Props { event: { id: string; name: string }; /** Imports the frames and resolves with how many became photos. */ onImport: (files: File[]) => Promise; onClose: () => void; } interface LogEntry { name: string; at: number; ok: boolean; message: string; } const POLL_MS = 4000; function stamp(ms: number) { return new Date(ms).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit', }); } /** * Tethering: watch a folder on the server and import what appears in it. * * Cameras and capture apps write into `/tether/`; this panel * polls that folder, pulls each new frame, runs the normal import path (so raw * decoding, metadata and cloud mirroring all behave the same as a drag and * drop), then deletes the file from the folder. A file is only deleted after a * successful import, so a closed tab or a dropped connection leaves it queued. */ export default function TetherPanel({ event, onImport, onClose }: Props) { const [folder, setFolder] = useState(''); const [waiting, setWaiting] = useState([]); const [log, setLog] = useState([]); const [watching, setWatching] = useState(true); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [imported, setImported] = useState(0); const seen = useRef(new Set()); const working = useRef(false); const watchingRef = useRef(true); watchingRef.current = watching; const drain = useCallback(async () => { if (working.current || !watchingRef.current) return; working.current = true; setBusy(true); try { const state = await listTether(event.id); setFolder(state.folder); setWaiting(state.files); setError(null); for (const file of state.files) { if (!watchingRef.current) break; const key = `${file.name}:${file.mtime}:${file.size}`; if (seen.current.has(key)) continue; seen.current.add(key); try { const res = await fetch(tetherFileUrl(event.id, file.name), { credentials: 'same-origin', }); if (!res.ok) throw new Error(`Download failed (${res.status})`); const blob = await res.blob(); const upload = new File([blob], file.name, { type: blob.type || 'application/octet-stream', lastModified: file.mtime, }); const count = await onImport([upload]); if (count < 1) { seen.current.delete(key); throw new Error('Nothing was imported'); } await removeTetherFile(event.id, file.name).catch(() => undefined); setImported((n) => n + count); setLog((entries) => [{ name: file.name, at: Date.now(), ok: true, message: 'imported' }, ...entries].slice(0, 40), ); setWaiting((files) => files.filter((item) => item.name !== file.name)); } catch (err) { seen.current.delete(key); setLog((entries) => [ { name: file.name, at: Date.now(), ok: false, message: err instanceof Error ? err.message : 'Import failed', }, ...entries, ].slice(0, 40), ); } } } catch (err) { setError(err instanceof Error ? err.message : 'Could not read the tether folder'); } finally { working.current = false; setBusy(false); } }, [event.id, onImport]); useEffect(() => { void drain(); const timer = window.setInterval(() => void drain(), POLL_MS); return () => window.clearInterval(timer); }, [drain]); return (
e.target === e.currentTarget && onClose()} >

Tethering

Frames dropped into the folder are imported into “{event.name}” automatically.

{error &&
{error}
}
Watch folder on the server {folder || 'Reading…'}

Point a tethering app, a camera's transfer folder or a sync client at this path. JPEG, TIFF, WebP and camera raw files are picked up.

{busy ? 'Pulling frames…' : waiting.length ? `${waiting.length} frame${waiting.length === 1 ? '' : 's'} queued` : 'Folder is empty'}
{imported} imported this session
{log.length > 0 && (
    {log.map((entry, index) => (
  • {stamp(entry.at)} {entry.name} {entry.message}
  • ))}
)}
); }