import { mountProjectListSkeleton } from './project-list-skeleton.js'; import { workspaceElements } from './workspace-elements.js'; // A row identifies a folder accessible through the current launch's grants. Private // recents supply the activity order only, never the inventory or a grant. export function mountOverview(target, {client, extensionName, text, locale, onImport, onOverview, onOpen, showEmptyProjectMessage = true}) { const document = target.ownerDocument; const {icon} = workspaceElements(document); function element(tag, label, className) { const node = document.createElement(tag); if (label) node.textContent = label; if (className) node.className = className; return node; } function button(label, action) { const node = element('button', label); node.type = 'button'; node.onclick = action; return node; } const root = element('section', '', 'isa-group-home'); const header = element('header'); header.append(element('h1', extensionName || text.projects)); const summary = element('dl', '', 'isa-group-summary'); function addCount(heading, label, symbol) { if (onOverview) { const badge = element('span', '', 'isa-group-count'); heading.append(badge); return badge; } const card = element('div'); const term = element('dt', label); term.prepend(icon(symbol)); const value = element('dd', '—'); card.append(term, value); summary.append(card); return value; } header.append(summary); const actions = element('div', '', 'isa-project-toolbar'); const refreshButton = button(text.refresh, refresh); refreshButton.className = 'isa-extension-button isa-extension-button--secondary'; refreshButton.prepend(icon('refresh')); actions.append(refreshButton); const pickFolder = button(text.pickFolder, () => run(async () => { if (await client.pickFolder()) { // An inventory requested before the picker grant may omit the new folder. await pendingRefresh; if (alive) await refresh(); } })); pickFolder.className = 'isa-extension-button isa-extension-button--secondary'; if (client.supports('pickFolder')) actions.append(pickFolder); const importButton = button(text.import, () => run(onImport)); importButton.className = 'isa-extension-button isa-extension-button--secondary'; importButton.prepend(icon('import')); if (onImport && client.supports('create')) actions.append(importButton); header.append(actions); const grid = element('div', '', 'isa-group-grid'); const activity = element('aside'); const recent = element('div'); const recentTitle = element('h2', text.recent); activity.append(recentTitle, recent); const panel = element('section', '', 'isa-group-projects'); const heading = element('header'); const create = button(client.groupFolder ? text.newProject : text.create, () => { name.value = ''; formError.textContent = ''; dialog.showModal(); name.focus(); }); create.className = 'isa-extension-button isa-extension-button--primary'; create.prepend(icon('plus')); const panelTitle = element('h2', text.projects); const projectCount = addCount(panelTitle, text.projects, 'folder'); const recentCount = addCount(recentTitle, text.recent, 'clock'); panelTitle.id = 'isa-projects-' + crypto.randomUUID(); panel.setAttribute('aria-labelledby',panelTitle.id); heading.append(panelTitle); if (client.supports('create')) heading.append(create); const controls = element('div', '', 'isa-project-toolbar'); const search = element('input'); search.className = 'isa-extension-input'; search.type = 'search'; search.placeholder = text.search; search.setAttribute('aria-label',text.search); const sort = element('select'); sort.className = 'isa-extension-select'; sort.setAttribute('aria-label',text.sort); for (const [value,label] of [['updated',text.updated],['name',text.name]]) { const option = element('option',label); option.value = value; sort.append(option); } controls.append(search,sort); const alert = element('p', '', 'isa-group-alert'); alert.setAttribute('role','alert'); const list = element('div', '', 'isa-group-rows'); const columns = element('div', '', 'isa-project-columns'); columns.append(element('span', text.name), element('span', text.owner), element('span', text.creationDate)); const status = element('p', '', 'isa-group-announcement'); status.setAttribute('role','status'); panel.append(heading,controls,columns,list); mountProjectListSkeleton(panel); grid.append(activity,panel); const dialog = element('dialog', '', 'isa-project-create-dialog'); const title = element('h2', text.createTitle); title.id = 'project-create-' + crypto.randomUUID(); dialog.setAttribute('aria-labelledby',title.id); const form = element('form'); const name = element('input', '', 'isa-extension-input'); name.required = true; name.maxLength = 160; name.placeholder = text.name; name.setAttribute('aria-label', text.name); const formError = element('p'); formError.setAttribute('role','alert'); const cancel = button(text.cancel, () => dialog.close()); const submit = element('button',client.groupFolder ? text.saveProject : text.create); submit.type = 'submit'; cancel.className = 'isa-extension-button isa-extension-button--secondary'; submit.className = 'isa-extension-button isa-extension-button--primary'; const progress = element('p', '', 'isa-project-create-progress'); progress.setAttribute('role','status'); const footer = element('div', '', 'isa-project-toolbar'); footer.append(cancel,submit); form.append(name,formError,footer); dialog.append(title,form,progress); if (!onOverview) root.append(header); else root.classList.add('isa-group-home-embedded'); root.append(alert,grid,status,dialog); target.append(root); let projects = [], recents = [], loading = true, hasInventory = false, busy = false, alive = true; let pendingRefresh, openingId; const byName = new Intl.Collator(locale); const date = new Intl.DateTimeFormat(locale,{dateStyle:'medium'}); // The host focuses an extension iframe on pointer-down. Preserve matching // buttons through that refresh so the subsequent click reaches the same row. function updateRows(container, entries, detailed) { const existing = new Map([...container.children].map(node => [node.dataset.projectId, node])); entries.forEach((project, index) => { let row = existing.get(project.id); if (!row) { row = button(); row.dataset.projectId = project.id; if (detailed) { row.append(element('span', '', 'isa-project-name'), element('small'), element('time')); } } existing.delete(project.id); row.onclick = () => openProject(project); row.setAttribute('aria-busy', String(openingId === project.id)); const firstUse = project.hasInstance === false && project.canWrite && !project.archived; if (detailed) { row.querySelector('.isa-project-name').textContent = project.name; row.querySelector('small').textContent = project.hasInstance === false ? text.noInstance : project.instanceCreatorName || '—'; row.title = firstUse ? text.createInstance : ''; row.querySelector('time').textContent = Number.isFinite(Date.parse(project.instanceCreatedAt)) ? date.format(new Date(project.instanceCreatedAt)) : '—'; } else row.textContent = firstUse ? `${project.name} · ${text.createInstance}` : project.name; if (container.children[index] !== row) container.insertBefore(row, container.children[index] ?? null); }); for (const obsolete of existing.values()) obsolete.remove(); } function draw() { const needle = search.value.trim().toLocaleLowerCase(locale); const visible = projects.filter(p => p.name.toLocaleLowerCase(locale).includes(needle)); visible.sort((a,b) => (sort.value === 'updated' && (Date.parse(b.updatedAt) || 0) - (Date.parse(a.updatedAt) || 0)) || byName.compare(a.name,b.name)); list.setAttribute('aria-busy', String(loading)); columns.hidden = !visible.length; updateRows(list, visible, true); if (!busy) { let message = ''; if (loading) message = text.loading; else if (hasInventory && !visible.length) { if (needle) message = text.noMatches; else if (showEmptyProjectMessage) message = text.noProjects; } status.textContent = message; } const recentProjects = recents.map(item => projects.find(project => project.id === item.id)).filter(Boolean); updateRows(recent, recentProjects, false); const unknownCount = onOverview ? '' : '—'; projectCount.textContent = hasInventory ? String(onOverview ? visible.length : projects.length) : unknownCount; recentCount.textContent = hasInventory ? String(recentProjects.length) : unknownCount; if (!recent.childElementCount && hasInventory) recent.append(element('p',text.noActivity)); syncControls(); } function syncControls() { pickFolder.disabled = create.disabled = importButton.disabled = busy; refreshButton.disabled = busy || loading; refreshButton.setAttribute('aria-busy', String(loading)); list.inert = recent.inert = busy; } function openProject(project) { if (busy) return; openingId = project.id; for (const container of [list, recent]) { const row = container.querySelector('[data-project-id="' + CSS.escape(project.id) + '"]'); row?.setAttribute('aria-busy', 'true'); } if (onOpen) void run(() => onOpen(project), alert, ''); else void run(() => client.openListed(project), alert, text.opening + ' ' + project.name); } function refresh() { if (pendingRefresh) return pendingRefresh; loading = true; alert.textContent = ''; if (!busy) status.textContent = text.loading; list.setAttribute('aria-busy','true'); syncControls(); pendingRefresh = Promise.resolve().then(() => client.list()).then(inventory => { if (!alive) return; projects = inventory.filter(p => !p.archived); hasInventory = true; }).catch(error => { if (!alive) return; if (client.sharedProjectsFolder) projects = []; hasInventory = false; alert.textContent = error.code === 'SHARED_GROUP_PROJECTS_UNAVAILABLE' ? text.updateHost : String(error.message ?? error); }).finally(() => { pendingRefresh = null; if (alive) { loading = false; draw(); } }); return pendingRefresh; } async function run(action, notice = alert, message = text.loading) { if (busy) return; busy = true; notice.textContent = ''; status.textContent = message; form.inert = true; syncControls(); try { await action(); } catch (error) { if (alive && error?.name !== 'AbortError') notice.textContent = String(error.message ?? error); } finally { if (alive) { busy = false; openingId = null; form.inert = false; draw(); } } } dialog.oncancel = event => { if (busy) event.preventDefault(); }; form.onsubmit = event => { event.preventDefault(); const projectName = name.value.trim(); if (!projectName || busy) return; progress.textContent = text.creating; submit.disabled = cancel.disabled = true; void run(() => client.create(projectName), formError).finally(() => { if (!alive) return; progress.textContent = ''; submit.disabled = cancel.disabled = false; name.focus(); }); }; search.oninput = sort.onchange = draw; const unsubscribe = client.subscribe(state => { recents = state.recents; draw(); }); onOverview?.({refresh}); void refresh(); const onVisible = () => { if (!document.hidden && !busy) void refresh(); }; document.addEventListener('visibilitychange',onVisible); document.defaultView?.addEventListener('focus',onVisible); return () => { alive = false; onOverview?.(null); unsubscribe(); document.removeEventListener('visibilitychange',onVisible); document.defaultView?.removeEventListener('focus',onVisible); root.remove(); }; }