Files
cameleer-server/ui/src/pages/Alerts/RuleEditor/ScopeStep.tsx

99 lines
3.4 KiB
TypeScript
Raw Normal View History

import { FormField, Input, Select } from '@cameleer/design-system';
import { useCatalog } from '../../../api/queries/catalog';
import { useAgents } from '../../../api/queries/agents';
import { useSelectedEnv } from '../../../api/queries/alertMeta';
import type { FormState } from './form-state';
refactor(ui/alerts): derive option lists + form-state types from schema.d.ts Closes item 5 on the Plan 03 cleanup triage. The option arrays ("METRICS", "COMPARATORS", KIND_OPTIONS, SEVERITY_OPTIONS, FIRE_MODES) scattered across RouteMetricForm / JvmMetricForm / ExchangeMatchForm / ConditionStep / ScopeStep were hand-typed string literals. They drifted silently — P95_LATENCY_MS appeared in a dropdown without a backend counterpart (caught at runtime in bcde6678); JvmMetric.LATEST and Comparator.EQ existed on the backend but were missing from the UI all along. Fix: new `ui/src/api/alerting-enums.ts` derives every enum from schema.d.ts and pairs each with a `Record<T, string>` label map. TypeScript enforces exhaustiveness — adding or removing a backend value fails the build of this file until the label map is updated. Every consumer imports the generated `*_OPTIONS` array. Covered (schema-derived): - ConditionKind → CONDITION_KIND_OPTIONS - Severity → SEVERITY_OPTIONS - RouteMetric → ROUTE_METRIC_OPTIONS - Comparator → COMPARATOR_OPTIONS (adds EQ that was missing) - JvmAggregation → JVM_AGGREGATION_OPTIONS (adds LATEST that was missing) - ExchangeMatch.fireMode → EXCHANGE_FIRE_MODE_OPTIONS - AlertRuleTarget.kind → TARGET_KIND_OPTIONS form-state.ts: `severity: 'CRITICAL' | 'WARNING' | 'INFO'` and `kind: 'USER' | 'GROUP' | 'ROLE'` literal unions swapped for the derived `Severity` / `TargetKind` aliases. Not covered, backend types them as `String` (no `@Schema(allowableValues)` annotation yet): - AgentStateCondition.state - DeploymentStateCondition.states - LogPatternCondition.level - ExchangeFilter.status - JvmMetricCondition.metric These stay hand-typed with a pointer-comment. Follow-up: add `@Schema(allowableValues = …)` to the Java record components so the enums land in schema.d.ts; then fold them into alerting-enums.ts. Plus: gitnexus index-stats refresh in AGENTS.md/CLAUDE.md from the post-deploy reindex. Verified: ui build green, 49/49 vitest pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 19:02:52 +02:00
import { SEVERITY_OPTIONS } from '../../../api/alerting-enums';
const SCOPE_OPTIONS = [
{ value: 'env', label: 'Environment-wide' },
{ value: 'app', label: 'Single app' },
{ value: 'route', label: 'Single route' },
{ value: 'agent', label: 'Single agent' },
];
type AgentSummary = {
instanceId?: string;
displayName?: string;
applicationId?: string;
};
export function ScopeStep({ form, setForm }: { form: FormState; setForm: (f: FormState) => void }) {
const env = useSelectedEnv();
const { data: catalog } = useCatalog(env);
const { data: agents } = useAgents();
const apps = (catalog ?? []).map((a) => ({
slug: a.slug,
name: a.displayName ?? a.slug,
routes: a.routes ?? [],
}));
const selectedApp = apps.find((a) => a.slug === form.appSlug);
const routes = selectedApp?.routes ?? [];
const appAgents: AgentSummary[] = Array.isArray(agents)
? (agents as AgentSummary[]).filter((a) => a.applicationId === form.appSlug)
: [];
return (
<div style={{ display: 'grid', gap: 12, maxWidth: 600 }}>
<FormField label="Name">
<Input
value={form.name}
onChange={(e) => setForm({ ...form, name: e.target.value })}
placeholder="Order API error rate"
/>
</FormField>
<FormField label="Description">
<Input
value={form.description}
onChange={(e) => setForm({ ...form, description: e.target.value })}
/>
</FormField>
<FormField label="Severity">
<Select
value={form.severity}
onChange={(e) => setForm({ ...form, severity: e.target.value as FormState['severity'] })}
options={SEVERITY_OPTIONS}
/>
</FormField>
<FormField label="Scope">
<Select
value={form.scopeKind}
onChange={(e) => setForm({ ...form, scopeKind: e.target.value as FormState['scopeKind'] })}
options={SCOPE_OPTIONS}
/>
</FormField>
{form.scopeKind !== 'env' && (
<FormField label="App">
<Select
value={form.appSlug}
onChange={(e) => setForm({ ...form, appSlug: e.target.value, routeId: '', agentId: '' })}
options={[{ value: '', label: '-- select --' }, ...apps.map((a) => ({ value: a.slug, label: a.name }))]}
/>
</FormField>
)}
{form.scopeKind === 'route' && (
<FormField label="Route">
<Select
value={form.routeId}
onChange={(e) => setForm({ ...form, routeId: e.target.value })}
options={[{ value: '', label: '-- select --' }, ...routes.map((r) => ({ value: r.routeId, label: r.routeId }))]}
/>
</FormField>
)}
{form.scopeKind === 'agent' && (
<FormField label="Agent">
<Select
value={form.agentId}
onChange={(e) => setForm({ ...form, agentId: e.target.value })}
options={[
{ value: '', label: '-- select --' },
...appAgents.map((a) => ({ value: a.instanceId ?? '', label: a.displayName ?? a.instanceId ?? '' })),
]}
/>
</FormField>
)}
</div>
);
}