// Copyright (c) 2026 Gordon Bolton. MIT License. import { state, STATUS_REFRESH_MS } from '../state.js'; import { refreshStatusPanel as apiRefreshStatusPanel } from '../api.js'; import { fetchWebhookStatus } from './webhooks.js'; export async function toggleStatusPanel() { const panel = document.getElementById('status-panel'); const webhooksSection = document.getElementById('webhooks-section'); if (!panel.classList.contains('hidden')) { // Close both panels (webhooks is a sibling, hide it too) panel.classList.add('hidden'); if (webhooksSection) webhooksSection.classList.add('hidden'); if (state.statusRefreshHandle) { clearInterval(state.statusRefreshHandle); state.statusRefreshHandle = null; } return; } // Open status panel and webhooks section (siblings) panel.classList.remove('hidden'); // Show webhooks section for admin users (collapsed by default) if (webhooksSection && state.isAdmin) { webhooksSection.classList.remove('hidden'); state.webhookSectionExpanded = false; document.getElementById('webhooks-content').classList.add('hidden'); document.getElementById('webhooks-toggle').classList.remove('expanded'); await fetchWebhookStatus(); } else if (webhooksSection) { webhooksSection.classList.add('hidden'); } refreshStatusPanel(); if (state.statusRefreshHandle) clearInterval(state.statusRefreshHandle); state.statusRefreshHandle = setInterval(refreshStatusPanel, STATUS_REFRESH_MS); } export function closeStatusPanel() { document.getElementById('status-panel').classList.add('hidden'); const webhooksSection = document.getElementById('webhooks-section'); if (webhooksSection) webhooksSection.classList.add('hidden'); if (state.statusRefreshHandle) { clearInterval(state.statusRefreshHandle); state.statusRefreshHandle = null; } } export async function refreshStatusPanel() { const panel = document.getElementById('status-panel'); const contentDiv = document.getElementById('status-content'); console.log('[Status] panel found:', !!panel, 'contentDiv found:', !!contentDiv, 'panel display:', panel?.style?.display); if (!panel || panel.classList.contains('hidden')) return; console.log('[Status] Refreshing status panel...'); try { const result = await apiRefreshStatusPanel(); if (result.success) { console.log('[Status] Got status data, rendering...'); renderStatusPanel(result.data, panel); } else { console.error('[Status] API returned error:', result.error); if (contentDiv && (!contentDiv.innerHTML || contentDiv.innerHTML.includes('status-loading'))) { contentDiv.innerHTML = '
Failed to load status: ' + result.error + '
'; } } } catch (err) { console.error('[Status] Error fetching status:', err); // Don't overwrite panel on transient error during auto-refresh if (contentDiv && (!contentDiv.innerHTML || contentDiv.innerHTML.includes('status-loading'))) { contentDiv.innerHTML = 'Failed to load status: ' + err.message + '
'; } } } export function renderStatusPanel(data, panel) { console.log('[Status] renderStatusPanel called with data:', data ? 'yes' : 'no', 'keys:', data ? Object.keys(data) : 'none'); const s = data.server; const hrs = Math.floor(s.uptimeSeconds / 3600); const mins = Math.floor((s.uptimeSeconds % 3600) / 60); const secs = s.uptimeSeconds % 60; const uptime = `${hrs}h ${mins}m ${secs}s`; const totalKB = (data.cache.totalSizeBytes / 1024).toFixed(1); let html = `| Key | Items | Size | TTL |
|---|---|---|---|
${escapeHtml(e.key)} | ${items} | ${sizeStr} | ${ttlStr} |