|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useMemo } from 'react' |
| 4 | +import { Tooltip } from '@/components/emcn' |
| 5 | + |
| 6 | +interface ContextUsageIndicatorProps { |
| 7 | + /** Usage percentage (0-100) */ |
| 8 | + percentage: number |
| 9 | + /** Size of the indicator in pixels */ |
| 10 | + size?: number |
| 11 | + /** Stroke width in pixels */ |
| 12 | + strokeWidth?: number |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Circular context usage indicator showing percentage of context window used. |
| 17 | + * Displays a progress ring that changes color based on usage level. |
| 18 | + * |
| 19 | + * @param props - Component props |
| 20 | + * @returns Rendered context usage indicator |
| 21 | + */ |
| 22 | +export function ContextUsageIndicator({ |
| 23 | + percentage, |
| 24 | + size = 20, |
| 25 | + strokeWidth = 2, |
| 26 | +}: ContextUsageIndicatorProps) { |
| 27 | + const radius = (size - strokeWidth) / 2 |
| 28 | + const circumference = radius * 2 * Math.PI |
| 29 | + const offset = circumference - (percentage / 100) * circumference |
| 30 | + |
| 31 | + const color = useMemo(() => { |
| 32 | + if (percentage >= 90) return '#dc2626' |
| 33 | + if (percentage >= 75) return '#d97706' |
| 34 | + return '#6b7280' |
| 35 | + }, [percentage]) |
| 36 | + |
| 37 | + const displayPercentage = useMemo(() => { |
| 38 | + return Math.round(percentage) |
| 39 | + }, [percentage]) |
| 40 | + |
| 41 | + return ( |
| 42 | + <Tooltip.Root delayDuration={100}> |
| 43 | + <Tooltip.Trigger asChild> |
| 44 | + <div |
| 45 | + className='flex cursor-pointer items-center justify-center transition-opacity hover:opacity-80' |
| 46 | + style={{ width: size, height: size }} |
| 47 | + > |
| 48 | + <svg width={size} height={size} className='rotate-[-90deg]'> |
| 49 | + <circle |
| 50 | + cx={size / 2} |
| 51 | + cy={size / 2} |
| 52 | + r={radius} |
| 53 | + stroke='currentColor' |
| 54 | + strokeWidth={strokeWidth} |
| 55 | + fill='none' |
| 56 | + className='text-muted-foreground/20' |
| 57 | + /> |
| 58 | + <circle |
| 59 | + cx={size / 2} |
| 60 | + cy={size / 2} |
| 61 | + r={radius} |
| 62 | + stroke={color} |
| 63 | + strokeWidth={strokeWidth} |
| 64 | + fill='none' |
| 65 | + strokeDasharray={circumference} |
| 66 | + strokeDashoffset={offset} |
| 67 | + className='transition-all duration-300 ease-in-out' |
| 68 | + strokeLinecap='round' |
| 69 | + /> |
| 70 | + </svg> |
| 71 | + </div> |
| 72 | + </Tooltip.Trigger> |
| 73 | + <Tooltip.Content side='top'> |
| 74 | + {displayPercentage}% context used |
| 75 | + </Tooltip.Content> |
| 76 | + </Tooltip.Root> |
| 77 | + ) |
| 78 | +} |
0 commit comments