|
| 1 | +import type {FormatProgressViewerValues} from '../../utils/progress'; |
| 2 | +import {isNumeric, safeParseNumber} from '../../utils/utils'; |
| 3 | + |
| 4 | +// Constants that were previously in TenantStorage/constants |
| 5 | +export const DEFAULT_PROGRESS_WIDTH = 400; |
| 6 | +export const MAX_PERCENTAGE = 100; |
| 7 | +export const MIN_PERCENTAGE = 0; |
| 8 | +export const PROGRESS_SIZE = 's'; |
| 9 | + |
| 10 | +export const isValidValue = (val?: number | string): boolean => |
| 11 | + isNumeric(val) && safeParseNumber(val) >= 0; |
| 12 | + |
| 13 | +export function calculateProgressWidth(value: number, capacity: number): number { |
| 14 | + const rawPercentage = |
| 15 | + capacity > 0 ? Math.floor((value / capacity) * MAX_PERCENTAGE) : MAX_PERCENTAGE; |
| 16 | + const fillWidth = Math.max(MIN_PERCENTAGE, rawPercentage); |
| 17 | + return Math.min(fillWidth, MAX_PERCENTAGE); |
| 18 | +} |
| 19 | + |
| 20 | +export function getProgressStyle(width?: number | 'full') { |
| 21 | + const isFullWidth = width === 'full'; |
| 22 | + const validatedWidth = isFullWidth ? 0 : Math.max(0, width || DEFAULT_PROGRESS_WIDTH); |
| 23 | + |
| 24 | + return { |
| 25 | + width: isFullWidth ? '100%' : `${validatedWidth}px`, |
| 26 | + flex: isFullWidth ? '1' : 'none', |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +export function formatProgressText( |
| 31 | + valueText: string | number | undefined, |
| 32 | + capacityText: string | number | undefined, |
| 33 | + numericCapacity: number, |
| 34 | + i18n: any, |
| 35 | +): string { |
| 36 | + if (numericCapacity <= 0) { |
| 37 | + return String(valueText); |
| 38 | + } |
| 39 | + return i18n('context_capacity-usage', {value: valueText, capacity: capacityText}); |
| 40 | +} |
| 41 | + |
| 42 | +export function formatDisplayValues( |
| 43 | + value: number | string | undefined, |
| 44 | + capacity: number | string | undefined, |
| 45 | + formatValues?: FormatProgressViewerValues, |
| 46 | +): [string | number | undefined, string | number | undefined] { |
| 47 | + if (formatValues) { |
| 48 | + const result = formatValues(Number(value), Number(capacity)); |
| 49 | + return [result[0], result[1]] as [string | number | undefined, string | number | undefined]; |
| 50 | + } |
| 51 | + return [value, capacity]; |
| 52 | +} |
0 commit comments