Files
typedserver/ts_swdash/sw-dash-events.ts

387 lines
12 KiB
TypeScript

import { LitElement, html, css, property, state, customElement } from './plugins.js';
import type { CSSResult, TemplateResult } from './plugins.js';
import { sharedStyles, panelStyles, tableStyles, buttonStyles } from './sw-dash-styles.js';
export interface IEventLogEntry {
id: string;
timestamp: number;
type: string;
message: string;
details?: Record<string, any>;
}
type TEventFilter = 'all' | 'sw_installed' | 'sw_activated' | 'sw_updated' | 'sw_stopped'
| 'speedtest_started' | 'speedtest_completed' | 'speedtest_failed'
| 'backend_connected' | 'backend_disconnected'
| 'cache_invalidated' | 'network_online' | 'network_offline'
| 'update_check' | 'error';
/**
* Events panel component for sw-dash
*/
@customElement('sw-dash-events')
export class SwDashEvents extends LitElement {
public static styles: CSSResult[] = [
sharedStyles,
panelStyles,
tableStyles,
buttonStyles,
css`
:host {
display: block;
}
.events-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: var(--space-4);
gap: var(--space-3);
flex-wrap: wrap;
}
.filter-group {
display: flex;
align-items: center;
gap: var(--space-2);
}
.filter-label {
font-size: 12px;
color: var(--text-tertiary);
}
.filter-select {
background: var(--bg-secondary);
border: 1px solid var(--border-default);
border-radius: var(--radius-sm);
padding: var(--space-1) var(--space-2);
color: var(--text-primary);
font-size: 12px;
}
.filter-select:focus {
outline: none;
border-color: var(--accent-primary);
}
.events-list {
display: flex;
flex-direction: column;
gap: var(--space-2);
max-height: 600px;
overflow-y: auto;
}
.event-card {
background: var(--bg-secondary);
border: 1px solid var(--border-default);
border-radius: var(--radius-md);
padding: var(--space-3);
}
.event-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: var(--space-2);
}
.event-type {
display: inline-flex;
align-items: center;
padding: var(--space-1) var(--space-2);
border-radius: var(--radius-sm);
font-size: 11px;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.event-type.sw { background: rgba(99, 102, 241, 0.15); color: var(--accent-primary); }
.event-type.speedtest { background: rgba(59, 130, 246, 0.15); color: #3b82f6; }
.event-type.network { background: rgba(34, 197, 94, 0.15); color: var(--accent-success); }
.event-type.cache { background: rgba(251, 191, 36, 0.15); color: var(--accent-warning); }
.event-type.error { background: rgba(239, 68, 68, 0.15); color: var(--accent-error); }
.event-time {
font-size: 11px;
color: var(--text-tertiary);
font-variant-numeric: tabular-nums;
}
.event-message {
font-size: 13px;
color: var(--text-primary);
margin-bottom: var(--space-2);
}
.event-details {
font-size: 11px;
color: var(--text-tertiary);
background: var(--bg-tertiary);
padding: var(--space-2);
border-radius: var(--radius-sm);
font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
white-space: pre-wrap;
word-break: break-all;
}
.stats-bar {
display: flex;
gap: var(--space-4);
margin-bottom: var(--space-4);
padding: var(--space-3);
background: var(--bg-secondary);
border-radius: var(--radius-md);
border: 1px solid var(--border-default);
}
.stat-item {
display: flex;
flex-direction: column;
gap: var(--space-1);
}
.stat-value {
font-size: 18px;
font-weight: 600;
color: var(--text-primary);
font-variant-numeric: tabular-nums;
}
.stat-label {
font-size: 11px;
color: var(--text-tertiary);
text-transform: uppercase;
letter-spacing: 0.5px;
}
.empty-state {
text-align: center;
padding: var(--space-6);
color: var(--text-tertiary);
}
.clear-btn {
background: rgba(239, 68, 68, 0.1);
color: var(--accent-error);
border: 1px solid transparent;
}
.clear-btn:hover {
background: rgba(239, 68, 68, 0.2);
border-color: var(--accent-error);
}
.pagination {
display: flex;
justify-content: center;
align-items: center;
gap: var(--space-2);
margin-top: var(--space-4);
}
.page-info {
font-size: 12px;
color: var(--text-tertiary);
}
`
];
@property({ type: Array }) accessor events: IEventLogEntry[] = [];
@state() accessor filter: TEventFilter = 'all';
@state() accessor searchText = '';
@state() accessor totalCount = 0;
@state() accessor isLoading = true;
@state() accessor page = 1;
private readonly pageSize = 50;
// Bound event handler reference for cleanup
private boundEventHandler: ((e: Event) => void) | null = null;
connectedCallback(): void {
super.connectedCallback();
this.loadEvents();
// Listen for pushed events from parent
this.setupPushEventListener();
}
disconnectedCallback(): void {
super.disconnectedCallback();
// Clean up event listener
if (this.boundEventHandler) {
window.removeEventListener('event-logged', this.boundEventHandler);
}
}
/**
* Sets up listener for pushed events from service worker (via sw-dash-app)
*/
private setupPushEventListener(): void {
this.boundEventHandler = (e: Event) => {
const customEvent = e as CustomEvent<IEventLogEntry>;
const newEvent = customEvent.detail;
// Only add if it matches current filter (or filter is 'all')
if (this.filter === 'all' || newEvent.type === this.filter) {
// Prepend new event to the list
this.events = [newEvent, ...this.events];
this.totalCount++;
}
};
// Listen at window level since events bubble up with composed: true
window.addEventListener('event-logged', this.boundEventHandler);
}
private async loadEvents(): Promise<void> {
this.isLoading = true;
try {
const params = new URLSearchParams();
params.set('limit', String(this.pageSize * this.page));
if (this.filter !== 'all') {
params.set('type', this.filter);
}
const response = await fetch(`/sw-dash/events?${params}`);
const data = await response.json();
this.events = data.events;
this.totalCount = data.totalCount;
} catch (err) {
console.error('Failed to load events:', err);
} finally {
this.isLoading = false;
}
}
private handleFilterChange(e: Event): void {
this.filter = (e.target as HTMLSelectElement).value as TEventFilter;
this.page = 1;
this.loadEvents();
}
private handleSearch(e: Event): void {
this.searchText = (e.target as HTMLInputElement).value.toLowerCase();
}
private async handleClear(): Promise<void> {
if (!confirm('Are you sure you want to clear the event log? This cannot be undone.')) {
return;
}
try {
await fetch('/sw-dash/events', { method: 'DELETE' });
this.loadEvents();
} catch (err) {
console.error('Failed to clear events:', err);
}
}
private loadMore(): void {
this.page++;
this.loadEvents();
}
private getTypeClass(type: string): string {
if (type.startsWith('sw_')) return 'sw';
if (type.startsWith('speedtest_')) return 'speedtest';
if (type.startsWith('network_') || type.startsWith('backend_')) return 'network';
if (type.startsWith('cache_') || type === 'update_check') return 'cache';
if (type === 'error') return 'error';
return 'sw';
}
private formatTimestamp(ts: number): string {
const date = new Date(ts);
return date.toLocaleString();
}
private formatTypeLabel(type: string): string {
return type.replace(/_/g, ' ');
}
private getFilteredEvents(): IEventLogEntry[] {
if (!this.searchText) return this.events;
return this.events.filter(e =>
e.message.toLowerCase().includes(this.searchText) ||
e.type.toLowerCase().includes(this.searchText) ||
(e.details && JSON.stringify(e.details).toLowerCase().includes(this.searchText))
);
}
public render(): TemplateResult {
const filteredEvents = this.getFilteredEvents();
return html`
<div class="stats-bar">
<div class="stat-item">
<span class="stat-value">${this.totalCount}</span>
<span class="stat-label">Total Events</span>
</div>
<div class="stat-item">
<span class="stat-value">${filteredEvents.length}</span>
<span class="stat-label">Showing</span>
</div>
</div>
<div class="events-header">
<div class="filter-group">
<span class="filter-label">Filter:</span>
<select class="filter-select" @change="${this.handleFilterChange}">
<option value="all">All Events</option>
<option value="sw_installed">SW Installed</option>
<option value="sw_activated">SW Activated</option>
<option value="sw_updated">SW Updated</option>
<option value="speedtest_started">Speedtest Started</option>
<option value="speedtest_completed">Speedtest Completed</option>
<option value="speedtest_failed">Speedtest Failed</option>
<option value="network_online">Network Online</option>
<option value="network_offline">Network Offline</option>
<option value="cache_invalidated">Cache Invalidated</option>
<option value="error">Errors</option>
</select>
<input
type="text"
class="search-input"
placeholder="Search events..."
.value="${this.searchText}"
@input="${this.handleSearch}"
style="width: 200px;"
>
</div>
<button class="btn clear-btn" @click="${this.handleClear}">Clear Log</button>
</div>
${this.isLoading && this.events.length === 0 ? html`
<div class="empty-state">Loading events...</div>
` : filteredEvents.length === 0 ? html`
<div class="empty-state">No events found</div>
` : html`
<div class="events-list">
${filteredEvents.map(event => html`
<div class="event-card">
<div class="event-header">
<span class="event-type ${this.getTypeClass(event.type)}">${this.formatTypeLabel(event.type)}</span>
<span class="event-time">${this.formatTimestamp(event.timestamp)}</span>
</div>
<div class="event-message">${event.message}</div>
${event.details ? html`
<div class="event-details">${JSON.stringify(event.details, null, 2)}</div>
` : ''}
</div>
`)}
</div>
${this.events.length < this.totalCount ? html`
<div class="pagination">
<button class="btn btn-secondary" @click="${this.loadMore}" ?disabled="${this.isLoading}">
${this.isLoading ? 'Loading...' : 'Load More'}
</button>
<span class="page-info">${this.events.length} of ${this.totalCount} events</span>
</div>
` : ''}
`}
`;
}
}