|
| 1 | +import { Box } from '@mui/material' |
| 2 | +import { useTheme, alpha } from '@mui/material/styles' |
| 3 | +import { useMemo, useId } from 'react' |
| 4 | + |
| 5 | +interface ActivitySparklineProps { |
| 6 | + data: number[] |
| 7 | + width?: number |
| 8 | + height?: number |
| 9 | + color?: string |
| 10 | +} |
| 11 | + |
| 12 | +interface Point { |
| 13 | + x: number |
| 14 | + y: number |
| 15 | +} |
| 16 | + |
| 17 | +export function ActivitySparkline({ |
| 18 | + data, |
| 19 | + width = 80, |
| 20 | + height = 30, |
| 21 | + color, |
| 22 | +}: ActivitySparklineProps) { |
| 23 | + const theme = useTheme() |
| 24 | + const strokeColor = color || theme.palette.primary.main |
| 25 | + const gradientId = useId() |
| 26 | + |
| 27 | + const { path, points, hasActivity } = useMemo(() => { |
| 28 | + if (!data || data.length === 0) { |
| 29 | + return { path: '', points: [] as Point[], hasActivity: false } |
| 30 | + } |
| 31 | + |
| 32 | + const max = Math.max(...data, 1) // Ensure at least 1 to avoid division by zero |
| 33 | + const hasAct = data.some(v => v > 0) |
| 34 | + |
| 35 | + const padding = 2 |
| 36 | + const chartWidth = width - padding * 2 |
| 37 | + const chartHeight = height - padding * 2 |
| 38 | + |
| 39 | + const pts = data.map((value, index) => { |
| 40 | + const x = padding + (index / (data.length - 1 || 1)) * chartWidth |
| 41 | + const y = padding + chartHeight - (value / max) * chartHeight |
| 42 | + return { x, y } |
| 43 | + }) |
| 44 | + |
| 45 | + // Create SVG path |
| 46 | + const pathData = pts |
| 47 | + .map((point, index) => { |
| 48 | + if (index === 0) return `M ${point.x} ${point.y}` |
| 49 | + return `L ${point.x} ${point.y}` |
| 50 | + }) |
| 51 | + .join(' ') |
| 52 | + |
| 53 | + return { path: pathData, points: pts, hasActivity: hasAct } |
| 54 | + }, [data, width, height]) |
| 55 | + |
| 56 | + // Create area path (for fill) - reuse points from above |
| 57 | + const areaPath = useMemo(() => { |
| 58 | + if (points.length === 0 || !path) return '' |
| 59 | + |
| 60 | + const padding = 2 |
| 61 | + const chartHeight = height - padding * 2 |
| 62 | + const bottomY = padding + chartHeight |
| 63 | + const firstX = points[0]?.x || padding |
| 64 | + const lastX = points[points.length - 1]?.x || width - padding |
| 65 | + |
| 66 | + return `${path} L ${lastX} ${bottomY} L ${firstX} ${bottomY} Z` |
| 67 | + }, [points, path, width, height]) |
| 68 | + |
| 69 | + // No activity - show placeholder (after all hooks) |
| 70 | + if (!hasActivity) { |
| 71 | + return ( |
| 72 | + <Box |
| 73 | + sx={{ |
| 74 | + width, |
| 75 | + height, |
| 76 | + display: 'flex', |
| 77 | + alignItems: 'center', |
| 78 | + justifyContent: 'center', |
| 79 | + bgcolor: alpha(theme.palette.grey[500], 0.08), |
| 80 | + borderRadius: 1, |
| 81 | + }} |
| 82 | + > |
| 83 | + <Box |
| 84 | + sx={{ |
| 85 | + width: '60%', |
| 86 | + height: 2, |
| 87 | + bgcolor: alpha(theme.palette.grey[500], 0.3), |
| 88 | + borderRadius: 1, |
| 89 | + }} |
| 90 | + /> |
| 91 | + </Box> |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + return ( |
| 96 | + <Box |
| 97 | + sx={{ |
| 98 | + width, |
| 99 | + height, |
| 100 | + display: 'flex', |
| 101 | + alignItems: 'center', |
| 102 | + justifyContent: 'center', |
| 103 | + }} |
| 104 | + > |
| 105 | + <svg width={width} height={height}> |
| 106 | + {/* Gradient definition */} |
| 107 | + <defs> |
| 108 | + <linearGradient id={gradientId} x1="0" y1="0" x2="0" y2="1"> |
| 109 | + <stop offset="0%" stopColor={strokeColor} stopOpacity={0.3} /> |
| 110 | + <stop offset="100%" stopColor={strokeColor} stopOpacity={0.05} /> |
| 111 | + </linearGradient> |
| 112 | + </defs> |
| 113 | + |
| 114 | + {/* Area fill */} |
| 115 | + <path d={areaPath} fill={`url(#${gradientId})`} /> |
| 116 | + |
| 117 | + {/* Line */} |
| 118 | + <path |
| 119 | + d={path} |
| 120 | + fill="none" |
| 121 | + stroke={strokeColor} |
| 122 | + strokeWidth={2} |
| 123 | + strokeLinecap="round" |
| 124 | + strokeLinejoin="round" |
| 125 | + /> |
| 126 | + </svg> |
| 127 | + </Box> |
| 128 | + ) |
| 129 | +} |
0 commit comments