import type { GridAsset } from './shared'; import type { Selection } from './useSelection'; import { useMemo } from 'react'; import { AssetTile } from './AssetTile'; import { justifyRows } from './shared'; import { useContainerWidth } from './useContainerWidth'; // A justified grid over a flat list of assets — albums, search results, a person's photos. // // The timeline uses TimelineGrid instead, because it has to lay out months it has not downloaded yet. This one // always has every asset in hand, so it can lay the whole thing out in one pass. type PhotoGridProps = { assets: GridAsset[]; token: string | null; selection: Selection; rowHeight?: number; gap?: number; empty?: React.ReactNode; }; export const PhotoGrid = ({ assets, token, selection, rowHeight = 210, gap = 4, empty }: PhotoGridProps) => { const [ref, width] = useContainerWidth(); const rows = useMemo( () => width > 0 ? justifyRows({ items: assets, ratioOf: (a) => a.ratio, containerWidth: width, targetHeight: rowHeight, gap }) : [], [assets, width, rowHeight, gap], ); return (
{assets.length === 0 && empty}
{rows.map((row, index) => (
{row.tiles.map(({ item, width: w, height: h }) => ( selection.toggle(id, ev)} /> ))}
))}
); };