// Justified-rows + square-grid layout, with optional month grouping. Pure // geometry: takes a container width, returns positions for every tile and // every header. Called by virtualisation on first render and on resize. import { state, TARGET_ROW_HEIGHT, GAP, MOBILE_BREAKPOINT, MOBILE_COLS, HEADER_HEIGHT, GROUP_GAP, type LayoutEntry, type HeaderEntry, type GroupSpec } from './state.js' import type { GroupByDateMode } from '../shared/types.js' export interface LayoutResult { layout: LayoutEntry[] headers: HeaderEntry[] totalHeight: number } /** * Compute tile positions, group-header positions, and total scroll height * for the gallery at the given container width. * * Reads `state.items` and `state.groupByDate`. Output is pure data - the * caller (virtualisation) is responsible for actually placing DOM nodes * according to the returned positions. * * Switches between a mobile square grid (under MOBILE_BREAKPOINT) and the * justified-rows algorithm. When grouping by date is enabled, items are * bucketed by YYYY-MM and a header is reserved before each group. */ export function computeLayout (containerW: number): LayoutResult { const tileLayout: LayoutEntry[] = new Array(state.items.length) const newHeaders: HeaderEntry[] = [] const groups: GroupSpec[] = state.groupByDate ? groupItemsByDate(state.groupByDate) : [{ label: null, indices: itemIndices() }] const isMobile = containerW < MOBILE_BREAKPOINT let y = 0 for (let g = 0; g < groups.length; g++) { const group = groups[g] if (group.label) { newHeaders.push({ label: group.label, top: y, height: HEADER_HEIGHT }) y += HEADER_HEIGHT } y = isMobile ? layoutSquareGroup(containerW, group.indices, y, tileLayout) : layoutJustifiedGroup(containerW, group.indices, y, tileLayout) if (g < groups.length - 1) y += GROUP_GAP } return { layout: tileLayout, headers: newHeaders, totalHeight: Math.max(0, y) } } function itemIndices (): number[] { const out = new Array(state.items.length) for (let i = 0; i < state.items.length; i++) out[i] = i return out } function groupItemsByDate (mode: GroupByDateMode): GroupSpec[] { // Bucket key length: YYYY-MM (month) or YYYY-MM-DD (day). We slice the local // timestamp, so no timezone maths is needed - the string already reads as // the photographer's wall-clock (matching Immich's own timeline grouping). const keyLen = mode === 'day' ? 10 : 7 // Preserve item order (already sorted desc by builder.ts when grouping is on) const map = new Map() for (let i = 0; i < state.items.length; i++) { const item = state.items[i] const key = (item.localDateTime || item.fileCreatedAt || '').slice(0, keyLen) || 'undated' let g = map.get(key) if (!g) { g = { label: dateLabel(key, mode), indices: [] } map.set(key, g) } g.indices.push(i) } // When every item is undated (typically because the share's "Show metadata" // toggle stripped dates upstream), drop the lone "Undated" header if (map.size === 1 && map.has('undated')) { map.get('undated')!.label = null } return Array.from(map.values()) } function dateLabel (key: string, mode: GroupByDateMode): string { if (key === 'undated') return 'Undated' const parts = key.split('-') const y = Number(parts[0]) const m = Number(parts[1]) const d = Number(parts[2]) if (!y || !m) return key // Intl.DateTimeFormat picks up the browser's locale; UTC timeZone keeps the // displayed date consistent with the bucket key (which is already local). return new Intl.DateTimeFormat(undefined, { year: 'numeric', month: 'long', ...(mode === 'day' ? { day: 'numeric' } : {}), timeZone: 'UTC' }).format(new Date(Date.UTC(y, m - 1, mode === 'day' ? (d || 1) : 1))) } /** * Lay out one group as a fixed-column square grid (mobile path). * Writes positions into `tileLayout` in place. * * @returns The y position past the last tile in this group (no trailing gap). */ function layoutSquareGroup (containerW: number, indices: number[], startY: number, tileLayout: LayoutEntry[]): number { const tileSize = Math.floor((containerW - (MOBILE_COLS - 1) * GAP) / MOBILE_COLS) let col = 0 let x = 0 let y = startY for (const idx of indices) { tileLayout[idx] = { index: idx, left: x, top: y, width: tileSize, height: tileSize } col++ if (col === MOBILE_COLS) { col = 0 x = 0 y += tileSize + GAP } else { x += tileSize + GAP } } // If the last row was partial, advance y to past it if (col > 0) y += tileSize // Otherwise back off the trailing inter-row gap else if (y > startY) y -= GAP return y } /** * Lay out one group as justified rows (desktop path): tiles in each row are * scaled so their combined width fills the container exactly. The final row * keeps the target height instead of stretching to fill, which avoids the * "huge last row" look when there are only one or two trailing tiles. * * @returns The y position past the last tile in this group (no trailing gap). */ function layoutJustifiedGroup (containerW: number, indices: number[], startY: number, tileLayout: LayoutEntry[]): number { let rowItems: Array<{ idx: number, aspect: number }> = [] let aspectSum = 0 let y = startY const applyRow = (rowItems: Array<{ idx: number, aspect: number }>, height: number, isLastRow: boolean) => { const intHeight = Math.floor(height) let x = 0 rowItems.forEach(({ idx, aspect }, i) => { const isFinalInRow = i === rowItems.length - 1 const w = (!isLastRow && isFinalInRow) ? containerW - x : Math.floor(aspect * height) tileLayout[idx] = { index: idx, left: x, top: y, width: w, height: intHeight } x += w + GAP }) y += intHeight + GAP } for (const idx of indices) { const item = state.items[idx] const w = item.width || 1 const h = item.height || 1 const aspect = w / h rowItems.push({ idx, aspect }) aspectSum += aspect const projectedH = (containerW - (rowItems.length - 1) * GAP) / aspectSum if (projectedH <= TARGET_ROW_HEIGHT) { applyRow(rowItems, projectedH, false) rowItems = [] aspectSum = 0 } } if (rowItems.length) applyRow(rowItems, TARGET_ROW_HEIGHT, true) // y advanced past the last row's trailing gap; back off return y > startY ? y - GAP : y }