(() => { if (window.NCHeaderControls) return; const state = { level: 'checking', health: null, readiness: null, service: '', checkedAt: null, loading: false, }; const boundButtons = new WeakSet(); let statusRequest = null; let statusTimer = null; let activeDialog = null; let activeOpener = null; const levelLabels = { checking: 'Checking', healthy: 'Healthy', degraded: 'Degraded', offline: 'Offline', }; const levelIcons = { checking: 'fa-circle-question', healthy: 'fa-circle-check', degraded: 'fa-triangle-exclamation', offline: 'fa-circle-xmark', }; const levelColors = { checking: 'var(--color-bar-text-muted)', healthy: 'var(--color-success-strong, #16a34a)', degraded: 'var(--color-warning, #f59e0b)', offline: 'var(--color-danger, #ef4444)', }; function setDialogVisible(dialog, visible) { if (!dialog) return; dialog.hidden = !visible; dialog.style.display = visible ? 'grid' : 'none'; dialog.setAttribute('aria-hidden', visible ? 'false' : 'true'); } function createOverlay(id, title, description) { const overlay = document.createElement('div'); overlay.id = id; overlay.hidden = true; overlay.setAttribute('aria-hidden', 'true'); overlay.setAttribute('role', 'dialog'); overlay.setAttribute('aria-modal', 'true'); overlay.setAttribute('aria-labelledby', `${id}Title`); overlay.className = 'fixed inset-0 z-[210] hidden place-items-center bg-black/70 p-4'; overlay.style.background = 'var(--color-overlay, rgba(0, 0, 0, 0.7))'; overlay.innerHTML = `

${title}

${description}

`; document.body.appendChild(overlay); overlay.addEventListener('click', event => { if (event.target === overlay) closeDialog(overlay); }); overlay.querySelector('[data-dialog-close]')?.addEventListener('click', () => closeDialog(overlay)); return overlay; } function ensureSettingsDialog() { let overlay = document.getElementById('ncSettingsOverlay'); if (overlay) return overlay; overlay = createOverlay('ncSettingsOverlay', 'Settings', 'Personalize the shared MediaCollection interface.'); const body = overlay.querySelector('[data-dialog-body]'); body.innerHTML = `
Theme
Switch between the light and dark appearance.
`; return overlay; } function ensureStatusDialog() { let overlay = document.getElementById('ncStatusOverlay'); if (overlay) return overlay; overlay = createOverlay('ncStatusOverlay', 'Status', 'Live liveness and database readiness checks.'); const body = overlay.querySelector('[data-dialog-body]'); body.innerHTML = `
Checking
LivenessNot checked
Readiness / databaseNot checked

`; overlay.querySelector('[data-status-refresh]')?.addEventListener('click', () => void refreshStatus()); overlay.querySelectorAll('[data-dialog-close]').forEach(button => { button.addEventListener('click', () => closeDialog(overlay)); }); return overlay; } function openDialog(dialog, opener) { activeDialog = dialog; activeOpener = opener || null; setDialogVisible(dialog, true); window.setTimeout(() => dialog.querySelector('[data-dialog-close]')?.focus(), 0); } function closeDialog(dialog) { if (!dialog) return; setDialogVisible(dialog, false); if (activeDialog === dialog) { const opener = activeOpener; activeDialog = null; activeOpener = null; opener?.focus?.(); } } async function probe(path) { try { const response = await fetch(path, { credentials: 'same-origin', headers: { Accept: 'application/json' }, }); const payload = await response.json().catch(() => null); return { ok: response.ok && payload?.ok !== false, status: response.status, service: payload?.service || '', error: response.ok ? '' : payload?.error || response.statusText || `HTTP ${response.status}`, }; } catch (error) { return { ok: false, status: null, service: '', error: error instanceof Error ? error.message : 'Unavailable', }; } } function resultLabel(result) { if (!result) return 'Not checked'; if (result.ok) return 'OK'; return result.error || (result.status ? `HTTP ${result.status}` : 'Unavailable'); } function renderStatus() { const iconClass = levelIcons[state.level]; const color = levelColors[state.level]; document.querySelectorAll('[data-status-button]').forEach(button => { const icon = button.querySelector('[data-status-icon]'); if (icon) { icon.className = `fas ${iconClass}`; icon.style.color = color; } button.title = `Service status: ${levelLabels[state.level]}`; button.setAttribute('aria-label', `Service status: ${levelLabels[state.level]}`); }); const dialog = document.getElementById('ncStatusOverlay'); if (!dialog) return; const overallIcon = dialog.querySelector('[data-status-overall-icon]'); if (overallIcon) { overallIcon.className = `fas ${iconClass} text-lg`; overallIcon.style.color = color; } const overall = dialog.querySelector('[data-status-overall]'); if (overall) { overall.textContent = levelLabels[state.level]; overall.style.color = color; } const checked = dialog.querySelector('[data-status-checked]'); if (checked) checked.textContent = state.checkedAt ? new Date(state.checkedAt).toLocaleTimeString() : ''; const health = dialog.querySelector('[data-status-health]'); if (health) { health.textContent = resultLabel(state.health); health.style.color = state.health?.ok ? 'var(--color-success-strong, #16a34a)' : 'var(--color-danger, #ef4444)'; } const readiness = dialog.querySelector('[data-status-readiness]'); if (readiness) { readiness.textContent = resultLabel(state.readiness); readiness.style.color = state.readiness?.ok ? 'var(--color-success-strong, #16a34a)' : 'var(--color-danger, #ef4444)'; } const service = dialog.querySelector('[data-status-service]'); if (service) service.textContent = state.service ? `Service: ${state.service}` : ''; const refreshButton = dialog.querySelector('[data-status-refresh]'); if (refreshButton) refreshButton.disabled = state.loading; } function refreshStatus() { if (statusRequest) return statusRequest; state.loading = true; renderStatus(); statusRequest = Promise.all([probe('/healthz'), probe('/ready')]) .then(([health, readiness]) => { state.health = health; state.readiness = readiness; state.service = health.service || readiness.service || ''; state.level = health.ok && readiness.ok ? 'healthy' : health.ok ? 'degraded' : 'offline'; state.checkedAt = new Date().toISOString(); }) .finally(() => { state.loading = false; statusRequest = null; renderStatus(); }); return statusRequest; } function bindHeaderButtons() { document.querySelectorAll('[data-settings-button], [data-settings-menu-button]').forEach(button => { if (boundButtons.has(button)) return; boundButtons.add(button); button.addEventListener('click', () => openDialog(ensureSettingsDialog(), button)); }); document.querySelectorAll('[data-status-button]').forEach(button => { if (boundButtons.has(button)) return; boundButtons.add(button); button.addEventListener('click', () => { const dialog = ensureStatusDialog(); openDialog(dialog, button); void refreshStatus(); }); }); renderStatus(); } function init() { if (!document.body) return; ensureSettingsDialog(); ensureStatusDialog(); bindHeaderButtons(); window.NCThemeToggle?.init?.(); if (!statusTimer) { void refreshStatus(); statusTimer = window.setInterval(() => void refreshStatus(), 15_000); } } document.addEventListener('keydown', event => { if (event.key === 'Escape' && activeDialog) closeDialog(activeDialog); }); document.addEventListener('DOMContentLoaded', init, { once: true }); document.addEventListener('nc:componentsLoaded', init); window.NCHeaderControls = { init, refreshStatus }; })();