import React from 'react'; import type {HelpMarkProps, TextProps} from '@gravity-ui/uikit'; import {Flex, HelpMark, Text} from '@gravity-ui/uikit'; import {cn} from '../../utils/cn'; import type {ProgressStatus} from '../../utils/progress'; import {SvgCircle} from './SvgCircle'; import { ROTATION_OFFSET, SIZE_CONFIG, calculateCircumference, calculateOverlapDasharray, calculateStrokeDasharray, } from './utils'; import './DoughnutMetrics.scss'; const b = cn('ydb-doughnut-metrics'); type Size = keyof typeof SIZE_CONFIG; const SizeContext = React.createContext('medium'); // Legend component interface LegendProps { children?: React.ReactNode; variant?: TextProps['variant']; color?: TextProps['color']; note?: React.ReactNode; noteIconSize?: HelpMarkProps['iconSize']; } function Legend({ children, variant = 'subheader-3', color = 'primary', note, noteIconSize = 'm', }: LegendProps) { return ( {children} {note && ( {note} )} ); } // Value component function Value({children, variant}: LegendProps) { const size = React.useContext(SizeContext); const finalVariant = variant || SIZE_CONFIG[size].textVariant; return ( {children} ); } // Main component interface DoughnutProps { status: ProgressStatus; fillWidth: number; children?: React.ReactNode; className?: string; size?: Size; } export function DoughnutMetrics({ status, fillWidth, children, className, size = 'medium', }: DoughnutProps) { const config = SIZE_CONFIG[size]; const radius = (config.width - config.strokeWidth) / 2; const circumference = calculateCircumference(radius); const strokeDashoffset = circumference * ROTATION_OFFSET; const centerX = config.width / 2; const centerY = config.width / 2; const strokeDasharray = calculateStrokeDasharray(fillWidth, circumference); const overlapDasharray = calculateOverlapDasharray(fillWidth, circumference); const needsOverlapCircle = fillWidth > 100; return (
{/* Background circle */} {/* Progress circle */} {/* Overlap circle for values > 100% */} {needsOverlapCircle && ( )}
{children}
); } DoughnutMetrics.Legend = Legend; DoughnutMetrics.Value = Value;