From 7b888278475481b7d2e5a24bf16a7f79be3cddd5 Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 17 Oct 2026 21:00:51 +0100 Subject: [PATCH] Folder search keeps going when the window is closed. Closing it only hides the window; a bar at the bottom opens it again. The Mac build also links compiler-rt, which the GPU recogniser needs and the installer was missing. --- apps/app/src/i18n.ts | 3 + apps/app/src/library/CardScan.test.tsx | 48 ++++++++++++++++ apps/app/src/library/CardScan.tsx | 80 ++++++++++++++++++++++---- apps/app/src/library/folderScan.tsx | 68 ++++++++++++++++++++++ apps/app/src/pages/LibraryPage.tsx | 26 ++++----- apps/app/src/pages/Shell.tsx | 5 +- apps/desktop/package.json | 2 +- apps/desktop/src-tauri/Cargo.toml | 2 +- apps/desktop/src-tauri/build.rs | 26 +++++++++ apps/desktop/src-tauri/tauri.conf.json | 2 +- 10 files changed, 233 insertions(+), 29 deletions(-) create mode 100644 apps/app/src/library/folderScan.tsx diff --git a/apps/app/src/i18n.ts b/apps/app/src/i18n.ts index 1f18bd2..8c4a1b0 100644 --- a/apps/app/src/i18n.ts +++ b/apps/app/src/i18n.ts @@ -639,6 +639,9 @@ export const t = { scanning: (done: number, total: number) => `A ver ${done} de ${total}`, cancel: "Parar", close: "Fechar", + reopen: "Abrir", + backgroundDone: "A pasta está lida.", + stopped: "A pesquisa foi parada.", none: "Nenhuma foto corresponde a essa pesquisa.", empty: "Nenhuma cara nem número nestas fotos.", unread: (n: number) => (n === 1 ? "1 foto sem cara nem número ficou de fora." : `${n} fotos sem cara nem número ficaram de fora.`), diff --git a/apps/app/src/library/CardScan.test.tsx b/apps/app/src/library/CardScan.test.tsx index 7b0b103..a77d038 100644 --- a/apps/app/src/library/CardScan.test.tsx +++ b/apps/app/src/library/CardScan.test.tsx @@ -1,5 +1,6 @@ import { fireEvent, render, screen } from "@testing-library/react"; import type { ApiClient, EventSummary } from "@photoreport/sdk"; +import { useState } from "react"; import { describe, expect, it, vi } from "vitest"; import { CardScan, cardMatches } from "./CardScan.js"; @@ -107,4 +108,51 @@ describe("card folder dialog", () => { expect(await screen.findByRole("button", { name: /Rui/ })).toBeTruthy(); expect(scanCard).toHaveBeenCalledTimes(1); }); + + it("keeps reading after the dialog is closed", async () => { + vi.stubGlobal("createImageBitmap", async () => ({ width: 800, height: 600, close() {} })); + vi.spyOn(URL, "createObjectURL").mockImplementation(() => "blob:later"); + vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {}); + HTMLCanvasElement.prototype.getContext = (() => ({ drawImage() {} })) as unknown as typeof HTMLCanvasElement.prototype.getContext; + HTMLCanvasElement.prototype.toBlob = function (cb) { + cb(new Blob([new Uint8Array([1])], { type: "image/jpeg" })); + }; + let release: (value: { slots: number; roster: number; faces: ReturnType[] }) => void = () => {}; + const scanCard = vi.fn( + () => + new Promise<{ slots: number; roster: number; faces: ReturnType[] }>((resolve) => { + release = resolve; + }), + ); + const dismissed = vi.fn(); + function Host() { + const [visible, setVisible] = useState(true); + return ( + {}} + onClose={() => setVisible(false)} + onShow={() => setVisible(true)} + onDismiss={dismissed} + /> + ); + } + render(); + const picker = document.querySelector("input[type=file]") as HTMLInputElement; + fireEvent.change(picker, { target: { files: [new File(["a"], "a.jpg", { type: "image/jpeg" })] } }); + expect(await screen.findByText("A ver 0 de 1")).toBeTruthy(); + fireEvent.click(screen.getByRole("button", { name: "Fechar" })); + expect(dismissed).not.toHaveBeenCalled(); + expect(screen.queryByRole("dialog")).toBeNull(); + expect(screen.getByRole("status").textContent).toContain("A ver 0 de 1"); + release({ slots: 4, roster: 1, faces: [face({ name: "Rui" })] }); + expect(await screen.findByText("A pasta está lida.")).toBeTruthy(); + fireEvent.click(screen.getByRole("button", { name: "Abrir" })); + expect(await screen.findByRole("button", { name: /Rui/ })).toBeTruthy(); + expect(scanCard).toHaveBeenCalledTimes(1); + }); }); diff --git a/apps/app/src/library/CardScan.tsx b/apps/app/src/library/CardScan.tsx index 6b78cbf..1c45e24 100644 --- a/apps/app/src/library/CardScan.tsx +++ b/apps/app/src/library/CardScan.tsx @@ -1,6 +1,6 @@ import type { ApiClient, CardScan as CardReading, EventSummary } from "@photoreport/sdk"; import { ApiError } from "@photoreport/sdk"; -import { useEffect, useMemo, useRef, useState } from "react"; +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { t } from "../i18n.js"; import { kv } from "../offline/kv.js"; import { asArrayBuffer, desktopInvoke, isDesktop } from "../platform.js"; @@ -107,13 +107,21 @@ export function CardScan({ games, onImport, onClose, + onDismiss = onClose, + onShow, + visible = true, }: { api: ApiClient; orgId: string; eventId: string; games: EventSummary[]; onImport: (files: File[], eventId: string) => void; + /** Hide the dialog. The read keeps going. */ onClose: () => void; + /** Throw the panel away. Used when nothing was started, or when the finished bar is closed. */ + onDismiss?: () => void; + onShow?: () => void; + visible?: boolean; }) { const input = useRef(null); const stop = useRef(false); @@ -129,24 +137,47 @@ export function CardScan({ const [roster, setRoster] = useState(undefined); const [localMode, setLocalMode] = useState(false); const [accelerators, setAccelerators] = useState(null); + const [stopped, setStopped] = useState(false); const folderRef = useRef(""); const gameRef = useRef(gameId); gameRef.current = gameId; const choices = useMemo(() => [...games].sort((a, b) => b.startsAt.localeCompare(a.startsAt)), [games]); + const runningRef = useRef(false); + const itemsRef = useRef([]); + const localRef = useRef(false); + const onCloseRef = useRef(onClose); + const onDismissRef = useRef(onDismiss); + runningRef.current = running; + itemsRef.current = items; + localRef.current = localMode; + onCloseRef.current = onClose; + onDismissRef.current = onDismiss; + const hide = useCallback(() => { + if (!runningRef.current && itemsRef.current.length === 0) onDismissRef.current(); + else onCloseRef.current(); + }, []); + const cancelScan = () => { + stop.current = true; + setStopped(true); + if (localRef.current) void desktopInvoke("faces_cancel").catch(() => {}); + }; + useEffect(() => { + if (!visible) return; const onKey = (e: KeyboardEvent) => { - if (e.key === "Escape") onClose(); + if (e.key === "Escape") hide(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); - }, [onClose]); + }, [visible, hide]); const live = useRef([]); live.current = items; useEffect(() => { return () => { stop.current = true; + if (localRef.current) void desktopInvoke("faces_cancel").catch(() => {}); for (const item of live.current) if (item.thumb.startsWith("blob:")) URL.revokeObjectURL(item.thumb); }; }, []); @@ -156,6 +187,7 @@ export function CardScan({ const run = async (list: FileList) => { stop.current = false; + setStopped(false); setNote(null); setSent(null); setSelected(new Set()); @@ -230,6 +262,7 @@ export function CardScan({ const runLocal = async (folder: string) => { stop.current = false; + setStopped(false); folderRef.current = folder; setLocalMode(true); setAccelerators(null); @@ -341,6 +374,36 @@ export function CardScan({ }); }; + if (!visible) { + if (!running && items.length === 0) return null; + return ( +
+ {t.cardScan.title} + {running ? ( + {total > 0 ? t.cardScan.scanning(done, total) : note || t.cardScan.preparing} + ) : ( + {stopped ? t.cardScan.stopped : t.cardScan.backgroundDone} + )} + {localMode && accelerators === "all" && {t.cardScan.accelAll}} + {localMode && accelerators === "gpu" && {t.cardScan.accelGpu}} + {localMode && accelerators === "ane" && {t.cardScan.accelAne}} + {localMode && accelerators === "cpu" && {t.cardScan.accelCpu}} + + {running ? ( + + ) : ( + + )} +
+ ); + } + return (
@@ -387,18 +450,11 @@ export function CardScan({ }} /> {running && ( - )} -
diff --git a/apps/app/src/library/folderScan.tsx b/apps/app/src/library/folderScan.tsx new file mode 100644 index 0000000..9f8917e --- /dev/null +++ b/apps/app/src/library/folderScan.tsx @@ -0,0 +1,68 @@ +import type { EventSummary } from "@photoreport/sdk"; +import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from "react"; +import { useAuth } from "../auth.js"; +import { CardScan } from "./CardScan.js"; +import { useIngest } from "./ingest.js"; + +interface Session { + eventId: string; + games: EventSummary[]; + visible: boolean; +} + +interface FolderScanApi { + open: (eventId: string, games: EventSummary[]) => void; + syncGames: (games: EventSummary[]) => void; +} + +const FolderScanContext = createContext(null); + +/** + * Folder search lives above the modules, same as the upload queue. Closing the dialog only hides it: the read keeps + * going, and a bar at the bottom opens it again. A new organization remounts this with the ingest, which stops the read. + */ +export function FolderScanProvider({ children }: { children: ReactNode }) { + const { api, organization } = useAuth(); + const { uploads } = useIngest(); + const orgId = organization?.id ?? ""; + const [session, setSession] = useState(null); + + const open = useCallback((eventId: string, games: EventSummary[]) => { + setSession((current) => { + if (current) return { ...current, visible: true, games: games.length > 0 ? games : current.games }; + return { eventId, games, visible: true }; + }); + }, []); + + const syncGames = useCallback((games: EventSummary[]) => { + if (games.length === 0) return; + setSession((current) => (current ? { ...current, games } : current)); + }, []); + + const value = useMemo(() => ({ open, syncGames }), [open, syncGames]); + + return ( + + {children} + {session && orgId && ( + uploads.add(files, id)} + onClose={() => setSession((current) => (current ? { ...current, visible: false } : current))} + onShow={() => setSession((current) => (current ? { ...current, visible: true } : current))} + onDismiss={() => setSession(null)} + /> + )} + + ); +} + +export function useFolderScan(): FolderScanApi { + const ctx = useContext(FolderScanContext); + if (!ctx) throw new Error("useFolderScan outside FolderScanProvider"); + return ctx; +} diff --git a/apps/app/src/pages/LibraryPage.tsx b/apps/app/src/pages/LibraryPage.tsx index 869a15f..76f7f78 100644 --- a/apps/app/src/pages/LibraryPage.tsx +++ b/apps/app/src/pages/LibraryPage.tsx @@ -8,7 +8,7 @@ import { t } from "../i18n.js"; import { EventForm } from "../library/EventForm.js"; import { EventsPanel } from "../library/EventsPanel.js"; import { EventSettingsButton } from "../library/EventSettings.js"; -import { CardScan } from "../library/CardScan.js"; +import { useFolderScan } from "../library/folderScan.js"; import { FacesView } from "../library/FacesView.js"; import { LookupBanner } from "../library/PhotoLookup.js"; import { Grid, Loupe, type LoupeFace } from "../library/Grid.js"; @@ -47,7 +47,7 @@ export function LibraryPage() { const [inboxCount, setInboxCount] = useState(0); const [targets, setTargets] = useState([]); const [iptcOpen, setIptcOpen] = useState(false); - const [cardOpen, setCardOpen] = useState(false); + const { open: openFolderScan, syncGames } = useFolderScan(); const [state, dispatch] = useReducer(libraryReducer, initialLibrary); const [filter, setFilterState] = useState(() => savedFilter(orgId)); useEffect(() => setFilterState(savedFilter(orgId)), [orgId]); @@ -211,6 +211,10 @@ export function LibraryPage() { const [dragging, setDragging] = useState(false); const uploadInto = state.scope && state.scope !== "inbox" && !state.scope.startsWith("search:") && !state.scope.startsWith("face:") ? state.scope : undefined; const openEvent = events.find((e) => e.id === uploadInto) ?? archivedEvents?.find((e) => e.id === uploadInto); + useEffect(() => { + const games = openEvent && !events.some((event) => event.id === openEvent.id) ? [openEvent, ...events] : events; + syncGames(games); + }, [events, openEvent, syncGames]); const sending = uploads.items.filter((i) => i.status === "queued" || i.status === "uploading"); const failed = uploads.items.filter((i) => i.status === "failed"); const sentCount = uploads.items.filter((i) => i.status === "done" || i.status === "duplicate").length; @@ -358,7 +362,13 @@ export function LibraryPage() { {t.importPhotos} {uploadInto && ( - )} @@ -581,16 +591,6 @@ export function LibraryPage() { onPlayers={(ids, players) => patch(ids, { players })} /> - {cardOpen && uploadInto && openEvent && ( - e.id === openEvent.id) ? events : [openEvent, ...events]} - onImport={(files, id) => uploads.add(files, id)} - onClose={() => setCardOpen(false)} - /> - )} {iptcOpen && setIptcOpen(false)} />} {creating && ( @@ -127,7 +128,9 @@ export function Shell() {
- + + +
diff --git a/apps/desktop/package.json b/apps/desktop/package.json index c6994f5..caa71a4 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -1,6 +1,6 @@ { "name": "@photoreport/desktop", - "version": "0.3.14", + "version": "0.3.15", "private": true, "type": "module", "scripts": { diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index ce1344a..eb3d5c5 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "photoreport-desktop" -version = "0.3.14" +version = "0.3.15" edition = "2024" publish = false description = "PhotoReport desktop: the web app in a native shell, with keychain sign-in and (later) local FTP and the native engine." diff --git a/apps/desktop/src-tauri/build.rs b/apps/desktop/src-tauri/build.rs index 9788b5b..39a2725 100644 --- a/apps/desktop/src-tauri/build.rs +++ b/apps/desktop/src-tauri/build.rs @@ -47,4 +47,30 @@ fn main() { ]), )) .expect("failed to run tauri-build"); + link_coreml_compiler_rt(); +} + +/// ONNX Runtime's Core ML objects call `___isPlatformVersionAtLeast`. That helper lives in compiler-rt, which Rust's +/// linker does not pull in, so the desktop build (deployment target 11) fails on the arm64 slice. +fn link_coreml_compiler_rt() { + let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); + let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); + if os != "macos" || arch != "aarch64" { + return; + } + let Ok(output) = std::process::Command::new("xcrun") + .args(["--sdk", "macosx", "clang", "-print-file-name=libclang_rt.osx.a"]) + .output() + else { + return; + }; + if !output.status.success() { + return; + } + let path = String::from_utf8(output.stdout).unwrap_or_default(); + let path = path.trim(); + if path.is_empty() || !std::path::Path::new(path).is_file() { + return; + } + println!("cargo:rustc-link-arg={path}"); } diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.json index 719a5ee..1ff8776 100644 --- a/apps/desktop/src-tauri/tauri.conf.json +++ b/apps/desktop/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "PhotoReport", - "version": "0.3.14", + "version": "0.3.15", "identifier": "pt.photoreport.desktop", "build": { "devUrl": "http://localhost:5173", -- 2.47.3