Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123/** * The grid view's data: album cells derived from the *same* flattened rows the * list renders. * * Deriving (rather than reading the album table straight from the store) buys * two properties the click-to-reveal interaction depends on: * - the grid cannot contain an album the list does not have, and * - each cell carries the index of its own album row, so clicking a cover * scrolls the list to exactly that row without a search or a name match. */import type { LibraryRow } from "./rows";
export interface GridAlbum { /** Same identity the list gives its album row — stable for <For>. */ key: string; /** Index into the LibraryRow[] these cells came from; the list scrolls here. */ rowIndex: number; name: string; artist: string; year?: number; songCount: number; /** What to hand getCoverArt: the album's own art id, else its album id. */ coverId?: string;}
export function albumCells(rows: LibraryRow[]): GridAlbum[] { const out: GridAlbum[] = []; rows.forEach((r, rowIndex) => { if (r.kind !== "album") return; out.push({ key: r.key, rowIndex, name: r.label, artist: r.artist, year: r.year, songCount: r.songs, coverId: r.coverArt ?? r.albumId, }); }); return out;}
/** * Where a cell's album section row lives, for click-to-reveal. Normally the * `rowIndex` the cell was built with is right, but a harvest can rebuild the * rows between the cover being rendered and being clicked — so look the row up * by key, and fall back to artist + title (which the sort is built on anyway) * rather than doing nothing when the keys have diverged. */export function revealRowIndex(rows: LibraryRow[], cell: GridAlbum): number { const exact = rows.findIndex((r) => r.kind === "album" && r.key === cell.key); if (exact >= 0) return exact; return rows.findIndex( (r) => r.kind === "album" && r.label === cell.name && r.artist === cell.artist, );}
/** * Column count for a container of `width` px, given a target cell width. Floors * to at least one column so a zero-width (hidden / unmeasured) container still * renders a single column instead of nothing. */export function gridColumns(width: number, cellWidth: number, gap: number): number { if (!Number.isFinite(width) || width <= 0) return 1; return Math.max(1, Math.floor((width + gap) / (cellWidth + gap)));}
/** Split cells into rows of `columns`, padding the last row with nothing. */export function gridRows(cells: GridAlbum[], columns: number): GridAlbum[][] { const cols = Math.max(1, columns); const out: GridAlbum[][] = []; for (let i = 0; i < cells.length; i += cols) out.push(cells.slice(i, i + cols)); return out;}
/** * Whether two cells say the same thing. * * Every field the tile wears, and nothing else: `key` is the album's own * identity, `rowIndex` is where the list keeps it, and the rest is what the * caption and the cover are drawn from. A field outside this set cannot change * what the gallery shows, so it must not be allowed to rebuild it. */export function sameCell(a: GridAlbum, b: GridAlbum): boolean { return ( a.key === b.key && a.rowIndex === b.rowIndex && a.name === b.name && a.artist === b.artist && a.year === b.year && a.songCount === b.songCount && a.coverId === b.coverId );}
/** * Whether two window layouts say the same thing — the `equals` that keeps * `<For>` from tearing the gallery down. * * `gridRows` returns a brand-new array per row, and `<For>` keys rows by array * identity, so any recomputation of the cells — a harvest page landing, a * favorite syncing, the observer measuring a width that yields the same column * count — disposed every row in the window and built it again. For an album * grid that is the expensive kind of nothing: each rebuilt cell is a new * `<img>`, and each new `<img>` is another `getCoverArt` for a cover already on * the screen a frame ago. That is what read as the gallery refreshing itself. * * Comparing the fields rather than the cell objects is the whole point: the * cells arrive as fresh objects from `albumCells` on every pass, so identity * would agree with anything and content is the question. */export function sameRows(a: GridAlbum[][], b: GridAlbum[][]): boolean { if (a === b) return true; if (a.length !== b.length) return false; for (let i = 0; i < a.length; i++) { const x = a[i]!; const y = b[i]!; if (x.length !== y.length) return false; for (let j = 0; j < x.length; j++) if (!sameCell(x[j]!, y[j]!)) return false; } return true;}