import { record, safeImageUrl } from './catalog'; interface Preview { id: number; thumb: string; full: string; label: string } interface RecentRun { id: string; label: string; workflow: string; date: string; status: string; previews: Preview[]; requested: number | null } type Translate = (text: string) => string; export const parseRecentRuns = (rows: unknown, origin: string): RecentRun[] => { if (!Array.isArray(rows)) return []; const seen = new Set(); return rows.slice(0, 12).flatMap((value): RecentRun[] => { const row = record(value); if (typeof row.runId !== 'string' || !/^[a-zA-Z0-9_-]{1,64}$/.test(row.runId) || seen.has(row.runId) || row.status === 'removed') return []; seen.add(row.runId); const ids = new Set(); const previews = (Array.isArray(row.previewAttachments) ? row.previewAttachments : []).slice(0, 3).flatMap((value): Preview[] => { const image = record(value); const thumb = safeImageUrl(image.thumbUrl, origin); const id = Number(image.attachmentId); if (!Number.isSafeInteger(image.attachmentId) || id <= 0 || ids.has(id) || !thumb) return []; ids.add(id); return [{ id, thumb, full: safeImageUrl(image.fullUrl, origin) || thumb, label: typeof image.label === 'string' ? image.label.slice(0, 240) : '' }]; }); const requested = Number.isSafeInteger(row.totalOutputs) && Number(row.totalOutputs) > 0 ? Number(row.totalOutputs) : null; return [{ id: row.runId, label: typeof row.label === 'string' ? row.label.slice(0, 300) : '', workflow: typeof row.workflow === 'string' ? row.workflow : '', date: typeof row.createdLabel === 'string' ? row.createdLabel.slice(0, 100) : '', status: typeof row.status === 'string' ? row.status : '', previews, requested }]; }).slice(0, 6); }; /** Chronological activity; missing previews never imply that a source image is a result. */ export const renderRecent = (root: HTMLElement, rows: unknown, historyUrl: string, studioUrl: string, translate: Translate, state: 'ready' | 'unavailable' = 'ready'): void => { const runs = parseRecentRuns(rows, window.location.origin); const node = (tag: string, className = '', text = ''): HTMLElement => { const element = document.createElement(tag); element.className = className; element.textContent = text; return element; }; const icon = (name: string): HTMLElement => { const element = node('span', 'fotogenic-ai-icon fotogenic-ai-icon--' + name); element.setAttribute('aria-hidden', 'true'); return element; }; const link = (text: string, href: string, className = ''): HTMLAnchorElement => { const anchor = document.createElement('a'); anchor.className = className; anchor.textContent = text; anchor.href = href; return anchor; }; const runTitle = (run: RecentRun): string => { if (run.workflow === 'product_images') return translate('Generate Images'); if (run.workflow === 'campaign_set') return translate('Campaign Set'); if (run.workflow === 'increase_resolution') return translate('Increase Resolution'); return run.label || translate('Recent run'); }; const status = (run: RecentRun): string => { if (['running', 'queued', 'processing', 'pending'].includes(run.status)) return translate('In progress'); if (run.status === 'failed') return translate('Needs attention'); if (['stopped', 'cancelled'].includes(run.status)) return translate('Stopped'); if (run.status === 'partial') return translate('Partially completed'); return run.previews.length ? translate('Images saved') : translate('Preview unavailable'); }; root.replaceChildren(); root.className = 'fotogenic-ai-home-activity'; if (!runs.length) { const empty = node('div', 'fotogenic-ai-home-activity-empty'); const unavailable = state === 'unavailable'; empty.append(icon(unavailable ? 'warning-circle' : 'image-square'), node('strong', '', translate(unavailable ? 'Recent activity is temporarily unavailable' : 'Your next creation starts here')), node('p', '', translate(unavailable ? 'Open History to check your saved runs.' : 'Your saved images and recent work will appear here.')), link(translate(unavailable ? 'Open History' : 'Open Studio'), unavailable ? historyUrl : studioUrl, 'fotogenic-ai-home-history-link')); root.append(empty); return; } const list = node('ul', 'fotogenic-ai-home-activity-list'); runs.forEach((run) => { const url = new URL(historyUrl, window.location.href); url.searchParams.set('run', run.id); const item = node('li'); const anchor = link('', url.href, 'fotogenic-ai-home-activity-row'); const preview = run.previews[0]; const thumb = node('span', 'fotogenic-ai-home-activity-thumb'); if (preview) { const image = document.createElement('img'); image.src = preview.thumb; image.alt = ''; image.width = 48; image.height = 48; image.loading = 'lazy'; image.decoding = 'async'; image.referrerPolicy = 'no-referrer'; image.addEventListener('error', () => { thumb.replaceChildren(icon('image-square')); thumb.setAttribute('aria-label', translate('Preview unavailable')); }, { once: true }); thumb.append(image); } else thumb.append(icon(run.status === 'failed' ? 'warning-circle' : 'image-square')); const copy = node('span', 'fotogenic-ai-home-activity-row-copy'); copy.append(node('strong', '', preview?.label || runTitle(run))); if (preview?.label) copy.append(node('small', '', runTitle(run))); const date = node('span', 'fotogenic-ai-home-activity-date', run.date); const action = node('span', 'fotogenic-ai-home-activity-action', translate(preview ? 'Open results' : 'View details')); action.append(icon('arrow-right')); copy.append(date); const stats = node('span', 'fotogenic-ai-home-activity-stats'); const knownStatus = run.status === 'completed' ? translate('Completed') : ['running', 'queued', 'processing', 'pending', 'failed', 'stopped', 'cancelled', 'partial'].includes(run.status) ? status(run) : ''; if (knownStatus) stats.append(node('span', '', knownStatus)); if (run.requested !== null) stats.append(node('span', '', `${run.requested} ${translate(run.requested === 1 ? 'image requested' : 'images requested')}`)); anchor.append(thumb, copy, stats, action); item.append(anchor); list.append(item); }); root.append(list); };