Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | 2x 213x 5x 5x 4x 5x 3x 76x 76x 76x 76x 76x 76x 31x 76x 27x 27x 76x 1x 1x 76x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 6x 18x 6x 6x 1x 1x 5x 5x 9x 76x 76x 76x 5x 5x 5x 5x 5x 5x 5x 2x 2x 3x 3x 8x 3x 3x 5x 5x 5x 76x 213x 213x 213x 27x | "use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { getPoolPhotoAssetUrl, PHOTO_WIDTH } from "@/lib/photos";
import type { Photo } from "@/types";
import { PoolActionBar, type MoveDestination, type MoveTarget } from "./PoolActionBar";
type PoolPhoto = Pick<Photo, "id" | "filename">;
// A success notice that offers a link to the thing it just created is an
// offer, not a redirect — it stays up until the person acts on it or moves
// on (toggle/group/each/move/clear), never auto-dismissed. A notice with no
// href is a plain confirmation and keeps the old 4s auto-dismiss. When href
// is set, the WHOLE sentence is the link's accessible name — a repo copy
// rule against several links sharing one bare accessible name (e.g. "take a
// look.") on the same page.
type Notice = { text: string; href?: string };
function alt(filename: string): string {
return filename.replace(/\.[^.]+$/, "").replace(/[-_]/g, " ");
}
// Reads a failed response's `error` field (the errorResponse contract — see
// src/server/api/http-errors.ts; there is no `message` field) so the UI can
// surface the server's own warm copy instead of a generic fallback. A body
// that isn't JSON, or has no string `error`, falls back quietly.
async function serverErrorMessage(res: Response, fallback: string): Promise<string> {
try {
const body: unknown = await res.json();
const message = (body as { error?: unknown })?.error;
if (typeof message === "string" && message) return message;
} catch {
// non-JSON or empty body — use the fallback below
}
return fallback;
}
export function PoolPhotoGrid({
collectionId,
photos,
targets = [],
}: {
collectionId: string;
photos: PoolPhoto[];
targets?: MoveTarget[];
}) {
const [pool, setPool] = useState<PoolPhoto[]>(photos);
const [selected, setSelected] = useState<string[]>([]); // selection order: [0] becomes hero
const [busy, setBusy] = useState(false);
const [notice, setNotice] = useState<Notice | null>(null);
const [error, setError] = useState<string | null>(null);
// A link-bearing notice is an offer to follow up, not a toast — it stays
// until the next selection change or action-bar action clears it (see
// toggle/clear/groupPool/moveTo below). A plain notice keeps the old
// quiet auto-dismiss so it never lingers.
useEffect(() => {
Eif (!notice || notice.href) return;
const timer = setTimeout(() => setNotice(null), 4000);
return () => clearTimeout(timer);
}, [notice]);
const toggle = (id: string) => {
setNotice(null);
setSelected((prev) => (prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id]));
};
const clear = () => {
setNotice(null);
setSelected([]);
};
// `mode: "each"` splits the selection into one item per photo; the default
// groups them all into a single item (whose number the success note surfaces).
const groupPool = async (mode?: "each") => {
Iif (selected.length === 0 || busy) return;
setBusy(true);
setError(null);
setNotice(null);
try {
const res = await fetch(`/api/collections/${collectionId}/pool/group`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(mode ? { photoIds: selected, mode } : { photoIds: selected }),
});
const fallback =
mode === "each"
? "We couldn’t make those items just now — your photos are safe; try again."
: "We couldn’t make that item just now — your photos are safe; try again.";
if (!res.ok) {
setError(await serverErrorMessage(res, fallback));
return;
}
const grouped = new Set(selected);
setPool((prev) => prev.filter((p) => !grouped.has(p.id)));
setSelected([]);
if (mode === "each") {
const data = (await res.json()) as { count: number };
setNotice({
text: `${data.count} items are ready — see them all.`,
href: `/collections/${collectionId}`,
});
} else {
const data = (await res.json()) as { number: number };
setNotice({
text: `Item #${data.number} is ready — take a look.`,
href: `/collections/${collectionId}/items/${data.number}`,
});
}
} catch {
setError(
mode === "each"
? "We couldn’t make those items just now — your photos are safe; try again."
: "We couldn’t make that item just now — your photos are safe; try again.",
);
} finally {
setBusy(false);
}
};
const makeItem = () => groupPool();
const makeEach = () => groupPool("each");
const moveTo = async (dest: MoveDestination) => {
Iif (selected.length === 0 || busy) return;
setBusy(true);
setError(null);
setNotice(null);
try {
const res = await fetch(`/api/collections/${collectionId}/photos/reassign`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ photoIds: selected, destination: dest }),
});
if (!res.ok) {
setError(await serverErrorMessage(res, "We couldn’t move those just now — your photos are safe; try again."));
return;
}
const data = (await res.json()) as { number: number | null };
const moved = new Set(selected);
setPool((prev) => prev.filter((p) => !moved.has(p.id)));
setSelected([]);
const base = dest.type === "new" ? "Now their own item" : "Moved";
const href = data.number != null ? `/collections/${collectionId}/items/${data.number}` : undefined;
setNotice({ text: href ? `${base} — take a look.` : `${base}.`, href });
} catch {
setError("We couldn’t move those just now — your photos are safe; try again.");
} finally {
setBusy(false);
}
};
return (
<div className="space-y-4">
{notice && (
<p className="rounded-lg bg-success-soft px-4 py-2 text-success-deep" role="status">
{notice.href ? (
<Link href={notice.href} className="font-medium underline">
{notice.text}
</Link>
) : (
notice.text
)}
</p>
)}
{error && (
<p className="rounded-lg bg-error-soft px-4 py-2 text-error-deep" role="alert">
{error}
</p>
)}
<div className="grid gap-3 grid-cols-[repeat(auto-fill,minmax(150px,1fr))]">
{pool.map((p) => {
const isSelected = selected.includes(p.id);
const isCover = selected[0] === p.id;
return (
<button
key={p.id}
type="button"
aria-pressed={isSelected}
onClick={() => toggle(p.id)}
className={`relative aspect-square min-h-11 overflow-hidden rounded-lg border-2 bg-surface-raised outline-hidden transition focus-visible:ring-2 focus-visible:ring-primary ${
isSelected ? "border-primary" : "border-transparent"
}`}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={getPoolPhotoAssetUrl(collectionId, p.id, { w: PHOTO_WIDTH.poolTile })}
alt={alt(p.filename)}
loading="lazy"
className="h-full w-full object-cover"
/>
{isSelected && isCover && (
// Unlike the plain ✓, "Cover" carries information aria-pressed
// doesn't (WHICH photo becomes the item's hero) — not aria-hidden.
<span className="absolute left-1.5 top-1.5 rounded-full bg-spark px-2 py-0.5 text-xs font-medium text-spark-ink shadow-sm">
Cover
</span>
)}
{isSelected && !isCover && (
<span
aria-hidden
className="absolute left-1.5 top-1.5 flex h-6 w-6 items-center justify-center rounded-full bg-primary text-sm text-primary-ink shadow-sm"
>
✓
</span>
)}
</button>
);
})}
</div>
<PoolActionBar
count={selected.length}
busy={busy}
targets={targets}
onMakeItem={makeItem}
onMakeEach={makeEach}
onMove={moveTo}
onClear={clear}
/>
</div>
);
}
|