fix: make empty runtime state reachable
This commit is contained in:
parent
c2fb20b066
commit
35855f018f
3 changed files with 44 additions and 15 deletions
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue