|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {Flex, Progress, Text} from '@gravity-ui/uikit'; |
| 4 | + |
| 5 | +import type {FormatProgressViewerValues} from '../../../../../components/ProgressViewer/ProgressViewer'; |
| 6 | +import {cn} from '../../../../../utils/cn'; |
| 7 | +import {isNumeric, safeParseNumber} from '../../../../../utils/utils'; |
| 8 | + |
| 9 | +import {DEFAULT_PROGRESS_WIDTH, MAX_PERCENTAGE, MIN_PERCENTAGE, PROGRESS_SIZES} from './constants'; |
| 10 | +import i18n from './i18n'; |
| 11 | + |
| 12 | +import './ProgressWrapper.scss'; |
| 13 | + |
| 14 | +interface ProgressWrapperProps { |
| 15 | + value?: number | string; |
| 16 | + capacity?: number | string; |
| 17 | + formatValues?: FormatProgressViewerValues; |
| 18 | + size?: 'storage'; |
| 19 | + className?: string; |
| 20 | + width?: number; |
| 21 | +} |
| 22 | + |
| 23 | +const defaultFormatValues: FormatProgressViewerValues = (value, total) => { |
| 24 | + return [value, total]; |
| 25 | +}; |
| 26 | + |
| 27 | +const b = cn('progress-wrapper'); |
| 28 | + |
| 29 | +export function ProgressWrapper({ |
| 30 | + value, |
| 31 | + capacity, |
| 32 | + formatValues = defaultFormatValues, |
| 33 | + size = 'storage', |
| 34 | + className, |
| 35 | + width = DEFAULT_PROGRESS_WIDTH, |
| 36 | +}: ProgressWrapperProps) { |
| 37 | + // Input validation and error handling |
| 38 | + if (!isNumeric(value)) { |
| 39 | + return <div className={className}>{i18n('alert_no-data')}</div>; |
| 40 | + } |
| 41 | + |
| 42 | + const numericValue = safeParseNumber(value); |
| 43 | + const numericCapacity = safeParseNumber(capacity); |
| 44 | + |
| 45 | + // Handle edge cases: negative values, zero capacity |
| 46 | + if (numericValue < 0) { |
| 47 | + return <div className={className}>{i18n('alert_no-data')}</div>; |
| 48 | + } |
| 49 | + |
| 50 | + if (numericCapacity <= 0 && capacity !== undefined) { |
| 51 | + return <div className={className}>{i18n('alert_no-data')}</div>; |
| 52 | + } |
| 53 | + |
| 54 | + // Calculate percentage for uikit Progress with proper bounds checking |
| 55 | + const rawPercentage = Math.floor((numericValue / numericCapacity) * MAX_PERCENTAGE); |
| 56 | + const fillWidth = Math.max(MIN_PERCENTAGE, rawPercentage); |
| 57 | + const clampedFillWidth = Math.min(fillWidth, MAX_PERCENTAGE); |
| 58 | + |
| 59 | + // Get formatted display text - match original ProgressViewer exactly |
| 60 | + const [valueText, capacityText] = React.useMemo(() => { |
| 61 | + if (formatValues) { |
| 62 | + return formatValues(Number(value), Number(capacity)); |
| 63 | + } |
| 64 | + return [value, capacity]; |
| 65 | + }, [formatValues, value, capacity]); |
| 66 | + |
| 67 | + // For storage variant, we always use the Figma green color regardless of colorizeProgress |
| 68 | + // This matches the original ProgressViewer behavior for storage size |
| 69 | + |
| 70 | + // Memoize display text to avoid unnecessary recalculations |
| 71 | + const displayText = React.useMemo(() => { |
| 72 | + if (!isNumeric(numericCapacity) || numericCapacity <= 0) { |
| 73 | + return String(valueText); |
| 74 | + } |
| 75 | + return i18n('value_of_capacity', {value: valueText, capacity: capacityText}); |
| 76 | + }, [valueText, capacityText, numericCapacity]); |
| 77 | + |
| 78 | + // Validate width prop |
| 79 | + const validatedWidth = Math.max(0, width); |
| 80 | + |
| 81 | + // For storage variant, use Flex layout similar to current ProgressViewer |
| 82 | + if (size === 'storage') { |
| 83 | + return ( |
| 84 | + <Flex alignItems="center" gap="2" className={className}> |
| 85 | + <div className={b('storage-progress')} style={{width: `${validatedWidth}px`}}> |
| 86 | + <Progress |
| 87 | + value={clampedFillWidth} |
| 88 | + theme="success" |
| 89 | + size={PROGRESS_SIZES.STORAGE} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + <Text variant="body-1" color="secondary"> |
| 93 | + {displayText} |
| 94 | + </Text> |
| 95 | + </Flex> |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + // Default layout (though we're only using storage variant for now) |
| 100 | + return ( |
| 101 | + <div className={className}> |
| 102 | + <Progress value={clampedFillWidth} text={displayText} size={PROGRESS_SIZES.DEFAULT} /> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
0 commit comments