0c113e7a91
Camera profiles, proofing tables and the half-float sidecar all have to agree with the pixels the editor actually grades, so they land together with the preset filter menu that was unusable while thumbnails streamed in. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
195 lines
6.6 KiB
TypeScript
195 lines
6.6 KiB
TypeScript
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<number>;
|
||
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 `<data>/tether/<event>`; 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<TetherFile[]>([]);
|
||
const [log, setLog] = useState<LogEntry[]>([]);
|
||
const [watching, setWatching] = useState(true);
|
||
const [busy, setBusy] = useState(false);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [imported, setImported] = useState(0);
|
||
const seen = useRef(new Set<string>());
|
||
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 (
|
||
<div
|
||
className="modal-backdrop"
|
||
onPointerDown={(e) => e.target === e.currentTarget && onClose()}
|
||
>
|
||
<div className="modal tether-modal" role="dialog" aria-modal="true" aria-label="Tethering">
|
||
<header className="modal-head">
|
||
<div>
|
||
<h2>Tethering</h2>
|
||
<p className="muted small">
|
||
Frames dropped into the folder are imported into “{event.name}” automatically.
|
||
</p>
|
||
</div>
|
||
<button className="icon-btn" onClick={onClose} title="Close">
|
||
×
|
||
</button>
|
||
</header>
|
||
|
||
<div className="modal-body">
|
||
{error && <div className="banner error">{error}</div>}
|
||
|
||
<div className="tether-folder">
|
||
<span className="muted small">Watch folder on the server</span>
|
||
<code>{folder || 'Reading…'}</code>
|
||
<p className="muted small">
|
||
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.
|
||
</p>
|
||
</div>
|
||
|
||
<div className="tether-actions">
|
||
<button
|
||
className={`chip ${watching ? 'on' : ''}`}
|
||
onClick={() => setWatching((value) => !value)}
|
||
>
|
||
{watching ? 'Watching' : 'Paused'}
|
||
</button>
|
||
<span className="muted small">
|
||
{busy
|
||
? 'Pulling frames…'
|
||
: waiting.length
|
||
? `${waiting.length} frame${waiting.length === 1 ? '' : 's'} queued`
|
||
: 'Folder is empty'}
|
||
</span>
|
||
<div className="spacer" />
|
||
<span className="muted small">
|
||
{imported} imported this session
|
||
</span>
|
||
<button className="btn ghost small" onClick={() => void drain()} disabled={busy}>
|
||
Check now
|
||
</button>
|
||
</div>
|
||
|
||
{log.length > 0 && (
|
||
<ul className="tether-log">
|
||
{log.map((entry, index) => (
|
||
<li key={`${entry.name}:${entry.at}:${index}`} className={entry.ok ? 'ok' : 'fail'}>
|
||
<span className="tether-time">{stamp(entry.at)}</span>
|
||
<span className="tether-name" title={entry.name}>
|
||
{entry.name}
|
||
</span>
|
||
<span className="muted small">{entry.message}</span>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
</div>
|
||
|
||
<footer className="modal-foot">
|
||
<span className="muted small">
|
||
Files leave the folder only after a successful import.
|
||
</span>
|
||
<button className="btn" onClick={onClose}>
|
||
Done
|
||
</button>
|
||
</footer>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|