Files
cameleer-server/ui/src/pages/Alerts/RuleEditor/ScopeStep.tsx
hsiegeln e590682f8f
All checks were successful
CI / cleanup-branch (push) Has been skipped
CI / build (push) Successful in 2m3s
CI / docker (push) Successful in 1m22s
CI / deploy-feature (push) Has been skipped
CI / deploy (push) Successful in 41s
refactor(ui/alerts): address code-review findings on alerting-enums
Follow-up to 83837ada addressing the critical-review feedback:

- Duplicate ConditionKind type consolidated: the one in
  api/queries/alertRules.ts (which was nullable — wrong) is gone;
  single source of truth lives in this module.

- Module moved out of api/ into pages/Alerts/ where it belongs.
  api/ is the data layer; labels + hide lists are view-layer concerns.

- Hidden values formalised: Comparator.EQ and JvmAggregation.LATEST
  are intentionally not surfaced in dropdowns (noisy / wrong feature
  boundary, see in-file comments). They remain in the type unions so
  rules that carry those values save/load correctly — we just don't
  advertise them in the UI.

- JvmAggregation declaration order restored to MAX/AVG/MIN (matches
  what users saw before 83837ada). LATEST declared last; hidden.

- Snapshot tests for every visible *_OPTIONS array — reviewer signal
  in future PRs when a backend enum change or hide-list edit
  silently reshapes the dropdown.

- `toOptions` gains a JSDoc noting that label-map declaration order
  is load-bearing (ES2015 Object.keys insertion-order guarantee).

- **Honest about the springdoc schema quirk**: the generated
  polymorphic condition types resolve to `never` at the TypeScript
  level (two conflicting `kind` discriminators — the class-name
  literal and the Jackson enum — intersect to never), which silently
  defeated `Record<T, string>` exhaustiveness. The previous commit's
  "schema-derived enums" claim was accurate only for the flat-field
  enums (ConditionKind, Severity, TargetKind); condition-specific
  enums (RouteMetric, Comparator, JvmAggregation, ExchangeFireMode)
  were silently `never`. Those are now declared as hand-written
  string-literal unions with a top-of-file comment spelling out the
  issue and the regen-and-compare workflow. Real upstream fix is a
  backend-side adjustment to how springdoc emits polymorphic
  `@JsonSubTypes` — out of scope for this phase.

Verified: ui build green, 56/56 vitest pass (49 pre-existing + 7
new enum snapshots).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 19:26:16 +02:00

99 lines
3.4 KiB
TypeScript

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';
import { SEVERITY_OPTIONS } from '../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>
);
}