fix(app): never block the upload queue on local preview/cache (BIT-167) #167

Merged
spark merged 1 commits from task/BIT-167-upload-queue-unstick into main 2026-11-03 20:53:16 +00:00
+49 -23
View File
@@ -183,18 +183,19 @@ export function useUploadQueue(api: ApiClient, orgId: string) {
const next = queue.current.shift()!;
running.current++;
void (async () => {
const { item, file, eventId, original, sha256, onSettled } = next;
const { item, file, original, onSettled } = next;
let final: UploadStatus = "failed";
let filed: PhotoSummary | undefined;
let waiting = false;
update(item.id, { status: "uploading", error: undefined });
try {
let lastTick = 0;
// Read eventId/sha at send time so a late EXIF route on `next` still wins if it finished first.
const res = await sendResumable(
api,
orgId,
file,
eventId,
next.eventId,
(p) => {
const now = Date.now();
if (now - lastTick > 150 || p === 1) {
@@ -234,7 +235,7 @@ export function useUploadQueue(api: ApiClient, orgId: string) {
} finally {
if (!waiting) {
if (final === "done" || final === "duplicate") {
settleLocalUpload(orgId, sha256, filed);
settleLocalUpload(orgId, next.sha256, filed);
void offlineQueue(orgId).flush(api);
}
onSettled?.(final, filed);
@@ -250,6 +251,10 @@ export function useUploadQueue(api: ApiClient, orgId: string) {
const add = useCallback(
/** `onSettled` is called once per accepted file, after it is sent (or definitely failed). */
(files: Iterable<File>, eventId?: string, onSettled?: (file: File, status: UploadStatus, photo?: PhotoSummary) => void, original?: DesktopOrigin) => {
// A previous offline pause must not leave new shots stuck on "queued" after the VPN is back.
if (paused.current) {
paused.current = false;
}
const added: UploadItem[] = [];
for (const file of files) {
const item: UploadItem = { id: crypto.randomUUID(), name: file.name, size: file.size, status: "queued", progress: 0 };
@@ -259,29 +264,50 @@ export function useUploadQueue(api: ApiClient, orgId: string) {
continue;
}
added.push(item);
// Preview in the grid before the bytes leave this computer (USB, drop, FTP share this path).
// Event from EXIF + window (not the open folder alone), so a morning studio card does not land on the afternoon training.
// Preview / EXIF routing must not block the wire: a hung IndexedDB cache left the bar at "queued" forever.
// Enqueue at once with the open pin; enrich sha + routed event while the queue runs (or before the first byte).
const entry: {
item: UploadItem;
file: File;
eventId: string | undefined;
original?: DesktopOrigin;
sha256?: string | null;
onSettled?: (status: UploadStatus, photo?: PhotoSummary) => void;
} = {
item,
file,
eventId,
...(original && { original }),
sha256: null,
...(onSettled && { onSettled: (status: UploadStatus, photo?: PhotoSummary) => onSettled(file, status, photo) }),
};
queue.current.push(entry);
void (async () => {
const cached = orgId ? await cache.events(orgId) : null;
const shown = await showLocalJpeg({
file,
eventId,
origin: original,
orgId,
events: cached?.events,
});
queue.current.push({
item,
file,
eventId: shown?.eventId ?? eventId,
...(original && { original }),
sha256: shown?.sha256 ?? null,
...(onSettled && { onSettled: (status: UploadStatus, photo?: PhotoSummary) => onSettled(file, status, photo) }),
});
pump();
try {
const cached = orgId
? await Promise.race([
cache.events(orgId),
new Promise<null>((resolve) => setTimeout(() => resolve(null), 800)),
])
: null;
const shown = await showLocalJpeg({
file,
eventId,
origin: original,
orgId,
events: cached?.events,
});
if (!shown) return;
entry.sha256 = shown.sha256;
// Prefer EXIF routing when it names an event; otherwise keep the open pin already on the entry.
if (shown.eventId) entry.eventId = shown.eventId;
} catch {
/* preview optional — upload already queued */
}
})();
}
setItems((list) => [...list.filter((i) => i.status !== "done" && i.status !== "duplicate"), ...added]);
setItems((list) => list.filter((i) => i.status !== "done" && i.status !== "duplicate").concat(added));
pump();
},
[orgId, pump],
);