feat(dashboard): add React foundation and overview

This commit is contained in:
jjbrunton committed 2026-08-08 14:53:53 +01:00
1 parent 59c3a61a88
commit 51e3e28a9b
20 files changed
+3347 -244

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
import type { ZodType } from 'zod';
import {
botStatsSchema,
controlResponseSchema,
timersSchema,
type BotStats,
type ControlResponse,
type Timers,
} from './contracts.js';
export class ApiError extends Error {
constructor(
message: string,
readonly status: number,
) {
super(message);
this.name = 'ApiError';
}
}
interface ErrorPayload {
error?: unknown;
}
async function parseResponse<T>(response: Response, schema: ZodType<T>): Promise<T> {
const payload: unknown = await response.json();
if (!response.ok) {
const errorPayload = payload as ErrorPayload;
const message = typeof errorPayload.error === 'string' ? errorPayload.error : `Request failed (${response.status})`;
throw new ApiError(message, response.status);
}
return schema.parse(payload);
}
export interface DashboardApi {
getStats(): Promise<BotStats>;
getTimers(): Promise<Timers>;
setRunning(isRunning: boolean): Promise<ControlResponse>;
}
export function createDashboardApi(fetchImplementation?: typeof fetch): DashboardApi {
function request(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
return (fetchImplementation ?? globalThis.fetch)(input, init);
}
return {
async getStats() {
return parseResponse(await request('/api/stats'), botStatsSchema);
},
async getTimers() {
return parseResponse(await request('/api/timers'), timersSchema);
},
async setRunning(isRunning) {
const endpoint = isRunning ? '/api/start' : '/api/stop';
return parseResponse(await request(endpoint, { method: 'POST' }), controlResponseSchema);
},
};
}
export const dashboardApi = createDashboardApi();