import { useEffect, useState } from 'react'; import { normalizeEdits } from '../lib/edits'; import { getBitmap } from '../lib/images'; import { Renderer, rotatedBounds } from '../gl/renderer'; import type { PhotoRecord, Preset } from '../types'; /** * Preset thumbnails are rendered once per photo/preset pair and cached as small * JPEG data URLs. One shared Renderer does all of them: a WebGL context per tile * would exhaust the browser's context limit long before 70 presets. */ const cache = new Map(); const CACHE_LIMIT = 900; const THUMB_EDGE = 200; /** The crop is part of the key so a turn, straighten or flip refreshes the tiles. */ const keyOf = (photoId: string, presetId: string, crop: string) => `${photoId}|${presetId}|${crop}`; export function usePresetThumbs(photo: PhotoRecord, presets: Preset[]): Record { const [thumbs, setThumbs] = useState>({}); // Preset edits never change after creation, so a stable id list is enough to // avoid re-running for every parent render. const presetKey = presets.map((p) => p.id).join('|'); const crop = photo.edits.crop; const cropKey = [crop.x, crop.y, crop.w, crop.h, crop.angle, crop.quarter, crop.flipH, crop.flipV].join(','); useEffect(() => { let alive = true; let renderer: Renderer | null = null; const canvas = document.createElement('canvas'); const known: Record = {}; for (const preset of presets) { const hit = cache.get(keyOf(photo.id, preset.id, cropKey)); if (hit) known[preset.id] = hit; } setThumbs(known); const pending = presets.filter((p) => !known[p.id]); if (!pending.length) return; (async () => { try { const bitmap = await getBitmap(photo); if (!alive) return; renderer = new Renderer(canvas); // Every tile shows the photo as it is framed now, so a turned photo's // presets are turned too. const frame = rotatedBounds(bitmap.width, bitmap.height, crop.angle, crop.quarter); const cropW = Math.max(1, frame.w * crop.w); const cropH = Math.max(1, frame.h * crop.h); const scale = THUMB_EDGE / Math.max(cropW, cropH); const width = Math.max(1, Math.round(cropW * scale)); const height = Math.max(1, Math.round(cropH * scale)); renderer.setImage(bitmap); for (const preset of pending) { if (!alive) break; try { renderer.render({ ...normalizeEdits(preset.edits), crop }, { width, height, upscale: Math.max(width / cropW, height / cropH) > 1.05, }); const url = canvas.toDataURL('image/jpeg', 0.72); cache.set(keyOf(photo.id, preset.id, cropKey), url); setThumbs((prev) => ({ ...prev, [preset.id]: url })); } catch { // One bad preset must not stop the rest of the grid. } // Yield between tiles so the panel fills in progressively. await new Promise((resolve) => setTimeout(resolve, 0)); } if (cache.size > CACHE_LIMIT) { for (const key of [...cache.keys()].slice(0, cache.size - CACHE_LIMIT)) cache.delete(key); } } catch { // No bitmap yet (raw not synced) or no WebGL: tiles keep their placeholders. } })(); return () => { alive = false; renderer?.dispose(); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [photo.id, presetKey, cropKey]); return thumbs; }