|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {Progress} from '@gravity-ui/uikit'; |
| 4 | + |
| 5 | +import {defaultFormatProgressValues} from '../../utils/progress'; |
| 6 | +import {safeParseNumber} from '../../utils/utils'; |
| 7 | +import {getMemorySegmentColor} from '../MemoryViewer/utils'; |
| 8 | + |
| 9 | +import {ProgressContainer} from './ProgressContainer'; |
| 10 | +import i18n from './i18n'; |
| 11 | +import { |
| 12 | + MAX_PERCENTAGE, |
| 13 | + PROGRESS_SIZE, |
| 14 | + formatDisplayValues, |
| 15 | + formatProgressText, |
| 16 | +} from './progressUtils'; |
| 17 | +import type {ProgressWrapperStackProps} from './types'; |
| 18 | + |
| 19 | +export function StackProgress({ |
| 20 | + stack, |
| 21 | + totalCapacity, |
| 22 | + formatValues = defaultFormatProgressValues, |
| 23 | + className, |
| 24 | + width, |
| 25 | + size = PROGRESS_SIZE, |
| 26 | + withCapacityUsage = false, |
| 27 | +}: ProgressWrapperStackProps) { |
| 28 | + const displaySegments = React.useMemo(() => { |
| 29 | + return stack.filter((segment) => !segment.isInfo && segment.value > 0); |
| 30 | + }, [stack]); |
| 31 | + |
| 32 | + if (displaySegments.length === 0) { |
| 33 | + return <div className={className}>{i18n('alert_no-data')}</div>; |
| 34 | + } |
| 35 | + |
| 36 | + const totalValue = React.useMemo(() => { |
| 37 | + return displaySegments.reduce((sum, segment) => sum + segment.value, 0); |
| 38 | + }, [displaySegments]); |
| 39 | + |
| 40 | + const numericTotalCapacity = React.useMemo(() => { |
| 41 | + return safeParseNumber(totalCapacity); |
| 42 | + }, [totalCapacity]); |
| 43 | + |
| 44 | + const maxValue = numericTotalCapacity || totalValue; |
| 45 | + |
| 46 | + const stackElements = React.useMemo(() => { |
| 47 | + return displaySegments.map((segment) => ({ |
| 48 | + value: maxValue > 0 ? (segment.value / maxValue) * MAX_PERCENTAGE : 0, |
| 49 | + color: getMemorySegmentColor(segment.key), |
| 50 | + title: segment.label, |
| 51 | + })); |
| 52 | + }, [displaySegments, maxValue]); |
| 53 | + |
| 54 | + const [totalValueText, totalCapacityText] = React.useMemo(() => { |
| 55 | + return formatDisplayValues(totalValue, numericTotalCapacity || totalValue, formatValues); |
| 56 | + }, [formatValues, totalValue, numericTotalCapacity]); |
| 57 | + |
| 58 | + const displayText = React.useMemo(() => { |
| 59 | + return formatProgressText(totalValueText, totalCapacityText, numericTotalCapacity || 0); |
| 60 | + }, [totalValueText, totalCapacityText, numericTotalCapacity]); |
| 61 | + |
| 62 | + return ( |
| 63 | + <ProgressContainer |
| 64 | + displayText={displayText} |
| 65 | + withCapacityUsage={withCapacityUsage} |
| 66 | + className={className} |
| 67 | + width={width} |
| 68 | + > |
| 69 | + <Progress value={MAX_PERCENTAGE} stack={stackElements} size={size} /> |
| 70 | + </ProgressContainer> |
| 71 | + ); |
| 72 | +} |
0 commit comments