import { useEffect, useRef, useState } from 'react'; import { COLOR_LABELS, isEdited } from '../lib/edits'; import { draggedPhotoIds, endDrag, PHOTO_DRAG, startPhotoDrag } from '../lib/drag'; import type { ColorLabel, Flag, PhotoRecord } from '../types'; import Stars, { ColorSwatches, FlagButtons } from './Stars'; import EditedImage from './EditedImage'; interface Props { photos: PhotoRecord[]; totalCount: number; selected: string[]; onSelect: (id: string, e: React.MouseEvent) => void; onContextMenu: (e: React.MouseEvent, id: string) => void; onOpen: (id: string) => void; onRate: (id: string, rating: number) => void; onBulkRate: (rating: number) => void; onBulkFlag: (flag: Flag) => void; onBulkColor: (color: ColorLabel) => void; onSendToImmich?: () => void; immichBusy?: boolean; onDelete: () => void; onExport: () => void; onCompare: () => void; onStack: () => void; onVirtualCopy: () => void; onMerge: () => void; /** Re-decode the selected RAW files with the server's current pipeline. */ onReprocessRaw?: () => void; reprocessBusy?: boolean; onPrint: () => void; stacks: Map; onToggleStack: (id: string) => void; onImport: () => void; onDropFiles: (files: File[]) => void; onClearSelection: () => void; onReorder: (ids: string[], targetId: string | null) => void; /** rubber-band selection: the complete new selection */ onMarqueeSelect: (ids: string[]) => void; /** client proofing picks per photo, shown as a badge */ proofCounts: Map; proofStatus: Map; } function fmtSize(bytes: number) { if (bytes > 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; return `${Math.max(1, Math.round(bytes / 1024))} KB`; } export default function LibraryGrid({ photos, totalCount, selected, onSelect, onContextMenu, onOpen, onRate, onBulkRate, onBulkFlag, onBulkColor, onSendToImmich, immichBusy = false, onDelete, onExport, onCompare, onStack, onVirtualCopy, onMerge, onReprocessRaw, reprocessBusy = false, onPrint, stacks, onToggleStack, onImport, onDropFiles, onClearSelection, onReorder, onMarqueeSelect, proofCounts, proofStatus, }: Props) { const gridRef = useRef(null); const band = useRef<{ sx: number; sy: number; base: string[]; moved: boolean; box: DOMRect } | null>(null); const [bandRect, setBandRect] = useState<{ x: number; y: number; w: number; h: number } | null>(null); const virtualize = photos.length > 140; const [viewWindow, setViewWindow] = useState({ start: 0, end: Math.min(photos.length, 140), top: 0, bottom: 0, }); const selectedSet = new Set(selected); useEffect(() => { if (!virtualize) { setViewWindow({ start: 0, end: photos.length, top: 0, bottom: 0 }); return; } const grid = gridRef.current; if (!grid) return; let frame = 0; const measure = () => { const card = grid.querySelector('.card'); if (!card) { if (viewWindow.start >= photos.length) { setViewWindow({ start: 0, end: Math.min(photos.length, 140), top: 0, bottom: 0 }); } return; } const box = card.getBoundingClientRect(); const style = getComputedStyle(grid); const gapX = Number.parseFloat(style.columnGap) || 12; const gapY = Number.parseFloat(style.rowGap) || 12; const horizontalPadding = (Number.parseFloat(style.paddingLeft) || 0) + (Number.parseFloat(style.paddingRight) || 0); const columns = Math.max( 1, Math.round((grid.clientWidth - horizontalPadding + gapX) / (box.width + gapX)), ); const rowStep = box.height + gapY; const firstRow = Math.max(0, Math.floor(grid.scrollTop / rowStep) - 2); const lastRow = Math.ceil((grid.scrollTop + grid.clientHeight) / rowStep) + 2; const start = Math.min(photos.length, firstRow * columns); const end = Math.min(photos.length, Math.max(start + columns, lastRow * columns)); const totalRows = Math.ceil(photos.length / columns); const top = firstRow * rowStep; const bottom = Math.max(0, totalRows - lastRow) * rowStep; setViewWindow((current) => current.start === start && current.end === end && current.top === top && current.bottom === bottom ? current : { start, end, top, bottom }, ); }; const schedule = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(measure); }; const observer = new ResizeObserver(schedule); observer.observe(grid); grid.addEventListener('scroll', schedule, { passive: true }); schedule(); return () => { cancelAnimationFrame(frame); observer.disconnect(); grid.removeEventListener('scroll', schedule); }; }, [photos.length, virtualize, viewWindow.start]); const someSelected = selected.length > 0; const sharedRating = (() => { if (!someSelected) return 0; const rated = photos.filter((p) => selectedSet.has(p.id)); if (!rated.length) return 0; const first = rated[0].rating; return rated.every((p) => p.rating === first) ? first : 0; })(); if (!photos.length) { return (
e.preventDefault()} onDrop={(e) => { e.preventDefault(); const ids = draggedPhotoIds(); if (ids.length) { onReorder(ids, null); endDrag(); return; } onDropFiles(Array.from(e.dataTransfer.files)); }} ref={gridRef} onPointerDown={(e) => { const grid = gridRef.current; // A band starts on empty grid space; cards keep their own click/drag. if (!grid || e.button !== 0 || e.target !== grid) return; e.preventDefault(); const box = grid.getBoundingClientRect(); band.current = { sx: e.clientX, sy: e.clientY, base: e.shiftKey || e.metaKey || e.ctrlKey ? [...selected] : [], moved: false, box, }; setBandRect({ x: e.clientX - box.left + grid.scrollLeft, y: e.clientY - box.top + grid.scrollTop, w: 0, h: 0 }); grid.setPointerCapture(e.pointerId); }} onPointerMove={(e) => { const grid = gridRef.current; const start = band.current; if (!grid || !start) return; const left = Math.min(start.sx, e.clientX); const top = Math.min(start.sy, e.clientY); const right = Math.max(start.sx, e.clientX); const bottom = Math.max(start.sy, e.clientY); if (Math.abs(e.clientX - start.sx) > 3 || Math.abs(e.clientY - start.sy) > 3) start.moved = true; setBandRect({ x: left - start.box.left + grid.scrollLeft, y: top - start.box.top + grid.scrollTop, w: right - left, h: bottom - top, }); if (!start.moved) return; const hits: string[] = []; for (const el of grid.querySelectorAll('.card')) { const r = el.getBoundingClientRect(); if (r.right > left && r.left < right && r.bottom > top && r.top < bottom) { const id = el.dataset.id; if (id) hits.push(id); } } onMarqueeSelect([...start.base, ...hits.filter((id) => !start.base.includes(id))]); }} onPointerUp={(e) => { const grid = gridRef.current; const start = band.current; band.current = null; setBandRect(null); if (!grid || !start) return; if (grid.hasPointerCapture(e.pointerId)) grid.releasePointerCapture(e.pointerId); if (!start.moved && !start.base.length) onClearSelection(); }} onPointerCancel={() => { band.current = null; setBandRect(null); }} >
{totalCount > 0 ? ( <>

No photos match these filters

Loosen the rating, flag or label filters to see more of this event.

) : ( <>

Your library is empty

Create an event and import a shoot. Photos are stored locally, or on your team server when one is connected.

  1. Import a shoot into an event.
  2. Rate and flag the keepers.
  3. Double-click a photo to open Develop.
  4. Press ? anytime for shortcuts.

Drop JPEG, PNG, TIFF or camera raw files anywhere in this area.

)}
); } return (
{someSelected ? ( <> {selected.length} selected
{onReprocessRaw && ( )} {onSendToImmich && ( )} ) : ( <> Click to select · drag on empty space to marquee · double-click to edit · 1–5 to rate ·{' '} ? for shortcuts
{photos.length} photos )}
{ if (draggedPhotoIds().length) { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; } }} onDrop={(e) => { e.preventDefault(); const ids = draggedPhotoIds(); if (ids.length) { onReorder(ids, null); endDrag(); return; } onDropFiles(Array.from(e.dataTransfer.files)); }} ref={gridRef} onPointerDown={(e) => { const grid = gridRef.current; // A band starts on empty grid space; cards keep their own click/drag. if (!grid || e.button !== 0 || e.target !== grid) return; e.preventDefault(); const box = grid.getBoundingClientRect(); band.current = { sx: e.clientX, sy: e.clientY, base: e.shiftKey || e.metaKey || e.ctrlKey ? [...selected] : [], moved: false, box, }; setBandRect({ x: e.clientX - box.left + grid.scrollLeft, y: e.clientY - box.top + grid.scrollTop, w: 0, h: 0 }); grid.setPointerCapture(e.pointerId); }} onPointerMove={(e) => { const grid = gridRef.current; const start = band.current; if (!grid || !start) return; const left = Math.min(start.sx, e.clientX); const top = Math.min(start.sy, e.clientY); const right = Math.max(start.sx, e.clientX); const bottom = Math.max(start.sy, e.clientY); if (Math.abs(e.clientX - start.sx) > 3 || Math.abs(e.clientY - start.sy) > 3) start.moved = true; setBandRect({ x: left - start.box.left + grid.scrollLeft, y: top - start.box.top + grid.scrollTop, w: right - left, h: bottom - top, }); if (!start.moved) return; const hits: string[] = []; for (const el of grid.querySelectorAll('.card')) { const r = el.getBoundingClientRect(); if (r.right > left && r.left < right && r.bottom > top && r.top < bottom) { const id = el.dataset.id; if (id) hits.push(id); } } onMarqueeSelect([...start.base, ...hits.filter((id) => !start.base.includes(id))]); }} onPointerUp={(e) => { const grid = gridRef.current; const start = band.current; band.current = null; setBandRect(null); if (!grid || !start) return; if (grid.hasPointerCapture(e.pointerId)) grid.releasePointerCapture(e.pointerId); if (!start.moved && !start.base.length) onClearSelection(); }} onPointerCancel={() => { band.current = null; setBandRect(null); }} > {bandRect && (
)} {virtualize && viewWindow.top > 0 && ( ); }