Same pattern as GameStream (commit 6a46007), adapted for CollabTable being genuinely multi-user (not single-admin): admin user management (list/create/update/delete, role reassignment, superadmin protection) is fully proxied to AuthGuard, which already enforces the same rules CollabTable checked locally. Cookie forwarding, no local session token, AUTHGUARD_URL optional (unset = auth fully disabled), transport failures always surface as errAuthGuardUnavailable rather than ever letting a request through - proven with live tests against a closed socket. CollabTable's device-sync API token feature (separate from browser sessions) is deliberately left local-only regardless of AUTHGUARD_URL, keyed by a new currentUserID helper (AuthGuard's id when enabled, the shared placeholder id 0 when disabled) - a real, functional token either way, not faked data. Real bug found and fixed: api_tokens.user_id had a hard FK into the local auth_users table, which would reject new tokens once accounts stop being written locally. Dropped the now-invalid FK. Known, accepted gap (same as GameStream): self-service profile editing for non-admin accounts is lost, since AuthGuard only exposes an admin endpoint, no self-edit. TOTP QR issuer branding now comes from AuthGuard rather than CollabTable. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
267 lines
11 KiB
JavaScript
267 lines
11 KiB
JavaScript
(() => {
|
|
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 = `
|
|
<div class="relative w-[min(560px,92vw)] overflow-hidden rounded-lg border border-[var(--color-border-strong)] bg-[var(--color-surface)] text-[var(--color-text)] shadow-2xl" role="document">
|
|
<button type="button" data-dialog-close aria-label="Close" class="absolute right-2.5 top-2.5 inline-flex h-9 w-9 items-center justify-center rounded-full border border-[var(--color-border-strong)] bg-[var(--color-bg-soft)] text-[var(--color-text)] shadow-sm hover:bg-[var(--color-surface)]"><i class="fas fa-xmark" aria-hidden="true"></i></button>
|
|
<div class="flex flex-col gap-1 px-4 pb-2 pt-4">
|
|
<h2 id="${id}Title" class="m-0 text-lg font-[650]">${title}</h2>
|
|
<p class="m-0 text-sm text-[var(--color-text-dim)]">${description}</p>
|
|
</div>
|
|
<div data-dialog-body class="px-4 pb-4 pt-3"></div>
|
|
</div>
|
|
`;
|
|
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 = `
|
|
<div class="flex items-center justify-between gap-3 rounded-md border border-[var(--color-border-strong)] bg-[var(--color-bg-soft)] px-3 py-3">
|
|
<div>
|
|
<div class="text-sm font-medium">Theme</div>
|
|
<div class="mt-1 text-xs text-[var(--color-text-dim)]">Switch between the light and dark appearance.</div>
|
|
</div>
|
|
<button type="button" data-theme-toggle class="inline-flex min-w-[92px] items-center justify-center rounded-full border border-[var(--color-border-strong)] bg-[var(--color-surface)] px-3 py-2 text-sm font-semibold text-[var(--color-text)] shadow-sm hover:bg-[var(--color-surface-raised)]" title="Toggle dark / light theme" aria-label="Toggle theme">Toggle</button>
|
|
</div>
|
|
`;
|
|
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 = `
|
|
<div class="mb-4 flex items-center gap-3 rounded-md border border-[var(--color-border-strong)] bg-[var(--color-bg-soft)] px-3 py-3" aria-live="polite">
|
|
<i data-status-overall-icon class="fas fa-circle-question text-lg" aria-hidden="true"></i>
|
|
<span data-status-overall class="font-semibold">Checking</span>
|
|
<span data-status-checked class="ml-auto text-xs text-[var(--color-text-dim)]"></span>
|
|
</div>
|
|
<div class="flex flex-col gap-2 text-sm">
|
|
<div class="flex items-center justify-between gap-4 rounded-md border border-[var(--color-border-strong)] px-3 py-2"><span>Liveness</span><span data-status-health>Not checked</span></div>
|
|
<div class="flex items-center justify-between gap-4 rounded-md border border-[var(--color-border-strong)] px-3 py-2"><span>Readiness / database</span><span data-status-readiness>Not checked</span></div>
|
|
</div>
|
|
<p data-status-service class="mt-3 mb-0 text-xs text-[var(--color-text-dim)]"></p>
|
|
<div class="mt-5 flex justify-end gap-2">
|
|
<button type="button" data-status-refresh class="inline-flex items-center gap-2 rounded-full border border-[var(--color-border-strong)] bg-[var(--color-surface)] px-3 py-2 text-sm font-semibold text-[var(--color-text)] shadow-sm hover:bg-[var(--color-surface-raised)]"><i class="fas fa-rotate" aria-hidden="true"></i>Refresh</button>
|
|
<button type="button" data-dialog-close class="inline-flex items-center justify-center rounded-full bg-[var(--color-action)] px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-[var(--color-action-hover)]">Dismiss</button>
|
|
</div>
|
|
`;
|
|
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 };
|
|
})();
|