import type { ContentImage } from '../content/types.ts'; import { preloadImage, type HeadLink } from '../seo.ts'; // Columns of the portfolio grid per screen width, narrowest first: the values // of PortfolioGrid's gridTemplateColumns (base, md, lg) export const GRID_COLUMNS = [ { media: null, columns: 1 }, { media: '(min-width: 48em)', columns: 2 }, { media: '(min-width: 64em)', columns: 3 }, ] as const; type GridColumns = readonly { media: string | null; columns: number; }[]; // 'always': in the first row at every width. { media }: in the first row from // that width on. null: never in the first row. export type FirstRow = 'always' | { media: string } | null; // Whether the tile at this index is in the first row of the grid, and from // which width on. The column count is CSS only, so the build cannot know the // width: it names the media query instead. export function firstRow( index: number, columns: GridColumns = GRID_COLUMNS, ): FirstRow { if (index < columns[0].columns) return 'always'; const breakpoint = columns.find((entry) => index < entry.columns); return breakpoint?.media ? { media: breakpoint.media } : null; } // The preloads React cannot write itself: the first-row tiles that sit in a // , each bound to the query of its export function firstRowPreloads( items: readonly { thumb: ContentImage }[], columns: GridColumns = GRID_COLUMNS, ): HeadLink[] { return items.flatMap((item, index) => { const row = firstRow(index, columns); return row && row !== 'always' ? [preloadImage(item.thumb, { media: row.media })] : []; }); }