import React from 'react' import { Line, LineChart, ResponsiveContainer, YAxis } from 'recharts' import { useIsMobileView } from '../../../hooks/useIsMobileView/useIsMobileView' /** Recharts v3 dimension type - accepts number or percentage string like '100%' */ type DimensionValue = number | `${number}%` type SparkLineType = { /** ClassName to be applied to the ResponsiveContainer */ containerClassName?: string /** Object key where the data points are stored */ dataKey: string /** The graph data (example: [{dataKey: number, date: 'YYYY-MM-DD', threshold: dashed_line_value}] ) */ graphData: Array> /** Props to be passed to the Line component {accepts all Rechart `Line` Props} */ lineProps?: React.ComponentPropsWithoutRef /** Props to be passed to the LineChart component {accepts all Rechart `LineChart` Props} */ lineChartProps?: React.ComponentPropsWithoutRef /** Specifies the line color. Accepted values are 'green', 'red', 'blue', or 'purple' (default is 'blue'). The dark variant of the selected color will be applied. */ strokeColor?: 'red' | 'green' | 'blue' | 'purple' /** Color of the dashed threshold line; expect 'green', 'red', or 'blue' (default: blue) */ thresholdColor?: string /** Object key where the threshold data value is stored */ thresholdKey?: string /** Props to be passed to the YAxis component {accepts all Rechart `YAxis` Props} */ yAxisProps?: React.ComponentPropsWithoutRef /* Props to be passed for sparkline container height and width (number or percentage like '100%') */ customDimension?: { height?: DimensionValue width?: DimensionValue } /** Optional prop to add a test id for QA testing */ qaTestId?: string } const DEFAULT_BLUE = 'var(--dark-blue)' const SparkLine = ({ containerClassName, dataKey = 'data_point', graphData, strokeColor, thresholdColor, thresholdKey, lineProps, lineChartProps, yAxisProps, customDimension, qaTestId = 'sparkline', }: SparkLineType): React.JSX.Element => { const isMobileView = useIsMobileView() const graphLineColor = strokeColor ? `var(--dark-${strokeColor})` : DEFAULT_BLUE const thresholdLineColor = thresholdColor ? `var(--dark-${thresholdColor})` : DEFAULT_BLUE const DEFAULT_CONTAINER_WIDTH = isMobileView ? 80 : 150 const DEFAULT_CONTAINER_HEIGHT = 25 return (
{/* DATA LINE */} {/* THRESHOLD (MIDDLE DASHED LINE) */}
) } export default SparkLine