|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import dynamic from 'next/dynamic'; |
| 3 | +import cx from 'classnames'; |
| 4 | +import { Text, useMantineColorScheme } from '@mantine/core'; |
| 5 | + |
| 6 | +import ChartContainer from '@/components/charts/ChartContainer'; |
| 7 | +import ChartErrorState, { |
| 8 | + ChartErrorStateVariant, |
| 9 | +} from '@/components/charts/ChartErrorState'; |
| 10 | + |
| 11 | +import { ColorLegend } from './ColorLegend'; |
| 12 | +import type { HeatmapScaleType } from './heatmapGrid'; |
| 13 | +import { HeatmapPlot } from './HeatmapPlot'; |
| 14 | +import type { HeatmapChartConfig } from './heatmapQueries'; |
| 15 | +import { darkPalette, lightPalette } from './palette'; |
| 16 | +import type { SelectionBounds } from './selection'; |
| 17 | +import { useHeatmapData } from './useHeatmapData'; |
| 18 | + |
| 19 | +type DBHeatmapChartProps = { |
| 20 | + config: HeatmapChartConfig; |
| 21 | + enabled?: boolean; |
| 22 | + onFilter?: (xMin: number, xMax: number, yMin: number, yMax: number) => void; |
| 23 | + onClearFilter?: () => void; |
| 24 | + /** |
| 25 | + * The currently-applied drag-select bounds. When provided, the heatmap |
| 26 | + * draws the dashed selection rectangle and reapplies it after any uPlot |
| 27 | + * recreation (theme switch, prop change, resize) so the user always sees |
| 28 | + * which slice they filtered. Caller owns the URL/query-state plumbing; |
| 29 | + * this is purely a visual mirror of that state. |
| 30 | + */ |
| 31 | + selectionBounds?: SelectionBounds | null; |
| 32 | + title?: React.ReactNode; |
| 33 | + toolbarPrefix?: React.ReactNode[]; |
| 34 | + toolbarSuffix?: React.ReactNode[]; |
| 35 | + scaleType?: HeatmapScaleType; |
| 36 | + showLegend?: boolean; |
| 37 | + errorVariant?: ChartErrorStateVariant; |
| 38 | +}; |
| 39 | + |
| 40 | +function DBHeatmapChart({ |
| 41 | + config, |
| 42 | + enabled = true, |
| 43 | + onFilter, |
| 44 | + onClearFilter, |
| 45 | + selectionBounds, |
| 46 | + title, |
| 47 | + toolbarPrefix, |
| 48 | + toolbarSuffix, |
| 49 | + scaleType = 'log', |
| 50 | + showLegend = false, |
| 51 | + errorVariant, |
| 52 | +}: DBHeatmapChartProps) { |
| 53 | + const { colorScheme } = useMantineColorScheme(); |
| 54 | + const palette = colorScheme === 'light' ? lightPalette : darkPalette; |
| 55 | + |
| 56 | + const { view, isLoading, isRefreshing, error } = useHeatmapData({ |
| 57 | + config, |
| 58 | + scaleType, |
| 59 | + enabled, |
| 60 | + }); |
| 61 | + |
| 62 | + const toolbarItemsMemo = useMemo(() => { |
| 63 | + const allToolbarItems: React.ReactNode[] = []; |
| 64 | + |
| 65 | + if (showLegend) { |
| 66 | + allToolbarItems.push( |
| 67 | + <ColorLegend key="heatmap-legend" colors={palette} />, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + if (toolbarPrefix && toolbarPrefix.length > 0) { |
| 72 | + allToolbarItems.push(...toolbarPrefix); |
| 73 | + } |
| 74 | + |
| 75 | + if (toolbarSuffix && toolbarSuffix.length > 0) { |
| 76 | + allToolbarItems.push(...toolbarSuffix); |
| 77 | + } |
| 78 | + |
| 79 | + return allToolbarItems; |
| 80 | + }, [showLegend, palette, toolbarPrefix, toolbarSuffix]); |
| 81 | + |
| 82 | + return ( |
| 83 | + <ChartContainer |
| 84 | + title={title} |
| 85 | + toolbarItems={toolbarItemsMemo} |
| 86 | + disableReactiveContainer |
| 87 | + > |
| 88 | + {isLoading ? ( |
| 89 | + <Text size="sm" ta="center" p="xl"> |
| 90 | + Loading... |
| 91 | + </Text> |
| 92 | + ) : error ? ( |
| 93 | + <ChartErrorState error={error} variant={errorVariant} /> |
| 94 | + ) : view.grid.cells.length < 2 || view.generatedTsBuckets.length < 2 ? ( |
| 95 | + <Text |
| 96 | + size="sm" |
| 97 | + ta="center" |
| 98 | + p="xl" |
| 99 | + className={cx({ 'effect-pulse': isRefreshing })} |
| 100 | + > |
| 101 | + Not enough data points to render heatmap. Try expanding your search |
| 102 | + criteria. |
| 103 | + </Text> |
| 104 | + ) : ( |
| 105 | + <HeatmapPlot |
| 106 | + key={JSON.stringify(config)} |
| 107 | + className={cx({ 'effect-pulse': isRefreshing })} |
| 108 | + grid={view.grid} |
| 109 | + numberFormat={config.numberFormat} |
| 110 | + onFilter={ |
| 111 | + onFilter |
| 112 | + ? (xMin, xMax, yMin, yMax) => { |
| 113 | + // In log mode, the bottom bucket collects all values |
| 114 | + // clamped by greatest(value, effectiveMin). If the |
| 115 | + // selection touches that bucket, widen yMin to 0 so |
| 116 | + // the downstream SQL filter captures all those spans. |
| 117 | + // The 1.1x threshold adds 10% headroom to account for |
| 118 | + // floating-point rounding in the bucket boundary. |
| 119 | + const adjustedYMin = |
| 120 | + view.scaleType === 'log' && yMin <= view.effectiveMin * 1.1 |
| 121 | + ? 0 |
| 122 | + : yMin; |
| 123 | + onFilter(xMin, xMax, adjustedYMin, yMax); |
| 124 | + } |
| 125 | + : undefined |
| 126 | + } |
| 127 | + onClearFilter={onClearFilter} |
| 128 | + scaleType={view.scaleType} |
| 129 | + palette={palette} |
| 130 | + selectionBounds={selectionBounds} |
| 131 | + /> |
| 132 | + )} |
| 133 | + </ChartContainer> |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +export default dynamic(() => Promise.resolve(DBHeatmapChart), { |
| 138 | + ssr: false, |
| 139 | +}); |
0 commit comments