Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

ai: replace legacy segmentedButtonClass with canonical FilterSegmentedControl

rcourtmanbe7156c46225
diff --git a/frontend-modern/src/components/AI/AICostDashboard.tsx b/frontend-modern/src/components/AI/AICostDashboard.tsx
index 7b9f16cdd7..5887d20bbf 100644
--- a/frontend-modern/src/components/AI/AICostDashboard.tsx
+++ b/frontend-modern/src/components/AI/AICostDashboard.tsx
@@ -9,6 +9,7 @@ import {
TableCell,
} from '@/components/shared/Table';
import { SectionHeader } from '@/components/shared/SectionHeader';
+import { FilterSegmentedControl } from '@/components/shared/FilterToolbar';
import { AIAPI } from '@/api/ai';
import { aiRuntimeSettings, loadAIRuntimeSettings } from '@/stores/aiRuntimeState';
import { formatNumber } from '@/utils/format';
@@ -31,7 +32,6 @@ import {
getAICostExportHistoryErrorMessage,
getAICostLoadingState,
getAICostRefreshErrorMessage,
- getAICostRangeButtonClass,
getAICostResetHistoryConfirmationMessage,
getAICostResetHistoryErrorMessage,
getAICostResetHistorySuccessMessage,
@@ -312,48 +312,19 @@ export const AICostDashboard: Component = () => {
<Show when={loading()}>
<div class="text-xs text-muted">Loading…</div>
</Show>
- <div class="flex items-center gap-1">
- <button
- type="button"
- disabled={loading()}
- onClick={() => handleRangeClick(1)}
- class={getAICostRangeButtonClass(days() === 1, loading())}
- >
- 1d
- </button>
- <button
- type="button"
- disabled={loading()}
- onClick={() => handleRangeClick(7)}
- class={getAICostRangeButtonClass(days() === 7, loading())}
- >
- 7d
- </button>
- <button
- type="button"
- disabled={loading()}
- onClick={() => handleRangeClick(30)}
- class={getAICostRangeButtonClass(days() === 30, loading())}
- >
- 30d
- </button>
- <button
- type="button"
- disabled={loading()}
- onClick={() => handleRangeClick(90)}
- class={getAICostRangeButtonClass(days() === 90, loading())}
- >
- 90d
- </button>
- <button
- type="button"
- disabled={loading()}
- onClick={() => handleRangeClick(365)}
- class={getAICostRangeButtonClass(days() === 365, loading())}
- >
- 1y
- </button>
- </div>
+ <FilterSegmentedControl
+ aria-label="Cost range"
+ value={String(days())}
+ disabled={loading()}
+ onChange={(value) => handleRangeClick(Number(value))}
+ options={[
+ { value: '1', label: '1d' },
+ { value: '7', label: '7d' },
+ { value: '30', label: '30d' },
+ { value: '90', label: '90d' },
+ { value: '365', label: '1y' },
+ ]}
+ />
</div>
</div>
diff --git a/frontend-modern/src/components/AI/FindingsPanel.tsx b/frontend-modern/src/components/AI/FindingsPanel.tsx
index badf0d683e..83df71b7ef 100644
--- a/frontend-modern/src/components/AI/FindingsPanel.tsx
+++ b/frontend-modern/src/components/AI/FindingsPanel.tsx
@@ -37,7 +37,7 @@ import { createSuppressionRuleFromFinding, reinvestigateFinding } from '@/api/pa
import type { PatrolRunRecord, PatrolRuntimeState } from '@/api/patrol';
import { formatRelativeTime } from '@/utils/format';
import { getFindingAlertIdentifier, hasTriggeringAlert } from '@/utils/findingAlertIdentity';
-import { segmentedButtonClass } from '@/utils/segmentedButton';
+import { FilterSegmentedControl, type FilterSegmentOption } from '@/components/shared/FilterToolbar';
import { getSemanticTonePresentation } from '@/utils/semanticTonePresentation';
import { formatIdentifierLabel } from '@/utils/textPresentation';
import type { IntelligenceHealthScore } from '@/types/aiIntelligence';
@@ -89,12 +89,20 @@ const FINDING_ROW_BADGE_PROPS = {
shape: 'rounded',
} as const;
+export type FindingsPanelFilter =
+ | 'all'
+ | 'active'
+ | 'resolved'
+ | 'approvals'
+ | 'attention'
+ | 'overdue';
+
interface FindingsPanelProps {
resourceId?: string;
showResolved?: boolean;
maxItems?: number;
onFindingClick?: (finding: UnifiedFinding) => void;
- filterOverride?: 'all' | 'active' | 'resolved' | 'approvals' | 'attention' | 'overdue';
+ filterOverride?: FindingsPanelFilter;
filterFindingIds?: string[];
showControls?: boolean;
scopeResourceIds?: string[];
@@ -144,9 +152,7 @@ function getFindingSuppressionRuleScope(finding: UnifiedFinding) {
export const FindingsPanel: Component<FindingsPanelProps> = (props) => {
const location = useLocation();
- const [filter, setFilter] = createSignal<
- 'all' | 'active' | 'resolved' | 'approvals' | 'attention' | 'overdue'
- >(props.filterOverride ?? 'active');
+ const [filter, setFilter] = createSignal<FindingsPanelFilter>(props.filterOverride ?? 'active');
const [sortBy, setSortBy] = createSignal<'severity' | 'time'>('severity');
const [expandedId, setExpandedId] = createSignal<string | null>(null);
const [actionLoading, setActionLoading] = createSignal<string | null>(null);
@@ -447,6 +453,26 @@ export const FindingsPanel: Component<FindingsPanelProps> = (props) => {
return buildPatrolFindingDisplayGroups(findings);
});
const filterOptions = createMemo(() => buildFindingFilterOptions(filterCounts()));
+ const filterSegments = createMemo<FilterSegmentOption[]>(() => {
+ const segments: FilterSegmentOption[] = filterOptions().map((option) => ({
+ value: option.value,
+ tone: option.tone,
+ label: (
+ <>
+ {option.label}
+ <Show when={typeof option.count === 'number'}>{` (${option.count})`}</Show>
+ </>
+ ),
+ }));
+ if (overdueCount() > 0) {
+ segments.push({
+ value: 'overdue',
+ tone: 'warning',
+ label: <>Overdue commitments ({overdueCount()})</>,
+ });
+ }
+ return segments;
+ });
const emptyStateCopy = createMemo(() => {
// 'overdue' is a FindingsPanel-local extension to the shared
// FindingsFilter union. The empty state for that case is rendered
@@ -1826,37 +1852,12 @@ export const FindingsPanel: Component<FindingsPanelProps> = (props) => {
{/* Controls */}
<Show when={showFilterControls()}>
<div class="flex items-center justify-between">
- <div class="flex text-xs">
- <For each={filterOptions()}>
- {(option, index) => {
- const isFirst = () => index() === 0;
- const isLast = () => index() === filterOptions().length - 1 && overdueCount() === 0;
- return (
- <button
- type="button"
- onClick={() => setFilter(option.value)}
- class={`px-2 py-1 border ${
- isFirst() ? 'rounded-l border-r-0' : isLast() ? 'rounded-r' : 'border-r-0'
- } ${segmentedButtonClass(filter() === option.value, false, option.tone ?? 'default')}`}
- >
- {option.label}
- <Show when={typeof option.count === 'number'}>{` (${option.count})`}</Show>
- </button>
- );
- }}
- </For>
- <Show when={overdueCount() > 0}>
- <button
- type="button"
- data-testid="findings-panel-filter-overdue"
- onClick={() => setFilter('overdue')}
- class={`px-2 py-1 border rounded-r ${segmentedButtonClass(filter() === 'overdue', false, 'warning')}`}
- title="Will-fix-later commitments past their remind deadline"
- >
- Overdue commitments ({overdueCount()})
- </button>
- </Show>
- </div>
+ <FilterSegmentedControl
+ aria-label="Filter findings"
+ value={filter()}
+ onChange={(value) => setFilter(value as FindingsPanelFilter)}
+ options={filterSegments()}
+ />
<Show when={patrolFindingDisplayGroups().length > 1}>
<FormSelect
label="Sort findings"
diff --git a/frontend-modern/src/components/AI/__tests__/FindingsPanel.overdue.test.tsx b/frontend-modern/src/components/AI/__tests__/FindingsPanel.overdue.test.tsx
index a70084d363..d2d4db9b05 100644
--- a/frontend-modern/src/components/AI/__tests__/FindingsPanel.overdue.test.tsx
+++ b/frontend-modern/src/components/AI/__tests__/FindingsPanel.overdue.test.tsx
@@ -173,7 +173,7 @@ describe('FindingsPanel overdue commitments filter', () => {
await waitFor(() => expect(mockState.loadPatrolFindings).toHaveBeenCalled());
// The Overdue chip appears in the filter bar.
- const overdueChip = screen.getByTestId('findings-panel-filter-overdue');
+ const overdueChip = screen.getByRole('button', { name: /overdue commitments/i });
expect(overdueChip).toHaveTextContent(/Overdue commitments \(1\)/);
fireEvent.click(overdueChip);
@@ -200,7 +200,9 @@ describe('FindingsPanel overdue commitments filter', () => {
render(() => <FindingsPanel findingsSource="patrol" />);
await waitFor(() => expect(mockState.loadPatrolFindings).toHaveBeenCalled());
- expect(screen.queryByTestId('findings-panel-filter-overdue')).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole('button', { name: /overdue commitments/i }),
+ ).not.toBeInTheDocument();
});
it('honors filterOverride="overdue" so callers can land directly on the filter', async () => {
diff --git a/frontend-modern/src/components/shared/FilterButtonGroup.tsx b/frontend-modern/src/components/shared/FilterButtonGroup.tsx
index 4ceb1f691b..22f072812a 100644
--- a/frontend-modern/src/components/shared/FilterButtonGroup.tsx
+++ b/frontend-modern/src/components/shared/FilterButtonGroup.tsx
@@ -10,6 +10,7 @@ import { useFilterButtonGroupState } from './useFilterButtonGroupState';
export type {
FilterButtonGroupProps,
+ FilterButtonGroupOptionTone,
FilterOption,
} from './filterButtonGroupModel';
diff --git a/frontend-modern/src/components/shared/FilterToolbar.tsx b/frontend-modern/src/components/shared/FilterToolbar.tsx
index 7826e5e340..e5b53daac3 100644
--- a/frontend-modern/src/components/shared/FilterToolbar.tsx
+++ b/frontend-modern/src/components/shared/FilterToolbar.tsx
@@ -2,7 +2,7 @@ import { Component, For, JSX, Show, splitProps } from 'solid-js';
import BarChartIcon from 'lucide-solid/icons/bar-chart';
import ListFilterIcon from 'lucide-solid/icons/list-filter';
import { FormSelect } from './FormSelect';
-import { FilterButtonGroup, type FilterOption } from './FilterButtonGroup';
+import { FilterButtonGroup, type FilterButtonGroupOptionTone, type FilterOption } from './FilterButtonGroup';
export const filterToolbarShellClass = '';
export const filterToolbarRowClass = 'flex flex-wrap items-center gap-2 text-xs text-muted';
@@ -190,6 +190,7 @@ export interface FilterSegmentOption {
ariaLabel?: string;
leading?: JSX.Element;
disabled?: boolean;
+ tone?: FilterButtonGroupOptionTone;
}
export const COMPACT_FILTER_TOGGLE_MAX_OPTIONS = 5;
@@ -204,10 +205,11 @@ interface FilterSegmentedControlProps extends Omit<JSX.HTMLAttributes<HTMLDivEle
onChange: (value: string) => void;
options: FilterSegmentOption[];
label?: JSX.Element;
+ disabled?: boolean;
}
export const FilterSegmentedControl: Component<FilterSegmentedControlProps> = (props) => {
- const [local, divProps] = splitProps(props, ['value', 'onChange', 'options', 'label', 'class']);
+ const [local, divProps] = splitProps(props, ['value', 'onChange', 'options', 'label', 'class', 'disabled']);
const options = (): FilterOption<string>[] =>
local.options.map((option) => ({
value: option.value,
@@ -219,6 +221,7 @@ export const FilterSegmentedControl: Component<FilterSegmentedControlProps> = (p
title: option.title,
ariaLabel: option.ariaLabel,
disabled: option.disabled,
+ tone: option.tone,
}));
return (
@@ -230,6 +233,7 @@ export const FilterSegmentedControl: Component<FilterSegmentedControlProps> = (p
onChange={local.onChange}
ariaLabel={divProps['aria-label'] as string | undefined}
variant="compact"
+ disabled={local.disabled}
options={options()}
/>
);
diff --git a/frontend-modern/src/utils/__tests__/aiCostPresentation.test.ts b/frontend-modern/src/utils/__tests__/aiCostPresentation.test.ts
index d9b38dbd91..5e7b9853f7 100644
--- a/frontend-modern/src/utils/__tests__/aiCostPresentation.test.ts
+++ b/frontend-modern/src/utils/__tests__/aiCostPresentation.test.ts
@@ -16,7 +16,6 @@ import {
getAICostExportHistoryErrorMessage,
getAICostLoadingState,
getAICostRefreshErrorMessage,
- getAICostRangeButtonClass,
getAICostResetHistoryConfirmationMessage,
getAICostResetHistoryErrorMessage,
getAICostResetHistorySuccessMessage,
@@ -24,12 +23,6 @@ import {
} from '@/utils/aiCostPresentation';
describe('aiCostPresentation', () => {
- it('returns canonical AI cost range button classes', () => {
- expect(getAICostRangeButtonClass(true)).toContain('inline-flex items-center');
- expect(getAICostRangeButtonClass(true)).toContain('bg-surface');
- expect(getAICostRangeButtonClass(false, true)).toContain('cursor-not-allowed');
- });
-
it('exports canonical AI cost presentation copy', () => {
expect(AI_COST_PANEL_TITLE).toBe('Provider Usage & Spend');
expect(AI_COST_PANEL_DESCRIPTION).toBe(
diff --git a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
index 29c6415fec..a0b62dae85 100644
--- a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
+++ b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
@@ -3753,7 +3753,8 @@ describe('frontend resource type boundaries', () => {
expect(thresholdSliderPresentationSource).toContain(
'export function getThresholdSliderFillClass',
);
- expect(aiCostDashboardSource).toContain('getAICostRangeButtonClass');
+ expect(aiCostDashboardSource).toContain('FilterSegmentedControl');
+ expect(aiCostDashboardSource).not.toContain('getAICostRangeButtonClass');
expect(aiCostDashboardSource).toContain('getAICostLoadingState');
expect(aiCostDashboardSource).toContain('AI_COST_PANEL_TITLE');
expect(aiCostDashboardSource).toContain('AI_COST_PANEL_DESCRIPTION');
diff --git a/frontend-modern/src/utils/__tests__/segmentedButton.test.ts b/frontend-modern/src/utils/__tests__/segmentedButton.test.ts
deleted file mode 100644
index 7f9035e612..0000000000
--- a/frontend-modern/src/utils/__tests__/segmentedButton.test.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-import { describe, expect, it } from 'vitest';
-import { segmentedButtonClass } from '@/utils/segmentedButton';
-
-describe('segmentedButtonClass', () => {
- it('returns selected classes when selected is true', () => {
- const result = segmentedButtonClass(true);
- expect(result).toContain('bg-surface');
- expect(result).toContain('shadow-sm');
- expect(result).toContain('ring-1');
- });
-
- it('returns accent selected classes when tone is accent', () => {
- const result = segmentedButtonClass(true, false, 'accent');
- expect(result).toContain('bg-blue-600');
- expect(result).toContain('text-white');
- });
-
- it('returns warning selected classes when tone is warning', () => {
- const result = segmentedButtonClass(true, false, 'warning');
- expect(result).toContain('bg-amber-50');
- expect(result).toContain('text-amber-700');
- expect(result).toContain('border-amber-300');
- });
-
- it('returns unselected classes when selected is false', () => {
- const result = segmentedButtonClass(false);
- expect(result).toContain('hover:text-base-content');
- expect(result).toContain('hover:bg-surface-hover');
- });
-
- it('returns disabled classes when disabled is true', () => {
- const result = segmentedButtonClass(true, true);
- expect(result).toContain('cursor-not-allowed');
- expect(result).toContain('text-muted');
- });
-
- it('returns disabled classes with selected true but disabled true', () => {
- const result = segmentedButtonClass(true, true);
- expect(result).toContain('cursor-not-allowed');
- });
-
- it('includes transition classes for animation', () => {
- const result = segmentedButtonClass(false);
- expect(result).toContain('transition-all');
- expect(result).toContain('duration-150');
- });
-
- it('includes active scale transform', () => {
- const result = segmentedButtonClass(false);
- expect(result).toContain('active:scale-95');
- });
-
- it('includes flex layout', () => {
- const result = segmentedButtonClass(false);
- expect(result).toContain('inline-flex');
- expect(result).toContain('items-center');
- expect(result).toContain('gap-1.5');
- });
-
- it('includes text styling', () => {
- const result = segmentedButtonClass(false);
- expect(result).toContain('text-xs');
- expect(result).toContain('font-medium');
- expect(result).toContain('rounded-md');
- });
-});
diff --git a/frontend-modern/src/utils/aiCostPresentation.ts b/frontend-modern/src/utils/aiCostPresentation.ts
index 918018336a..0ab1e26032 100644
--- a/frontend-modern/src/utils/aiCostPresentation.ts
+++ b/frontend-modern/src/utils/aiCostPresentation.ts
@@ -1,5 +1,3 @@
-import { segmentedButtonClass } from '@/utils/segmentedButton';
-
export const AI_COST_PANEL_TITLE = 'Provider Usage & Spend';
export const AI_COST_PANEL_DESCRIPTION =
'Token usage and estimated spend across providers backing Pulse Assistant and Patrol.';
@@ -56,10 +54,6 @@ export function buildAICostExportFilename(
return `pulse-provider-usage-${now.toISOString().split('T')[0]}-${rangeDays}d.${format}`;
}
-export function getAICostRangeButtonClass(selected: boolean, disabled = false): string {
- return `min-h-10 sm:min-h-9 min-w-10 border px-2.5 py-2 text-sm ${segmentedButtonClass(selected, disabled)}`;
-}
-
export interface AICostTargetPresentationInput {
target_type?: string | null;
target_id?: string | null;
diff --git a/frontend-modern/src/utils/segmentedButton.ts b/frontend-modern/src/utils/segmentedButton.ts
deleted file mode 100644
index 78fc0fd684..0000000000
--- a/frontend-modern/src/utils/segmentedButton.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-export type SegmentedButtonTone = 'default' | 'accent' | 'warning';
-
-/**
- * Returns consistent CSS classes for segmented toggle buttons used in filter bars.
- * Includes flex/icon layout, spacing, animation, and selected/unselected visual states.
- *
- * Usage:
- * <button class={segmentedButtonClass(isSelected)}>Label</button>
- * <button class={segmentedButtonClass(isSelected, isDisabled)}>Label</button>
- * <button class={segmentedButtonClass(isSelected, isDisabled, 'accent')}>Label</button>
- */
-export const segmentedButtonClass = (
- selected: boolean,
- disabled = false,
- tone: SegmentedButtonTone = 'default',
-): string => {
- const base =
- 'inline-flex items-center gap-1.5 px-2 py-1 text-xs font-medium rounded-md transition-all duration-150 active:scale-95';
- if (disabled) {
- return `${base} text-muted cursor-not-allowed`;
- }
- if (selected) {
- if (tone === 'accent') {
- return `${base} bg-blue-600 text-white`;
- }
- if (tone === 'warning') {
- return `${base} bg-amber-50 dark:bg-amber-900 text-amber-700 dark:text-amber-300 border-amber-300 dark:border-amber-700 shadow-sm`;
- }
- return `${base} bg-surface text-base-content shadow-sm ring-1 ring-border`;
- }
- return `${base} text-muted hover:text-base-content hover:bg-surface-hover`;
-};