fix: make empty runtime state reachable

This commit is contained in:
vince 2026-06-18 19:32:32 +02:00
parent c2fb20b066
commit 35855f018f
3 changed files with 44 additions and 15 deletions

View file

@ -42,21 +42,24 @@ export interface DashboardRuntimeInvalid {
errors: string[];
}
export interface DashboardRuntimeOptions {
seedIfEmpty?: boolean;
seedDocument?: DashboardDocument;
}
export function loadDashboardRuntime(
store?: DashboardStore,
options: DashboardRuntimeOptions = {},
): DashboardRuntimeState {
const dashboardStore = store || createDashboardStore();
try {
const seeded = dashboardStore.seedDashboardIfEmpty(dimensionLabDashboardFixture, {
actor: "initial-seed",
message: "load initial dashboard document",
});
const active = dashboardStore.getActiveDashboard();
const document = active?.document || seeded.document;
const currentRevisionId = active?.currentRevisionId || seeded.id;
if (active) {
return readyRuntimeState(active.document, active.currentRevisionId);
}
if (!document) {
if (!options.seedIfEmpty) {
return {
state: "empty",
title: "No Dashboard Model",
@ -65,12 +68,15 @@ export function loadDashboardRuntime(
};
}
return {
state: "ready",
document,
schemaVersion: document.schemaVersion,
currentRevisionId,
};
const seeded = dashboardStore.seedDashboardIfEmpty(
options.seedDocument || dimensionLabDashboardFixture,
{
actor: "initial-seed",
message: "load initial dashboard document",
},
);
return readyRuntimeState(seeded.document, seeded.id);
} catch (error) {
if (error instanceof DashboardPersistenceValidationError) {
return invalidRuntimeState(error.failure.errors);
@ -86,6 +92,18 @@ export function loadDashboardRuntime(
}
}
function readyRuntimeState(
document: DashboardDocument,
currentRevisionId: string,
): DashboardRuntimeReady {
return {
state: "ready",
document,
schemaVersion: document.schemaVersion,
currentRevisionId,
};
}
function invalidRuntimeState(errors: string[]): DashboardRuntimeInvalid {
return {
state: "invalid",