import './catalog-loading.css'; import { createLoadingState } from '../feedback/loading-state'; /** Decorative placeholders use the same grid as the eventual catalog cards. */ export const createCatalogSkeleton = (gridClass: string, count = 10, kind?: 'models' | 'environments'): HTMLElement => { const grid = document.createElement('div'); grid.className = gridClass; grid.setAttribute('aria-hidden', 'true'); for (let index = 0; index < Math.min(24, Math.max(1, count)); index++) { const card = document.createElement('div'); card.className = 'fotogenic-ai-catalog-skeleton'; card.setAttribute('aria-hidden', 'true'); if (kind) card.dataset.kind = kind; const image = document.createElement('span'); image.className = 'fotogenic-ai-catalog-skeleton-image'; const caption = document.createElement('span'); caption.className = 'fotogenic-ai-catalog-skeleton-caption'; const line = document.createElement('span'); caption.append(line); card.append(image, caption); grid.append(card); } return grid; }; /** Reserve only the next visible rows, capped by the request's actual page capacity. */ export const catalogAppendCount = (grid: HTMLElement, cardSelector: string, pageSize = 24, remaining = pageSize): number => { const style = window.getComputedStyle(grid); const tracks = style.gridTemplateColumns?.trim(); let columns = tracks && tracks !== 'none' ? Math.max(1, tracks.split(/\s+/).length) : 2; if (style.gridAutoFlow?.includes('column')) { const width = grid.querySelector(cardSelector)?.getBoundingClientRect().width || 0; const gap = Number.parseFloat(style.columnGap) || 0; if (width > 0) columns = Math.max(1, Math.round((grid.clientWidth + gap) / (width + gap))); } return Math.max(0, Math.min(24, pageSize, remaining, columns * 2)); }; /** Nonvirtual grids retain their real cards while pending slots follow their viewport. */ export const createCatalogAppendSkeleton = (grid: HTMLElement, { cardSelector, kind, pageSize = 24 }: { cardSelector: string; kind?: 'models' | 'environments'; pageSize?: number; }): { sync: (busy: boolean, remaining?: number) => void; clear: () => void; destroy: () => void } => { let active = false, remaining = pageSize; let cards: HTMLElement[] = []; const clear = (): void => { active = false; cards.forEach(card => card.remove()); cards = []; }; const render = (): void => { if (!active) return; const count = catalogAppendCount(grid, cardSelector, pageSize, remaining); if (cards.length === count && cards.every(card => card.parentElement === grid)) return; cards.forEach(card => card.remove()); cards = count ? Array.from(createCatalogSkeleton('', count, kind).children) as HTMLElement[] : []; cards.forEach(card => { card.dataset.faCatalogAppendSkeleton = ''; grid.append(card); }); }; const observer = new ResizeObserver(render); observer.observe(grid); return { sync(busy, count = pageSize) { if (!busy) { clear(); return; } active = true; remaining = count; render(); }, clear, destroy() { clear(); observer.disconnect(); }, }; }; /** One accessible status accompanies decorative cards only while discovery is cold. */ export const createCatalogLoading = (translate: (text: string) => string, gridClass: string, kind?: 'models' | 'environments', statusPresentation: 'visible' | 'screen-reader' = 'visible', count = 10): HTMLDivElement => { const state = document.createElement('div'); state.className = 'fotogenic-ai-catalog-loading'; const cards = createCatalogSkeleton(gridClass, count, kind); cards.dataset.faCatalogSkeleton = ''; if (kind) cards.dataset.kind = kind; const status = statusPresentation === 'visible' ? createLoadingState(translate) : document.createElement('span'); if (statusPresentation === 'screen-reader') { status.className = 'screen-reader-text'; status.setAttribute('role', 'status'); status.textContent = translate('Loading'); } state.append(cards, status); return state; }; /** Reuse server artwork without hiding images the browser already loaded. */ export const hydrateCatalogPreview = (wrap: HTMLElement, unavailable: string): void => { const image = wrap.querySelector('img'); if (!image) return; const loaded = (): void => { wrap.classList.remove('is-loading'); }; const failed = (): void => { loaded(); wrap.textContent = unavailable; }; image.addEventListener('load', loaded, { once: true }); image.addEventListener('error', failed, { once: true }); if (image.complete) { if (image.naturalWidth > 0) loaded(); else failed(); } else wrap.classList.add('is-loading'); }; /** Only genuinely pending artwork needs a placeholder. Visible cards load eagerly. */ export const createCatalogPreview = (src: string, unavailable: string, eager = false): HTMLElement => { const wrap = document.createElement('span'); wrap.className = 'fotogenic-ai-catalog-preview'; if (!src) { wrap.textContent = unavailable; return wrap; } const image = document.createElement('img'); image.alt = ''; image.loading = eager ? 'eager' : 'lazy'; image.decoding = 'async'; image.referrerPolicy = 'no-referrer'; wrap.append(image); image.src = src; hydrateCatalogPreview(wrap, unavailable); return wrap; };