|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import type {TextProps} from '@gravity-ui/uikit'; |
| 4 | +import {Text} from '@gravity-ui/uikit'; |
| 5 | + |
| 6 | +import {cn} from '../../utils/cn'; |
| 7 | +import type {ProgressStatus} from '../../utils/progress'; |
| 8 | + |
| 9 | +import './DoughnutMetrics.scss'; |
| 10 | + |
| 11 | +const b = cn('ydb-doughnut-metrics'); |
| 12 | + |
| 13 | +interface LegendProps { |
| 14 | + children?: React.ReactNode; |
| 15 | + variant?: TextProps['variant']; |
| 16 | +} |
| 17 | + |
| 18 | +function Legend({children, variant = 'subheader-3'}: LegendProps) { |
| 19 | + return ( |
| 20 | + <Text variant={variant} color="secondary" className={b('legend')}> |
| 21 | + {children} |
| 22 | + </Text> |
| 23 | + ); |
| 24 | +} |
| 25 | +function Value({children, variant = 'subheader-2'}: LegendProps) { |
| 26 | + return ( |
| 27 | + <Text variant={variant} color="secondary" className={b('value')}> |
| 28 | + {children} |
| 29 | + </Text> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +interface DoughnutProps { |
| 34 | + status: ProgressStatus; |
| 35 | + fillWidth: number; |
| 36 | + children?: React.ReactNode; |
| 37 | + className?: string; |
| 38 | +} |
| 39 | + |
| 40 | +export function DoughnutMetrics({status, fillWidth, children, className}: DoughnutProps) { |
| 41 | + let gradientFill = 'var(--g-color-line-generic-solid)'; |
| 42 | + let filledDegrees = fillWidth * 3.6 - 90; |
| 43 | + |
| 44 | + if (fillWidth > 50) { |
| 45 | + gradientFill = 'var(--doughnut-color)'; |
| 46 | + filledDegrees = fillWidth * 3.6 + 90; |
| 47 | + } |
| 48 | + const gradientDegrees = filledDegrees; |
| 49 | + return ( |
| 50 | + <div className={b(null, className)}> |
| 51 | + <div |
| 52 | + style={{ |
| 53 | + backgroundImage: `linear-gradient(${gradientDegrees}deg, transparent 50%, ${gradientFill} 50%), linear-gradient(-90deg, var(--g-color-line-generic-solid) 50%, transparent 50%)`, |
| 54 | + }} |
| 55 | + className={b('doughnut', {status})} |
| 56 | + > |
| 57 | + <div className={b('text-wrapper')}>{children}</div> |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +DoughnutMetrics.Legend = Legend; |
| 64 | +DoughnutMetrics.Value = Value; |
0 commit comments