Files
spark 03df893e34 Library: keep the grid loading reliably and never show a silent blank tile
Two causes of the "half-loaded" grid where some READY photos stay grey:

1. cachedImage fetched thumbnails with no timeout, inside a 6-slot concurrency
   limiter. A single stalled request (busy server, dropped connection) held its
   slot forever and starved every queued tile, so the rest of the grid never
   loaded. Download now runs through an AbortController that aborts after 20s,
   freeing the slot so the queue keeps moving; the stalled photo falls back to
   loading its URL directly instead of blocking the others.

2. The grid cell rendered `shown && <img>` with no onError and no fallback, so a
   pending or failed image was an indistinguishable grey blank. A READY photo
   now shows "…" while its thumbnail is still resolving and "erro" if the image
   fails to load (onError), mirroring the loupe's resilience.

UI/cache only; no API or data changes.

Claude-Session: https://claude.ai/code/session_01RAkmwWv6L5KmpX4h9JPD2i
2026-10-17 16:06:47 +01:00

157 lines
6.6 KiB
TypeScript

import type { ColorLabel, PhotoSummary } from "@photoreport/contracts";
import { memo, useEffect, useRef, useState } from "react";
import { t } from "../i18n.js";
import { useCachedImage } from "../offline/cache.js";
import { isDesktop } from "../platform.js";
export const LABEL_COLOR: Record<ColorLabel, string | null> = {
NONE: null,
RED: "#c94a4a",
YELLOW: "#d6b13b",
GREEN: "#3fa35f",
BLUE: "#3f7fd6",
PURPLE: "#9a5fd0",
};
interface CellProps {
photo: PhotoSummary;
index: number;
src: string;
selected: boolean;
focused: boolean;
onPick: (id: string, e: React.MouseEvent) => void;
onOpen: (id: string) => void;
}
const Cell = memo(function Cell({ photo, index, src, selected, focused, onPick, onOpen }: CellProps) {
const ref = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (focused) ref.current?.scrollIntoView({ block: "nearest" });
}, [focused]);
const label = LABEL_COLOR[photo.colorLabel];
// Desktop app: thumbnails are kept on the computer, so the grid shows without a network.
const shown = useCachedImage(src, isDesktop());
const [imgFailed, setImgFailed] = useState(false);
useEffect(() => setImgFailed(false), [shown]);
return (
<button
ref={ref}
type="button"
data-photo-id={photo.id}
onClick={(e) => onPick(photo.id, e)}
onDoubleClick={() => onOpen(photo.id)}
aria-pressed={selected}
aria-label={`${photo.name}${photo.flag === "PICK" ? ", escolhida" : photo.flag === "REJECT" ? ", rejeitada" : ""}${photo.rating ? `, ${photo.rating} estrelas` : ""}`}
className={`relative flex aspect-square flex-col items-center justify-between gap-1 border px-2 pb-1 pt-4 ${
selected ? "border-[#9a9a9a] bg-cell-on" : "border-[#151516] bg-cell hover:bg-[#444445]"
} ${focused ? "outline outline-1 outline-offset-[-3px] outline-white/60" : ""} ${photo.flag === "REJECT" ? "opacity-40" : ""}`}
>
<span aria-hidden="true" className="absolute left-2 top-0.5 font-mono text-xl font-bold text-white/[0.08]">
{index + 1}
</span>
<span className="flex min-h-0 w-full flex-1 items-center justify-center">
{photo.status === "READY" ? (
imgFailed ? (
<span className="font-mono text-[10px] uppercase text-faint">erro</span>
) : shown ? (
<img src={shown} onError={() => setImgFailed(true)} alt="" loading="lazy" decoding="async" draggable={false} className="max-h-full max-w-full object-contain shadow-[0_1px_3px_rgba(0,0,0,0.6)]" />
) : (
<span className="font-mono text-[10px] uppercase text-faint">…</span>
)
) : (
<span className="font-mono text-[10px] uppercase text-faint">{photo.status === "FAILED" ? "erro" : "…"}</span>
)}
</span>
<span className="flex w-full items-center justify-between text-[11px]">
<span aria-hidden="true" className={`w-3 ${photo.flag === "REJECT" ? "text-danger" : "text-ink"}`}>
{photo.flag === "PICK" ? "⚑" : photo.flag === "REJECT" ? "✕" : ""}
</span>
<span aria-hidden="true" className="tracking-[1px] text-[#cfcfcf]">
{"★".repeat(photo.rating)}
</span>
<span aria-hidden="true" className="h-2.5 w-2.5 rounded-[2px]" style={{ background: label ?? "transparent" }} />
</span>
{photo.delivery && photo.delivery !== "NONE" && (
<span
title={t.deliveryBadge[photo.delivery]}
aria-label={t.deliveryBadge[photo.delivery]}
className={`absolute left-1.5 top-1 text-[11px] font-bold ${photo.delivery === "DONE" ? "text-ok" : photo.delivery === "FAILED" ? "text-[#f0a35a]" : "text-[#7fb0ff]"}`}
>
{photo.delivery === "DONE" ? "✓" : photo.delivery === "FAILED" ? "!" : "↑"}
</span>
)}
{photo.players.length > 0 && (
<span className="absolute right-1.5 top-1 max-w-[70%] truncate text-[10px] text-muted">{photo.players[0]}</span>
)}
</button>
);
});
export function Grid(props: {
photos: PhotoSummary[];
selected: ReadonlySet<string>;
focus: string | null;
srcFor: (photo: PhotoSummary) => string;
onPick: (id: string, e: React.MouseEvent) => void;
onOpen: (id: string) => void;
}) {
return (
<section aria-label="Grelha" className="grid content-start gap-1 p-1.5" style={{ gridTemplateColumns: "repeat(auto-fill, minmax(168px, 1fr))" }}>
{props.photos.map((photo, index) => (
<Cell
key={photo.id}
photo={photo}
index={index}
src={props.srcFor(photo)}
selected={props.selected.has(photo.id)}
focused={props.focus === photo.id}
onPick={props.onPick}
onOpen={props.onOpen}
/>
))}
</section>
);
}
export interface LoupeFace {
id: string;
x0: number;
y0: number;
x1: number;
y1: number;
label: string | null;
suggested: boolean;
}
export function Loupe({ photo, src, fallback, index, total, faces = [] }: { photo: PhotoSummary; src: string; fallback?: string; index: number; total: number; faces?: LoupeFace[] }) {
// Desktop app: previews seen once show again without a network; never seen, the thumbnail stands in.
const preview = useCachedImage(src, isDesktop());
const thumb = useCachedImage(fallback ?? null, isDesktop() && !!fallback);
const [failed, setFailed] = useState(false);
useEffect(() => setFailed(false), [src]);
const shown = (failed ? thumb : preview) ?? thumb ?? "";
return (
<section aria-label="Lupa" className="relative flex h-full min-h-[420px] items-center justify-center bg-[#121213] p-6">
<span className="relative inline-block max-h-full max-w-full">
<img src={shown} onError={() => setFailed(true)} alt={photo.name} className="block max-h-[calc(100vh-14rem)] max-w-full object-contain shadow-[0_2px_12px_rgba(0,0,0,0.7)]" />
{faces.map((f) => (
<span
key={f.id}
className={`pointer-events-none absolute border ${f.label && !f.suggested ? "border-white/80" : "border-white/40 border-dashed"}`}
style={{ left: `${f.x0 * 100}%`, top: `${f.y0 * 100}%`, width: `${(f.x1 - f.x0) * 100}%`, height: `${(f.y1 - f.y0) * 100}%` }}
>
{f.label && (
<span className="absolute left-0 top-full mt-0.5 whitespace-nowrap bg-black/70 px-1.5 py-0.5 text-[11px] text-white">
{f.suggested ? `${f.label}?` : f.label}
</span>
)}
</span>
))}
</span>
<p className="absolute bottom-2 left-3 font-mono text-[11px] text-faint">
{photo.name} · {index + 1}/{total}
</p>
</section>
);
}