89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import { dimensionLabDashboardFixture } from "$lib/model/fixtures/dimensionlab";
|
|
import type { DashboardDocument } from "$lib/model";
|
|
import {
|
|
createDashboardStore,
|
|
DashboardPersistenceValidationError,
|
|
type DashboardStore,
|
|
} from "$lib/server/db/dashboard-store";
|
|
import { UnsupportedDashboardModelVersionError } from "$lib/server/db/model-migrations";
|
|
|
|
export type DashboardRuntimeState =
|
|
| DashboardRuntimeEmpty
|
|
| DashboardRuntimeInvalid
|
|
| DashboardRuntimeReady;
|
|
|
|
export interface DashboardRuntimeReady {
|
|
state: "ready";
|
|
document: DashboardDocument;
|
|
schemaVersion: string;
|
|
currentRevisionId: string;
|
|
}
|
|
|
|
export interface DashboardRuntimeEmpty {
|
|
state: "empty";
|
|
title: string;
|
|
subtitle: string;
|
|
message: string;
|
|
}
|
|
|
|
export interface DashboardRuntimeInvalid {
|
|
state: "invalid";
|
|
title: string;
|
|
subtitle: string;
|
|
message: string;
|
|
errors: string[];
|
|
}
|
|
|
|
export function loadDashboardRuntime(
|
|
store?: DashboardStore,
|
|
): 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 (!document) {
|
|
return {
|
|
state: "empty",
|
|
title: "No Dashboard Model",
|
|
subtitle: "No active document",
|
|
message: "No validated dashboard document is active yet.",
|
|
};
|
|
}
|
|
|
|
return {
|
|
state: "ready",
|
|
document,
|
|
schemaVersion: document.schemaVersion,
|
|
currentRevisionId,
|
|
};
|
|
} catch (error) {
|
|
if (error instanceof DashboardPersistenceValidationError) {
|
|
return invalidRuntimeState(error.failure.errors);
|
|
}
|
|
|
|
if (error instanceof UnsupportedDashboardModelVersionError) {
|
|
return invalidRuntimeState([error.message]);
|
|
}
|
|
|
|
throw error;
|
|
} finally {
|
|
if (!store) dashboardStore.close();
|
|
}
|
|
}
|
|
|
|
function invalidRuntimeState(errors: string[]): DashboardRuntimeInvalid {
|
|
return {
|
|
state: "invalid",
|
|
title: "Invalid Dashboard Model",
|
|
subtitle: "Validation failed",
|
|
message: "The active dashboard document could not be validated.",
|
|
errors,
|
|
};
|
|
}
|