Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(alerts): let users clear the anomaly threshold input (#126528)

posthog[bot]616d29e8ca3c
diff --git a/products/alerts/frontend/views/DetectorSelector.test.tsx b/products/alerts/frontend/views/DetectorSelector.test.tsx
new file mode 100644
index 000000000000..d980461624e9
--- /dev/null
+++ b/products/alerts/frontend/views/DetectorSelector.test.tsx
@@ -0,0 +1,26 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+
+import { getDefaultZScoreDetectorConfig } from '../logic/detectorConfigDefaults'
+import { DetectorSelector } from './DetectorSelector'
+
+describe('DetectorSelector', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ it.each([
+ ['an intermediate zero while backspacing', '0', 0],
+ ['a valid threshold', '0.8', 0.8],
+ ['an empty field', '', 0.95],
+ ])('commits %s to the anomaly threshold', (_, typed, expected) => {
+ const onChange = jest.fn()
+ render(<DetectorSelector value={getDefaultZScoreDetectorConfig(30)} onChange={onChange} />)
+
+ const thresholdInput = screen.getByDisplayValue('0.95')
+ fireEvent.change(thresholdInput, { target: { value: typed } })
+
+ expect(onChange).toHaveBeenLastCalledWith(expect.objectContaining({ threshold: expected }))
+ })
+})
diff --git a/products/alerts/frontend/views/DetectorSelector.tsx b/products/alerts/frontend/views/DetectorSelector.tsx
index 8d57500796c5..8166d4c2ea1e 100644
--- a/products/alerts/frontend/views/DetectorSelector.tsx
+++ b/products/alerts/frontend/views/DetectorSelector.tsx
@@ -572,7 +572,9 @@ function AnomalyThresholdInput({ value, onChange }: { value: number; onChange: (
max={0.99}
step={0.05}
value={value}
- onChange={(val) => onChange(val ? parseFloat(String(val)) : DEFAULT_ANOMALY_DETECTION_THRESHOLD)}
+ onChange={(val) =>
+ onChange(val === undefined || Number.isNaN(val) ? DEFAULT_ANOMALY_DETECTION_THRESHOLD : val)
+ }
/>
</div>
)