Build the gallery: virtualised grid, filters and a full-size viewer

The client half. The gallery now reads real captures, pages by cursor and
loads thumbnails through the auth-checked route, so an image is only ever
served to a request that carried a valid session.

Virtualised because the gallery is the one screen that grows without bound.
5000 captures a day is past a million DOM nodes inside a year, and the browser
gives up long before the database does. react-virtual virtualises a list
rather than a grid, so the grid is a list of rows whose column count is
computed from the measured container width - which also makes it responsive
without a media-query ladder, and is why the width is observed rather than
assumed. A ResizeObserver rather than a window resize listener, because the
filter panel changes that element's width without the window changing at all.

Filters cover what the server can answer: date range, device, kept, reviewed,
pre-trigger. Taxon and tag are deliberately absent until taxa is seeded and
the observation editor exists, for the same reason they are absent from the
endpoint. The triage filters are tri-state selects rather than checkboxes,
because "kept" has three meanings - everything, only kept, only rejected - and
a checkbox expresses two of them.

Filters are part of the query key, so changing one starts fresh pagination
instead of appending. A cursor from the previous filter set describes a
position in a list that no longer exists.

The viewer is keyboard-driven on purpose. Labelling a day of captures is
look-decide-next a few hundred times, and that loop is only bearable from the
keyboard. It steps only through what is already loaded, and the button is
disabled at the edge rather than showing a blank frame while a page arrives.

Two contract details the UI has to get right, both now carried in code with
the reason attached: captured_at_device is null when the camera's clock was
unsynced and is rendered as "not synced" rather than as 1970 (API.md,
X-Captured-At: 0), and a model-sourced observation is badged as a prediction,
because that table is also the training data for any future model and a
prediction shown as a human label would poison it.

has_thumb is why the summary carries it: the grid shows a placeholder for a
capture whose thumbnail job has not run, instead of a broken image.

Conventions held: no user-facing string literal in a component - 60-odd new
keys in en.ts and de.ts, and the typed catalogue makes a missing German string
a build error rather than English in front of a German user. No colour literal
outside tokens.css either; the one place that wanted rgba() uses color-mix
against a token instead. 36 contrast pairs across three themes still pass.

Verified against the real stack with 506 real captures through the Vite proxy,
which is the path the browser takes: 9 pages of 60 following the cursor, all
506 seen, zero duplicates across pages that include real burst timestamp ties,
0.28s for the lot, and 12 of 12 sampled thumbnails arriving as valid WebP.

Not verified: how it looks. I have no browser here, so the layout, the scroll
behaviour and the virtualisation actually recycling rows are unconfirmed by
eye. Typecheck, build and the data path are green; the visual pass is yours.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Thomas KarnerandClaude Opus 5 committed 2026-09-21 19:36:47 +02:00
1 parent 51750e1644
commit 29c8cf6bfe
12 files changed
+1197 -18

No files matched your search

+111 -6
View File
@@ -1,16 +1,121 @@
import { useInfiniteQuery } from "@tanstack/react-query";
import { useMemo, useState } from "react";
import { CaptureGrid } from "../components/CaptureGrid";
import { CaptureViewer } from "../components/CaptureViewer";
import { GalleryFilters } from "../components/GalleryFilters";
import type { CaptureFilters } from "../components/GalleryFilters";
import { EmptyState } from "../components/EmptyState";
import { useT } from "../i18n/I18nProvider";
import { ApiError, listCaptures } from "../lib/api";
/* The gallery. Phase 2 gives it something to show; until /captures exists
* there is nothing to fetch, so this is honestly empty rather than filled
* with fake rows that would hide layout problems. */
/* The gallery.
*
* Paged by cursor rather than page number, matching the server: at 5000
* captures a day OFFSET gets slower the further back you scroll, which is the
* direction a gallery scrolls.
*
* The filters are part of the query key, so changing one starts a fresh
* pagination rather than appending to the previous result -- a cursor from the
* old filter set describes a position in a list that no longer exists.
*/
export function GalleryPage() {
const t = useT();
const [filters, setFilters] = useState<CaptureFilters>({});
const [openIndex, setOpenIndex] = useState<number | null>(null);
const query = useInfiniteQuery({
queryKey: ["captures", filters],
queryFn: ({ pageParam }) => listCaptures(filters, pageParam),
initialPageParam: null as string | null,
getNextPageParam: (lastPage) => lastPage.next_cursor,
staleTime: 30_000,
});
const captures = useMemo(
() => query.data?.pages.flatMap((page) => page.items) ?? [],
[query.data]
);
if (query.isPending) {
return (
<section>
<h1 className="page-title">{t("gallery.title")}</h1>
<p className="muted">{t("common.loading")}</p>
</section>
);
}
if (query.isError) {
const error = query.error;
const message =
error instanceof ApiError && error.status === 0
? t("system.unreachable.body")
: t("common.error");
return (
<section>
<h1 className="page-title">{t("gallery.title")}</h1>
<p className="alert alert-danger" role="alert">
{message}
</p>
<button className="button" type="button" onClick={() => void query.refetch()}>
{t("common.retry")}
</button>
</section>
);
}
// Devices to offer in the filter come from what has actually been loaded.
// A device list endpoint is Phase 4; until then this is honest about only
// knowing the cameras it has seen captures from.
const devices = [...new Set(captures.map((capture) => capture.device_id))]
.sort((a, b) => a - b)
.map((id) => ({ id, name: `#${id}` }));
const filtersActive = Object.values(filters).some((value) => value !== undefined);
const open = openIndex === null ? null : (captures[openIndex] ?? null);
return (
<section>
<h1 className="page-title">{t("gallery.title")}</h1>
<EmptyState title={t("gallery.empty.title")} body={t("gallery.empty.body")} />
<section className="gallery">
<header className="gallery-header">
<h1 className="page-title">{t("gallery.title")}</h1>
<span className="muted">
{t("gallery.count", { shown: captures.length, total: captures.length })}
</span>
</header>
<GalleryFilters filters={filters} onChange={setFilters} devices={devices} />
{captures.length === 0 ? (
<EmptyState
title={
filtersActive ? t("gallery.empty.filtered.title") : t("gallery.empty.title")
}
body={filtersActive ? t("gallery.empty.filtered.body") : t("gallery.empty.body")}
/>
) : (
<CaptureGrid
captures={captures}
hasMore={query.hasNextPage}
isLoadingMore={query.isFetchingNextPage}
onLoadMore={() => void query.fetchNextPage()}
onOpen={(capture) =>
setOpenIndex(captures.findIndex((candidate) => candidate.id === capture.id))
}
/>
)}
{open !== null && openIndex !== null && (
<CaptureViewer
capture={open}
onClose={() => setOpenIndex(null)}
onPrevious={openIndex > 0 ? () => setOpenIndex(openIndex - 1) : null}
// Only as far as what is loaded. Stepping past the last loaded
// capture would show a blank frame while a page arrives, so the
// button is simply disabled there.
onNext={openIndex < captures.length - 1 ? () => setOpenIndex(openIndex + 1) : null}
/>
)}
</section>
);
}