diff --git a/apps/app/src/i18n.ts b/apps/app/src/i18n.ts index f54193c..e235328 100644 --- a/apps/app/src/i18n.ts +++ b/apps/app/src/i18n.ts @@ -219,11 +219,17 @@ export const t = { newShot: "nova", imported: "já puxada", selectNew: "Seleccionar novas", + selectEvent: "Do evento", selectAll: "Todas", selectNone: "Nenhuma", refresh: "Actualizar lista", download: (n: number) => (n === 1 ? "Descarregar 1" : `Descarregar ${n}`), emptyCard: "O cartão ainda não tem fotos listadas.", + outsideEvent: "fora do evento", + windowHint: (inWin: number, total: number) => `${inWin} de ${total} na janela do evento`, + autoIngest: "Puxar automaticamente (só na janela do evento)", + autoIngestHelp: "Só fotos novas cuja hora da câmara cai entre o início e o fim de ingestão do evento. O resto do cartão fica por escolher.", + autoNeedsEvent: "Escolhe um evento com janela de ingestão para o automático.", }, squad: { title: "Pessoas", diff --git a/apps/app/src/library/LocalFtp.tsx b/apps/app/src/library/LocalFtp.tsx index 5086aad..ff0fac1 100644 --- a/apps/app/src/library/LocalFtp.tsx +++ b/apps/app/src/library/LocalFtp.tsx @@ -47,6 +47,9 @@ export interface FtpTarget { id: string; title: string; date: string; + /** Event ingest window (ISO). USB auto-ingest only pulls shots inside this range. */ + ingestFrom?: string; + ingestUntil?: string; } export type Invoke = (cmd: string, args?: Record) => Promise; @@ -120,7 +123,15 @@ export function useLocalFtp(add: Add, enabled: boolean, invoke: Invoke = tauriIn originalsRef.current = !!status?.settings.uploadOriginals; const setTarget = useCallback((e: EventSummary | null) => { - const next = e ? { id: e.id, title: eventLabel(e), date: e.startsAt.slice(0, 10) } : null; + const next = e + ? { + id: e.id, + title: eventLabel(e), + date: e.startsAt.slice(0, 10), + ingestFrom: e.ingestFrom, + ingestUntil: e.ingestUntil, + } + : null; setTargetState(next); prefs.set("ftp.target", JSON.stringify(next)); }, []); diff --git a/apps/app/src/library/UsbCamera.test.tsx b/apps/app/src/library/UsbCamera.test.tsx index 6f9fc97..fe07002 100644 --- a/apps/app/src/library/UsbCamera.test.tsx +++ b/apps/app/src/library/UsbCamera.test.tsx @@ -3,7 +3,7 @@ import type { EventSummary } from "@photoreport/sdk"; import { describe, expect, it, vi } from "vitest"; import type { Invoke } from "./LocalFtp.js"; import type { UploadStatus } from "./uploads.js"; -import { UsbCameraButton, type UsbStatus, useUsbCamera, withSidecars } from "./UsbCamera.js"; +import { UsbCameraButton, type UsbStatus, inEventWindow, useUsbCamera, withSidecars } from "./UsbCamera.js"; const camera: UsbStatus = { supported: true, @@ -42,6 +42,8 @@ const usbStub = { syncClock: vi.fn(), importFiles: vi.fn(), refresh: vi.fn(), + autoIngest: false, + setAutoIngest: vi.fn(), }; describe("usb tether", () => { @@ -83,4 +85,13 @@ describe("usb tether", () => { expect(withSidecars(["IMG_0001.JPG"], camera.files).sort()).toEqual(["IMG_0001.CR3", "IMG_0001.JPG"]); expect(withSidecars(["IMG_0002.JPG"], camera.files)).toEqual(["IMG_0002.JPG"]); }); + + it("only treats shots inside the event ingest window as auto-eligible", () => { + const from = "2026-11-03T18:00:00.000Z"; + const until = "2026-11-03T23:00:00.000Z"; + expect(inEventWindow({ createdMs: Date.parse("2026-11-03T19:30:00Z") }, from, until)).toBe(true); + expect(inEventWindow({ createdMs: Date.parse("2026-11-02T19:30:00Z") }, from, until)).toBe(false); + expect(inEventWindow({ createdMs: null }, from, until)).toBe(false); + expect(inEventWindow({ createdMs: Date.parse("2026-11-03T19:30:00Z") }, undefined, until)).toBe(false); + }); }); diff --git a/apps/app/src/library/UsbCamera.tsx b/apps/app/src/library/UsbCamera.tsx index 90a67bb..16e5c77 100644 --- a/apps/app/src/library/UsbCamera.tsx +++ b/apps/app/src/library/UsbCamera.tsx @@ -1,7 +1,7 @@ import type { EventSummary } from "@photoreport/sdk"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { t } from "../i18n.js"; -import { asArrayBuffer } from "../platform.js"; +import { asArrayBuffer, prefs } from "../platform.js"; import { type FtpTarget, type IncomingFile, type Invoke, eventLabel } from "./LocalFtp.js"; import type { PreviewOf, UploadStatus } from "./uploads.js"; @@ -52,10 +52,15 @@ export function useUsbCamera(add: Add, enabled: boolean, target: FtpTarget | nul const [status, setStatus] = useState(null); const [outdated, setOutdated] = useState(false); const [counts, setCounts] = useState({ received: 0, sent: 0, failed: 0 }); + /** Auto-pull only new shots whose camera time sits inside the event's ingest window. */ + const [autoIngest, setAutoIngestState] = useState(savedAutoIngest); const backlog = useRef([]); const inFlight = useRef(0); + const autoAsked = useRef>(new Set()); const targetRef = useRef(target); targetRef.current = target; + const autoIngestRef = useRef(autoIngest); + autoIngestRef.current = autoIngest; const pump = useCallback(() => { while (inFlight.current < IN_FLIGHT && backlog.current.length) { @@ -160,7 +165,24 @@ export function useUsbCamera(add: Add, enabled: boolean, target: FtpTarget | nul } }, [invoke]); - return { status, outdated, counts, setTether, syncClock, importFiles, refresh }; + const setAutoIngest = useCallback((on: boolean) => { + setAutoIngestState(on); + prefs.set("usb.autoIngest", on ? "1" : "0"); + if (!on) autoAsked.current.clear(); + }, []); + + // New tethered shots inside the event window only — never the rest of the card / other days. + useEffect(() => { + if (!autoIngest || !status?.tether || !target?.ingestFrom || !target.ingestUntil) return; + const due = status.files.filter( + (f) => /\.jpe?g$/i.test(f.name) && f.isNew && !f.imported && inEventWindow(f, target.ingestFrom, target.ingestUntil) && !autoAsked.current.has(f.name), + ); + if (!due.length) return; + for (const f of due) autoAsked.current.add(f.name); + void importFiles(withSidecars(due.map((f) => f.name), status.files)); + }, [autoIngest, status, target, importFiles]); + + return { status, outdated, counts, setTether, syncClock, importFiles, refresh, autoIngest, setAutoIngest }; } export type UsbCameraState = ReturnType; @@ -177,6 +199,23 @@ export function withSidecars(selected: Iterable, files: UsbFile[]): stri return files.filter((f) => want.has(stemOf(f.name))).map((f) => f.name); } +/** + * True when the shot's camera time falls inside the event ingest window. No capture time → not auto-eligible + * (a full card without dates must not flood the event; the photographer picks those by hand). + */ +export function inEventWindow(file: Pick, fromIso: string | undefined, untilIso: string | undefined): boolean { + if (!fromIso || !untilIso) return false; + if (file.createdMs == null || !Number.isFinite(file.createdMs)) return false; + const from = Date.parse(fromIso); + const until = Date.parse(untilIso); + if (!Number.isFinite(from) || !Number.isFinite(until)) return false; + return file.createdMs >= from && file.createdMs <= until; +} + +function savedAutoIngest(): boolean { + return prefs.get("usb.autoIngest") === "1"; +} + /** Button + panel in the Library toolbar. Hidden where the shell has no USB cameras (Windows, Linux). */ export function UsbCameraButton({ usb, @@ -191,7 +230,7 @@ export function UsbCameraButton({ }) { const [open, setOpen] = useState(false); const [picked, setPicked] = useState>(() => new Set()); - const { status, counts } = usb; + const { status, counts, autoIngest, setAutoIngest } = usb; const L = t.usbCamera; const devices = status?.devices ?? []; const files = status?.files ?? []; @@ -199,6 +238,11 @@ export function UsbCameraButton({ const on = connected.some((d) => d.state === "tethered" || d.state === "opening"); const catalogReady = devices.some((d) => d.catalogReady); const jpegs = useMemo(() => files.filter((f) => /\.jpe?g$/i.test(f.name)), [files]); + const inWindow = useMemo( + () => jpegs.filter((f) => inEventWindow(f, target?.ingestFrom, target?.ingestUntil)), + [jpegs, target?.ingestFrom, target?.ingestUntil], + ); + const hasWindow = !!(target?.ingestFrom && target.ingestUntil); useEffect(() => { if (connected.length && !target && openEvent) setTarget(openEvent); @@ -259,11 +303,24 @@ export function UsbCameraButton({
{t.localFtp.target}
{target ? `${target.date} · ${target.title}` : t.localFtp.noTarget}
+ {hasWindow && ( +
+ {L.windowHint(inWindow.length, jpegs.length)} +
+ )} {openEvent && openEvent.id !== target?.id && ( )} + {on && hasWindow && ( + + )} + {autoIngest && !hasWindow &&

{L.autoNeedsEvent}

} + {autoIngest && hasWindow &&

{L.autoIngestHelp}

}
{devices.length === 0 &&

{L.none}

} {devices.map((d) => ( @@ -297,6 +354,11 @@ export function UsbCameraButton({ {jpegs.length > 0 && {L.files(jpegs.length)}}
+ {hasWindow && ( + + )} @@ -317,6 +379,7 @@ export function UsbCameraButton({
    {jpegs.map((f) => { const checked = picked.has(f.name); + const matches = inEventWindow(f, target?.ingestFrom, target?.ingestUntil); return (
  • toggle(f.name)} className="accent-accent" /> @@ -325,6 +388,7 @@ export function UsbCameraButton({ {f.isNew && !f.imported && {L.newShot}} {f.imported && {L.imported}} + {hasWindow && !matches && !f.imported && {L.outsideEvent}}
  • ); })}