diff --git a/.changeset/tile-alert-opens-alert-panel.md b/.changeset/tile-alert-opens-alert-panel.md new file mode 100644 index 0000000000..57cd6217dc --- /dev/null +++ b/.changeset/tile-alert-opens-alert-panel.md @@ -0,0 +1,7 @@ +--- +'@hyperdx/app': patch +--- + +fix: the alert button on a dashboard tile opens the tile editor with the alert +panel showing, on the tile's alert or on a new one, instead of an editor where +"Add alert" had to be clicked again diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index f5000d1a75..2abe77e4ab 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -446,6 +446,7 @@ const Tile = ({ dateRange, onDuplicateClick, onEditClick, + onAlertClick, onDeleteClick, onUpdateChart, onMoveToGroup, @@ -477,7 +478,7 @@ const Tile = ({ dateRange: [Date, Date]; onDuplicateClick: () => void; onEditClick: () => void; - onAddAlertClick?: () => void; + onAlertClick: () => void; onDeleteClick: () => void; onUpdateChart?: (chart: Tile) => void; onMoveToGroup?: (containerId: string | undefined, tabId?: string) => void; @@ -983,7 +984,7 @@ const Tile = ({ data-testid={`tile-alerts-button-${chart.id}`} variant="subtle" size="sm" - onClick={onEditClick} + onClick={onAlertClick} > @@ -997,7 +998,7 @@ const Tile = ({ data-testid={`tile-alerts-button-${chart.id}`} variant="subtle" size="sm" - onClick={onEditClick} + onClick={onAlertClick} mr={4} > @@ -1080,6 +1081,7 @@ const Tile = ({ onDeleteClick, onDuplicateClick, onEditClick, + onAlertClick, onMoveToGroup, openFullscreen, readOnly, @@ -1119,7 +1121,7 @@ const Tile = ({ leftSection={ alert ? : } - onClick={onEditClick} + onClick={onAlertClick} > {alertTooltip} @@ -1170,6 +1172,7 @@ const Tile = ({ onDeleteClick, onDuplicateClick, onEditClick, + onAlertClick, onMoveToGroup, openFullscreen, readOnly, @@ -1618,6 +1621,7 @@ const Tile = ({ const EditTileModal = ({ dashboardId, chart, + defaultAlertPanelOpened, onClose, onSave, isSaving, @@ -1628,6 +1632,7 @@ const EditTileModal = ({ }: { dashboardId?: string; chart: Tile | undefined; + defaultAlertPanelOpened?: boolean; onClose: () => void; dateRange: [Date, Date]; isSaving?: boolean; @@ -1714,6 +1719,7 @@ const EditTileModal = ({ onDirtyChange={setHasUnsavedChanges} isDashboardForm autoRun + defaultAlertPanelOpened={defaultAlertPanelOpened} /> @@ -2233,6 +2239,7 @@ function DBDashboardPage({ }, [dashboard, isLocalDashboard, setDashboard]); const [editedTile, setEditedTile] = useState(); + const [isEditingTileAlert, setIsEditingTileAlert] = useState(false); const containers = useMemo( () => dashboard?.containers ?? [], @@ -2434,7 +2441,14 @@ function DBDashboardPage({ key={chart.id} chart={chart} dateRange={searchedTimeRange} - onEditClick={() => setEditedTile(chart)} + onEditClick={() => { + setIsEditingTileAlert(false); + setEditedTile(chart); + }} + onAlertClick={() => { + setIsEditingTileAlert(true); + setEditedTile(chart); + }} readOnly={isKioskMode} isLive={isRefreshEnabled} granularity={ @@ -2737,6 +2751,7 @@ function DBDashboardPage({ return !t.containerId; }); const pos = calculateNextTilePosition(targetTiles, newW); + setIsEditingTileAlert(false); setEditedTile({ id: makeId(), x: pos.x, @@ -3233,6 +3248,7 @@ function DBDashboardPage({ { if (!isSaving) setEditedTile(undefined); }} diff --git a/packages/app/src/components/AlertPanel.tsx b/packages/app/src/components/AlertPanel.tsx index 4de9aa501d..7d72b97cf4 100644 --- a/packages/app/src/components/AlertPanel.tsx +++ b/packages/app/src/components/AlertPanel.tsx @@ -133,6 +133,7 @@ const AlertPanelContext = createContext< /** Lays out its children beside the alert panel. */ export function AlertPanelLayout({ defaultOpened = false, + defaultIsDraft = false, actions, onOpen, onCancel, @@ -140,6 +141,8 @@ export function AlertPanelLayout({ ...props }: BoxProps & { defaultOpened?: boolean; + /** Whether the panel opens on a new alert, which closing it discards. */ + defaultIsDraft?: boolean; /** * The alert's primary action. The alert form places it under its inputs * with `AlertPanelActions`, so it reads as part of the alert rather than @@ -153,7 +156,7 @@ export function AlertPanelLayout({ children: ReactNode; }) { const [opened, setOpened] = useState(defaultOpened); - const [isDraft, setIsDraft] = useState(false); + const [isDraft, setIsDraft] = useState(defaultIsDraft); const [target, setTarget] = useState(null); const [hasContent, setHasContent] = useState(false); const [root, setRoot] = useState(null); diff --git a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx index 3fb9440fa7..30db17810d 100644 --- a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx @@ -16,6 +16,7 @@ import { displayTypeSupportsBuilderAlerts, displayTypeSupportsRawSqlAlerts, isTimeSeriesDisplayType, + savedChartConfigSupportsAlerts, } from '@hyperdx/common-utils/dist/core/utils'; import { displayTypeRequiresSource, @@ -90,7 +91,7 @@ import { firstSourceItemValue, useFilteredSortedSourceItems, } from '@/components/sourceSelectUtils'; -import { IS_PROMQL_ENABLED } from '@/config'; +import { IS_LOCAL_MODE, IS_PROMQL_ENABLED } from '@/config'; import HDXMarkdownChart from '@/HDXMarkdownChart'; import { getDurationMsExpression, @@ -98,7 +99,10 @@ import { useSource, useSources, } from '@/source'; -import { normalizeNoOpAlertScheduleFields } from '@/utils/alerts'; +import { + DEFAULT_TILE_ALERT, + normalizeNoOpAlertScheduleFields, +} from '@/utils/alerts'; import { AlertPanelPrimaryAction } from './AlertPanelPrimaryAction'; import { ChartActionBar, DashboardFiltersToggleProps } from './ChartActionBar'; @@ -158,6 +162,11 @@ type EditTimeChartFormProps = { isSavingAlert?: boolean; /** Hides the alert editor's remove control, for surfaces that require one. */ isAlertRequired?: boolean; + /** + * Opens with the alert panel showing, as the alert button does: on the + * chart's alert, or on a new draft where the editor offers alerts. + */ + defaultAlertPanelOpened?: boolean; /** Whether to offer "Save to dashboard". Defaults to "outside a dashboard". */ showSaveToDashboard?: boolean; }; @@ -241,13 +250,29 @@ export default function EditTimeChartForm({ saveAlertLabel, isSavingAlert, isAlertRequired = false, + defaultAlertPanelOpened = false, showSaveToDashboard, }: EditTimeChartFormProps) { const alertsEnabled = enableAlerts ?? dashboardId != null; - const formValue: ChartEditorFormState = useMemo( - () => convertSavedChartConfigToFormState(chartConfig), - [chartConfig], - ); + // Opened from a tile's alert button, a chart without an alert starts on a + // new one where the editor offers alerts, as if Add alert were clicked. + const opensOnNewAlert = + defaultAlertPanelOpened && + chartConfig.alert == null && + alertsEnabled && + !IS_LOCAL_MODE && + savedChartConfigSupportsAlerts(chartConfig); + const formValue: ChartEditorFormState = useMemo(() => { + const value = convertSavedChartConfigToFormState(chartConfig); + if (!opensOnNewAlert) return value; + return { + ...value, + alert: { + ...DEFAULT_TILE_ALERT, + ...(value.name && { displayName: value.name }), + }, + }; + }, [chartConfig, opensOnNewAlert]); const { control, @@ -630,9 +655,12 @@ export default function EditTimeChartForm({ ], ); + // Until the chart has run, a new alert from a tile's alert button doesn't + // stop it running: the alert has no webhook yet. + const ignoresNewAlert = opensOnNewAlert && queriedConfig == null; const onSubmit = useCallback( (suppressErrorNotification: boolean = false) => { - handleSubmit(form => { + const runChart = (form: ChartEditorFormState) => { const { errors, config } = validateAndNormalize(form); if (errors.length > 0) { if (!suppressErrorNotification) { @@ -661,11 +689,25 @@ export default function EditTimeChartForm({ isRawSqlChart ? undefined : tableSource, ); } - })(); + }; + handleSubmit( + runChart, + // The resolver only checks the alert. The chart runs without it, and + // the alert's errors are neither shown nor focused. + ignoresNewAlert + ? () => { + clearErrors('alert'); + runChart({ ...getValues(), alert: undefined }); + } + : undefined, + )(); }, [ validateAndNormalize, handleSubmit, + ignoresNewAlert, + clearErrors, + getValues, setChartConfig, setQueriedConfigAndSource, tableSource, @@ -1110,10 +1152,11 @@ export default function EditTimeChartForm({ return ( // The panel opens on request, except where the alert is what is being - // edited (the inline-alert modal). + // edited (the inline-alert modal) or what the editor was opened for. { + // Runnable, like a saved tile: the tile editor runs it on open. + const tileConfig: SavedChartConfig = { + ...defaultChartConfig, + displayType: DisplayType.Number, + select: [ + { + aggFn: 'avg', + aggCondition: '', + aggConditionLanguage: 'lucene' as const, + valueExpression: 'Value', + metricType: MetricsDataType.Gauge, + metricName: 'test.metric.gauge', + }, + ], + }; + + const renderFromAlertButton = ( + props: Partial> = {}, + ) => + renderAlertComponent({ + chartConfig: tileConfig, + autoRun: true, + defaultAlertPanelOpened: true, + ...props, + }); + + it('runs the chart and starts on a new alert', async () => { + renderFromAlertButton(); + + expect(await screen.findByText('Number Chart')).toBeInTheDocument(); + expect(screen.getByTestId('alert-panel')).toContainElement( + screen.getByTestId('alert-details'), + ); + // The new alert has no webhook yet, which the first run ignores. + expect( + screen.queryByText("Webhook ID can't be empty"), + ).not.toBeInTheDocument(); + expect(screen.getByTestId('select-webhook')).not.toHaveFocus(); + expect( + screen.getByTestId('alert-panel-confirm-button'), + ).toHaveTextContent('Add alert'); + expect(screen.getByTestId('alert-display-name-input')).toHaveValue( + 'Test Chart', + ); + }); + + it('starts on a new alert for a chart it cannot run yet', async () => { + // No source to run against, so nothing runs on open. + renderFromAlertButton({ + chartConfig: { ...tileConfig, source: 'missing-source' }, + }); + + expect(await screen.findByTestId('alert-panel')).toContainElement( + screen.getByTestId('alert-details'), + ); + }); + + it('ignores the new alert on a first run by hand', async () => { + // As on a tile with nothing to run on open, like raw SQL with no source. + renderFromAlertButton({ autoRun: false }); + + await userEvent.click( + await screen.findByTestId('chart-run-query-button'), + ); + + expect(await screen.findByText('Number Chart')).toBeInTheDocument(); + expect( + screen.queryByText("Webhook ID can't be empty"), + ).not.toBeInTheDocument(); + expect(screen.getByTestId('select-webhook')).not.toHaveFocus(); + }); + + it('discards that alert when its panel is closed', async () => { + renderFromAlertButton(); + + await userEvent.click( + await screen.findByTestId('close-alert-panel-button'), + ); + + expect(screen.queryByTestId('alert-details')).not.toBeInTheDocument(); + expect(screen.getByTestId('alert-button')).toHaveTextContent('Add alert'); + }); + + it('adds no alert where the editor offers none', async () => { + renderFromAlertButton({ dashboardId: undefined }); + + expect(await screen.findByText('Number Chart')).toBeInTheDocument(); + expect(screen.queryByTestId('alert-panel')).not.toBeInTheDocument(); + expect(screen.queryByTestId('alert-details')).not.toBeInTheDocument(); + }); + }); + describe('with an existing alert', () => { const renderWithExistingAlert = ( alert: NonNullable, @@ -679,6 +772,17 @@ describe('DBEditTimeChartForm - Add/delete alerts for display type Number', () = ).toHaveTextContent('Update alert'); }); + it('starts on it when opened from a tile alert button', () => { + renderWithExistingAlert(completeAlert, { defaultAlertPanelOpened: true }); + + expect(screen.getByTestId('alert-panel')).toContainElement( + screen.getByTestId('alert-details'), + ); + expect( + screen.getByTestId('alert-panel-confirm-button'), + ).toHaveTextContent('Update alert'); + }); + it('keeps the alert when the panel is closed', async () => { renderWithExistingAlert(completeAlert); diff --git a/packages/app/src/components/__tests__/AlertPanel.test.tsx b/packages/app/src/components/__tests__/AlertPanel.test.tsx index 0546279d1b..68e28adfaa 100644 --- a/packages/app/src/components/__tests__/AlertPanel.test.tsx +++ b/packages/app/src/components/__tests__/AlertPanel.test.tsx @@ -21,9 +21,11 @@ function Controls() { function renderLayout({ defaultOpened = false, + defaultIsDraft = false, withContent = true, }: { defaultOpened?: boolean; + defaultIsDraft?: boolean; withContent?: boolean; }) { const onOpen = jest.fn(); @@ -31,6 +33,7 @@ function renderLayout({ renderWithMantine( } @@ -119,6 +122,18 @@ describe('AlertPanel', () => { expect(screen.queryByTestId('alert-panel')).not.toBeInTheDocument(); }); + it('discards a draft it opens on, closed without confirming', () => { + const { onCancel } = renderLayout({ + defaultOpened: true, + defaultIsDraft: true, + }); + + click('Close'); + + expect(onCancel).toHaveBeenCalledWith(true); + expect(screen.queryByTestId('alert-panel')).not.toBeInTheDocument(); + }); + it('snapshots an existing alert as the panel opens on it', () => { const { onOpen } = renderLayout({});