import { useRef, useState } from 'react'; import { COLOR_LABELS, isEdited, thumbTransform } from '../lib/edits'; import { draggedPhotoIds, endDrag, PHOTO_DRAG, startPhotoDrag } from '../lib/drag'; import { thumbSrc } from '../lib/images'; import type { ColorLabel, Flag, PhotoRecord } from '../types'; import Stars, { ColorSwatches, FlagButtons } from './Stars'; 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; onDelete: () => void; onExport: () => 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; } 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, onDelete, onExport, onImport, onDropFiles, onClearSelection, onReorder, onMarqueeSelect, }: 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 selectedSet = new Set(selected); 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.

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

)}
); } return (
{someSelected ? ( <> {selected.length} selected
) : ( <> 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 && (
)} {photos.map((p) => { const color = COLOR_LABELS.find((c) => c.value === p.color); const isDragging = draggedPhotoIds().includes(p.id); return (
onSelect(p.id, e)} onContextMenu={(e) => onContextMenu(e, p.id)} onDoubleClick={() => onOpen(p.id)} draggable onDragStart={(e) => { const ids = selectedSet.has(p.id) && selected.length > 1 ? selected : [p.id]; startPhotoDrag(ids); e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setData(PHOTO_DRAG, ids.join(',')); e.dataTransfer.setData('text/plain', ids.join(',')); e.currentTarget.classList.add('dragging'); }} onDragEnd={(e) => { e.currentTarget.classList.remove('dragging'); endDrag(); }} onDragOver={(e) => { if (!draggedPhotoIds().length) return; e.preventDefault(); e.stopPropagation(); e.dataTransfer.dropEffect = 'move'; e.currentTarget.classList.add('drop-target'); }} onDragLeave={(e) => e.currentTarget.classList.remove('drop-target')} onDrop={(e) => { const ids = draggedPhotoIds(); e.currentTarget.classList.remove('drop-target'); if (!ids.length) return; e.preventDefault(); e.stopPropagation(); onReorder(ids, p.id); endDrag(); }} >
{p.name}
{p.flag === 'pick' && ✓} {p.flag === 'reject' && ✕} {p.raw && ( RAW )} {isEdited(p.edits) && ( ✎ )}
{color && }
{fmtSize(p.size)} {p.width}×{p.height}
{p.name}
onRate(p.id, n)} /> {p.meta.camera && {p.meta.camera}}
); })}
); }