Files
archie/ui/src/stores/workflows.ts
T

94 lines
3.2 KiB
TypeScript

import { defineStore } from "pinia";
import { computed } from "vue";
import type { StepTypeInfo } from "../workflows/workflow-yaml.ts";
import { useSettingsStore } from "./settings.ts";
const KIND = "workflow-definitions";
export interface WorkflowDefinitionEntry {
id: string;
yaml: string;
}
export interface WorkflowDefinitionCollection {
definitions: WorkflowDefinitionEntry[];
}
export function upsertWorkflowDefinition(
collection: WorkflowDefinitionCollection,
entry: WorkflowDefinitionEntry,
): WorkflowDefinitionCollection {
const found = collection.definitions.some(({ id }) => id === entry.id);
return {
definitions: found
? collection.definitions.map((definition) =>
definition.id === entry.id ? entry : definition,
)
: [...collection.definitions, entry],
};
}
// restoreShippedDefinitions resets every shipped workflow to what archie ships
// and keeps the operator's custom ones, which archie has no copy of to restore.
export function restoreShippedDefinitions(
collection: WorkflowDefinitionCollection,
shipped: WorkflowDefinitionCollection,
): WorkflowDefinitionCollection {
return shipped.definitions.reduce(upsertWorkflowDefinition, collection);
}
export function removeWorkflowDefinition(
collection: WorkflowDefinitionCollection,
id: string,
): WorkflowDefinitionCollection {
return {
definitions: collection.definitions.filter(
(definition) => definition.id !== id,
),
};
}
function parse<T>(json: string | undefined, pick: (value: Record<string, unknown>) => unknown, fallback: T): T {
if (!json) return fallback;
try {
return (pick(JSON.parse(json)) as T | undefined) ?? fallback;
} catch {
return fallback;
}
}
/** The workflow definitions: one control-plane resource, with the shipped
* copies and step vocabulary its catalog entry serves. */
export const useWorkflowsStore = defineStore("workflows", () => {
const settings = useSettingsStore();
const descriptor = computed(() => settings.catalog.find((resource) => resource.kind === KIND));
const state = computed(() => settings.stateFor(KIND));
const collection = computed<WorkflowDefinitionCollection>(
() => (state.value.resource?.value as WorkflowDefinitionCollection | undefined) ?? { definitions: [] },
);
/** The shipped definitions the catalog offers as "restore shipped". */
const shipped = computed(() =>
parse<WorkflowDefinitionCollection>(descriptor.value?.defaults_json, (value) => value, { definitions: [] }),
);
/** The registered workflow step types, served in the definitions schema. */
const stepTypes = computed(() =>
parse<StepTypeInfo[]>(descriptor.value?.schema_json, (value) => value["x-step-types"], []),
);
/** Where issues are routed by label, served beside the step types. */
const intakeRoutes = computed(() =>
parse<Record<string, string>>(descriptor.value?.schema_json, (value) => value["x-intake-routes"], {}),
);
return {
state,
error: computed(() => settings.catalogError || state.value.error),
collection,
shipped,
stepTypes,
intakeRoutes,
load: settings.load,
save: (next: WorkflowDefinitionCollection) => settings.replace(KIND, next),
};
});