Skip to content

Commit c048ae2

Browse files
fix(app): clear exemplar cards when the plot itself goes away
Addresses a review finding on the previous fix. Loading, error, formatting-error and empty-data states replace the chart's whole subtree, so the marker layer unmounts and nothing can report a position. The card state survived that, so recovering left the card pinned at coordinates belonging to the previous chart instance until a remounted marker happened to report — and if the marker was gone from the new data, until the rendered-set guard fired. Cleared on the same signal now, alongside the existing display-type case: both are "the markers a card was anchored to no longer exist". The branch condition is derived once and shared with the JSX rather than restated, so the cleanup cannot drift from the branches that cause it.
1 parent b6c3487 commit c048ae2

3 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎packages/app/src/components/DBTimeChart/DBTimeChart.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ function DBTimeChartComponent({
299299
}
300300
}, [displayTypeLocal, displayTypeProp, setDisplayType]);
301301

302+
// The chart replaces its whole subtree — marker layer and exemplar card
303+
// included — for the loading, error and empty states. Derived here from the
304+
// same booleans the branches below use, so the two cannot drift.
305+
const showLoading = isLoading && !data;
306+
const showEmpty = graphResults.length === 0;
307+
const isPlotRendered =
308+
!showLoading && !isError && !resultFormattingError && !showEmpty;
309+
302310
// Exemplar overlay: data plus the hover/pin card state machine. See
303311
// useExemplarCard — the chart coordinates with it below because its drill-down
304312
// tooltip and the exemplar card are mutually exclusive.
@@ -324,6 +332,7 @@ function DBTimeChartComponent({
324332
queriedConfig,
325333
source,
326334
displayType,
335+
isPlotRendered,
327336
// Rendered series count, so a multi-line chart cannot be given markers that
328337
// belong to an unknown line. Taken from the main query's own result — the
329338
// exemplar response can't answer it, since Prometheus returns only series
@@ -468,7 +477,7 @@ function DBTimeChartComponent({
468477

469478
return (
470479
<ChartContainer title={title} toolbarItems={toolbarItemsMemo}>
471-
{isLoading && !data ? (
480+
{showLoading ? (
472481
<div className="d-flex h-100 w-100 align-items-center justify-content-center text-muted">
473482
Loading Chart Data...
474483
</div>
@@ -483,7 +492,7 @@ function DBTimeChartComponent({
483492
: new Error(String(resultFormattingError))
484493
}
485494
/>
486-
) : graphResults.length === 0 ? (
495+
) : showEmpty ? (
487496
<div className="d-flex h-100 w-100 align-items-center justify-content-center text-muted">
488497
No data found within time range.
489498
</div>

‎packages/app/src/components/DBTimeChart/__tests__/DBTimeChartExemplarPin.test.tsx‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { Exemplar } from '@hyperdx/common-utils/dist/types';
3-
import { act } from '@testing-library/react';
3+
import { MantineProvider } from '@mantine/core';
4+
import { act, render } from '@testing-library/react';
45

56
import { DBTimeChart } from '@/components/DBTimeChart';
67
import type { ExemplarHoverCard } from '@/components/Exemplars';
@@ -295,6 +296,67 @@ describe('DBTimeChart exemplar pin lifecycle', () => {
295296
});
296297
});
297298

299+
// Loading, error and empty states replace the whole subtree, so the marker layer
300+
// unmounts and nothing can report a position. A card left open through that
301+
// comes back anchored to the previous chart instance's coordinates.
302+
describe('the plot going away', () => {
303+
const setQueryState = (state: Record<string, unknown>) =>
304+
mockUseQueriedChartConfig.mockReturnValue({
305+
data: {
306+
data: [{ timestamp: '2024-01-01 00:00:00', value: 100 }],
307+
meta: [
308+
{ name: 'timestamp', type: 'DateTime' },
309+
{ name: 'value', type: 'Float64' },
310+
],
311+
rows: 1,
312+
isComplete: true,
313+
},
314+
isLoading: false,
315+
isError: false,
316+
isSuccess: true,
317+
isPlaceholderData: false,
318+
...state,
319+
});
320+
321+
// `wrapper` rather than renderWithMantine: it keeps the provider outside the
322+
// rerendered element, so the chart stays at the same tree position and keeps
323+
// its state. A rerender that remounts it would clear the card by itself and
324+
// let these pass with the cleanup removed.
325+
const wrapper = ({ children }: { children: React.ReactNode }) => (
326+
<MantineProvider>{children}</MantineProvider>
327+
);
328+
329+
it.each([
330+
['an error', { isError: true, error: new Error('boom') }],
331+
['no data in range', { data: { data: [], meta: [], rows: 0 } }],
332+
])('does not restore a pinned card after %s', (_label, state) => {
333+
const { rerender } = render(<DBTimeChart config={config} />, { wrapper });
334+
pin();
335+
expect(cardProps().pinned).toBe(true);
336+
337+
// The plot goes away. The card unmounts with it, so nothing renders and
338+
// nothing can report a marker position.
339+
setQueryState(state);
340+
act(() => {
341+
// A fresh object each time: DBTimeChart is memo'd, so reusing the same
342+
// config reference would skip the re-render entirely.
343+
rerender(<DBTimeChart config={{ ...config }} />);
344+
});
345+
const rendersWhileGone = mockCard.mock.calls.length;
346+
347+
// And comes back. Without the cleanup the card returns still pinned, at
348+
// the coordinates it had before the chart was torn down.
349+
setQueryState({});
350+
act(() => {
351+
rerender(<DBTimeChart config={{ ...config }} />);
352+
});
353+
354+
expect(mockCard.mock.calls.length).toBeGreaterThan(rendersWhileGone);
355+
expect(cardProps().pinned).toBe(false);
356+
expect(cardProps().hovered).toBeNull();
357+
});
358+
});
359+
298360
describe('outside-click dismissal (ported from #2748)', () => {
299361
const openTooltip = () =>
300362
act(() => {

‎packages/app/src/components/DBTimeChart/useExemplarCard.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function useExemplarCard({
4343
queriedConfig,
4444
source,
4545
displayType,
46+
isPlotRendered,
4647
plottedSeriesCount,
4748
}: {
4849
queriedConfig: ChartConfigWithDateRange;
@@ -53,6 +54,13 @@ export function useExemplarCard({
5354
* card would be left over markers that no longer exist.
5455
*/
5556
displayType: DisplayType | undefined;
57+
/**
58+
* Whether the chart is currently drawing its plot. False for the loading,
59+
* error and empty states, which replace the whole subtree — so the marker layer
60+
* unmounts, no marker can report a position, and a card left open would come
61+
* back anchored to coordinates from the previous chart instance.
62+
*/
63+
isPlotRendered: boolean;
5664
/** Series the chart actually draws; see useExemplars for why it matters. */
5765
plottedSeriesCount?: number;
5866
}) {
@@ -163,7 +171,16 @@ export function useExemplarCard({
163171
// zoom inside the 30s bucket moved the markers and left the cards behind) and
164172
// in the wrong place.
165173

166-
// Clear both cards before the chart subtree remounts on a display-type switch.
174+
// Clear both cards whenever the marker layer goes away: a display-type switch
175+
// remounts the recharts subtree (Area and Bar are different element types), and
176+
// the loading, error and empty states replace it outright. In every case the
177+
// markers a card was anchored to no longer exist.
178+
useEffect(() => {
179+
if (isPlotRendered) return;
180+
setPinnedExemplar(null);
181+
setHoveredExemplar(null);
182+
}, [isPlotRendered]);
183+
167184
useEffect(() => {
168185
setPinnedExemplar(null);
169186
setHoveredExemplar(null);

0 commit comments

Comments
 (0)