-
Notifications
You must be signed in to change notification settings - Fork 420
refactor(dashboards): extract shared Sparkline primitive from number tile #2520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alex-fedotyev
wants to merge
2
commits into
main
Choose a base branch
from
alex/HDX-1360-shared-sparkline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { | ||
| Area, | ||
| AreaChart, | ||
| Bar, | ||
| BarChart, | ||
| Line, | ||
| LineChart, | ||
| ResponsiveContainer, | ||
| } from 'recharts'; | ||
|
|
||
| type SparklineType = 'line' | 'area' | 'bar'; | ||
|
|
||
| export type SparklinePoint = { x: number; y: number }; | ||
|
|
||
| // `y` is the plotted value; `x` (bucket timestamp, seconds) is retained for | ||
| // ordering and future axis use. With no `<XAxis>`, recharts spaces points by | ||
| // array order, which callers keep sorted by bucket. | ||
| const VALUE_KEY = 'y'; | ||
|
|
||
| // The line / area variants are drawn behind or beside a value, so they are | ||
| // intentionally low-contrast: a translucent stroke with a fainter area fill. | ||
| const STROKE_OPACITY = 0.5; | ||
| const STROKE_WIDTH = 2; | ||
| const AREA_FILL_OPACITY = 0.15; | ||
|
|
||
| const CHART_MARGIN = { top: 4, right: 0, bottom: 0, left: 0 }; | ||
|
|
||
| /** | ||
| * Chrome-less recharts trend, drawn as a line, area, or bar. No axes, grid, | ||
| * legend, or tooltip; dots and animation are off. Fills its parent via | ||
| * `ResponsiveContainer`, so the parent owns the dimensions (pass `height` to | ||
| * override the default 100%). Renders nothing for fewer than two points, since | ||
| * a single point has no trend to draw. | ||
| */ | ||
| export function Sparkline({ | ||
| points, | ||
| type, | ||
| color, | ||
| height = '100%', | ||
| }: { | ||
| points: SparklinePoint[]; | ||
| type: SparklineType; | ||
| color: string; | ||
| height?: number | string; | ||
| }) { | ||
| if (points.length < 2) return null; | ||
|
|
||
| return ( | ||
| <ResponsiveContainer width="100%" height={height}> | ||
| {type === 'bar' ? ( | ||
| <BarChart data={points} margin={CHART_MARGIN}> | ||
| <Bar | ||
| dataKey={VALUE_KEY} | ||
| fill={color} | ||
| maxBarSize={24} | ||
| isAnimationActive={false} | ||
| /> | ||
| </BarChart> | ||
| ) : type === 'area' ? ( | ||
| <AreaChart data={points} margin={CHART_MARGIN}> | ||
| <Area | ||
| type="monotone" | ||
| dataKey={VALUE_KEY} | ||
| stroke={color} | ||
| strokeOpacity={STROKE_OPACITY} | ||
| strokeWidth={STROKE_WIDTH} | ||
| fill={color} | ||
| fillOpacity={AREA_FILL_OPACITY} | ||
| isAnimationActive={false} | ||
| dot={false} | ||
| /> | ||
| </AreaChart> | ||
| ) : ( | ||
| <LineChart data={points} margin={CHART_MARGIN}> | ||
| <Line | ||
| type="monotone" | ||
| dataKey={VALUE_KEY} | ||
| stroke={color} | ||
| strokeOpacity={STROKE_OPACITY} | ||
| strokeWidth={STROKE_WIDTH} | ||
| isAnimationActive={false} | ||
| dot={false} | ||
| /> | ||
| </LineChart> | ||
| )} | ||
| </ResponsiveContainer> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { Sparkline, type SparklinePoint } from '@/components/Sparkline'; | ||
|
|
||
| // recharts `ResponsiveContainer` sizes itself from its parent via a | ||
| // ResizeObserver, which is a no-op in jsdom (see setupTests), so it never | ||
| // reports a size and the chart never paints. Swap it for a fixed-size | ||
| // pass-through so the SVG renders and the variant can be asserted. | ||
| jest.mock('recharts', () => { | ||
| const actual = jest.requireActual<typeof import('recharts')>('recharts'); | ||
| const { cloneElement } = jest.requireActual<typeof import('react')>('react'); | ||
| return { | ||
| ...actual, | ||
| ResponsiveContainer: ({ | ||
| children, | ||
| }: { | ||
| children: React.ReactElement<{ width?: number; height?: number }>; | ||
| }) => cloneElement(children, { width: 300, height: 80 }), | ||
| }; | ||
| }); | ||
|
|
||
| const COLOR = '#abcdef'; | ||
|
|
||
| const POINTS: SparklinePoint[] = [ | ||
| { x: 1, y: 3 }, | ||
| { x: 2, y: 7 }, | ||
| { x: 3, y: 5 }, | ||
| ]; | ||
|
|
||
| describe('Sparkline', () => { | ||
| it('renders a line trend in the given color', () => { | ||
| const { container } = renderWithMantine( | ||
| <Sparkline points={POINTS} type="line" color={COLOR} />, | ||
| ); | ||
| expect(container.querySelector('.recharts-line')).toBeInTheDocument(); | ||
| expect(container.innerHTML).toContain(COLOR); | ||
| }); | ||
|
|
||
| it('renders an area trend in the given color', () => { | ||
| const { container } = renderWithMantine( | ||
| <Sparkline points={POINTS} type="area" color={COLOR} />, | ||
| ); | ||
| expect(container.querySelector('.recharts-area')).toBeInTheDocument(); | ||
| expect(container.innerHTML).toContain(COLOR); | ||
| }); | ||
|
|
||
| it('renders a bar trend in the given color', () => { | ||
| const { container } = renderWithMantine( | ||
| <Sparkline points={POINTS} type="bar" color={COLOR} />, | ||
| ); | ||
| expect(container.querySelector('.recharts-bar')).toBeInTheDocument(); | ||
| expect(container.innerHTML).toContain(COLOR); | ||
| }); | ||
|
|
||
| it('renders nothing for fewer than two points', () => { | ||
| const { container } = renderWithMantine( | ||
| <Sparkline points={[{ x: 1, y: 3 }]} type="line" color={COLOR} />, | ||
| ); | ||
| const surface = container.querySelector('.recharts-surface'); | ||
| expect(surface).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders with an explicit numeric height (table-cell usage)', () => { | ||
| const { container } = renderWithMantine( | ||
| <Sparkline points={POINTS} type="line" color={COLOR} height={24} />, | ||
| ); | ||
| expect(container.querySelector('.recharts-line')).toBeInTheDocument(); | ||
| }); | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SparklineTypeis not exported. Consumers who need to track or type thetypeprop in their own state will have to re-declare'line' | 'area' | 'bar'themselves or useReact.ComponentProps<typeof Sparkline>['type']. GivenSparklinePointis already exported and the follow-upDBTableChartconsumer is the intended caller, exportingSparklineTypealongside it would keep the public surface consistent.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept local on purpose for now. This package trims unused exports (knip reports unused exported types), and the only current consumer passes a value that is already typed by its own schema, so exporting it now would be a dead export. The table-cell consumer in the follow-up imports it, and the export lands with that change so it is never unused.