The schema's event fields gain values, a values_from marker (agents, repos) and a closed flag; a node field gains the marker and the flag. kind is now listed as issue and PR, what the router sends. The flow editor shows a dropdown where the list is complete, suggestions where it is only examples, and a plain box where there is none.
96 lines
3.1 KiB
TypeScript
96 lines
3.1 KiB
TypeScript
// What the editor offers for a value the operator would otherwise have to
|
|
// know: the server's schema says which values a field takes, or where to ask
|
|
// for them. Pure, so `npm test` can run it.
|
|
import type { Described, FlowSchema } from "./FlowTypes.js";
|
|
|
|
// Where each `values_from` marker of the schema is fetched, a list of names.
|
|
export const SOURCE_URLS: Record<string, string> = {
|
|
agents: "/api/agents",
|
|
repos: "/api/repos",
|
|
};
|
|
|
|
// Names fetched so far per marker. A marker is absent until its list arrives,
|
|
// and stays absent when the fetch fails.
|
|
export type Sources = Record<string, string[]>;
|
|
|
|
/** What the schema says one field can hold. */
|
|
export interface ValueSpec {
|
|
values: Described[];
|
|
values_from?: string | null;
|
|
closed?: boolean;
|
|
}
|
|
|
|
export interface Offer {
|
|
options: { value: string; label: string }[];
|
|
// The options are every value there is, so a dropdown will do; otherwise
|
|
// they are suggestions beside a box that takes any text.
|
|
closed: boolean;
|
|
}
|
|
|
|
/** The markers the schema names that this editor can fetch, once each. */
|
|
export function markersIn(schema: FlowSchema): string[] {
|
|
const specs: ValueSpec[] = [
|
|
...schema.fields,
|
|
...schema.node_kinds.flatMap((k) => k.fields),
|
|
];
|
|
const wanted = specs.flatMap((s) => (s.values_from ? [s.values_from] : []));
|
|
return [...new Set(wanted)].filter((m) => m in SOURCE_URLS).sort();
|
|
}
|
|
|
|
/** The spec of node field `field` of `kind`, if the schema has one. */
|
|
export function kindSpec(
|
|
schema: FlowSchema,
|
|
kind: string,
|
|
field: string,
|
|
): ValueSpec | undefined {
|
|
return schema.node_kinds
|
|
.find((k) => k.kind === kind)
|
|
?.fields.find((f) => f.name === field);
|
|
}
|
|
|
|
/** The spec of event field `name`, if the schema has one. */
|
|
export function eventSpec(
|
|
schema: FlowSchema,
|
|
name: string,
|
|
): ValueSpec | undefined {
|
|
return schema.fields.find((f) => f.name === name);
|
|
}
|
|
|
|
/**
|
|
* What to offer for a field that holds `current`, or null when it has no
|
|
* known values and stays a plain text box.
|
|
*
|
|
* A dropdown needs every value, so one whose source has not arrived becomes
|
|
* suggestions; a value already set that the list lacks (an agent since
|
|
* removed) stays selectable so opening a flow never changes it.
|
|
*/
|
|
export function offerFor(
|
|
spec: ValueSpec | undefined,
|
|
sources: Sources,
|
|
current: string,
|
|
): Offer | null {
|
|
if (!spec) return null;
|
|
const from = spec.values_from;
|
|
const fetched = from ? sources[from] : undefined;
|
|
const named = spec.values.map((v) => ({
|
|
value: v.name,
|
|
label: v.description ? `${v.name} — ${v.description}` : v.name,
|
|
}));
|
|
const options = [
|
|
...named,
|
|
...(fetched ?? [])
|
|
.filter((n) => !spec.values.some((v) => v.name === n))
|
|
.map((n) => ({ value: n, label: n })),
|
|
];
|
|
if (options.length === 0) return null;
|
|
const complete = spec.closed === true && (!from || fetched !== undefined);
|
|
if (!complete) return { options, closed: false };
|
|
const known = options.some((o) => o.value === current);
|
|
return {
|
|
options:
|
|
current === "" || known
|
|
? options
|
|
: [{ value: current, label: `${current} (not listed)` }, ...options],
|
|
closed: true,
|
|
};
|
|
}
|