-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDiskStateProgressBar.tsx
More file actions
121 lines (101 loc) · 3.07 KB
/
Copy pathDiskStateProgressBar.tsx
File metadata and controls
121 lines (101 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React from 'react';
import {Flex, Icon} from '@gravity-ui/uikit';
import {SETTING_KEYS} from '../../store/reducers/settings/constants';
import {cn} from '../../utils/cn';
import {DONOR_COLOR} from '../../utils/disks/constants';
import {getSeverityColor, getVDiskStatusIcon} from '../../utils/disks/helpers';
import {useSetting} from '../../utils/hooks';
import './DiskStateProgressBar.scss';
const b = cn('storage-disk-progress-bar');
interface DiskStateProgressBarProps {
diskAllocatedPercent?: number;
severity?: number;
compact?: boolean;
faded?: boolean;
inactive?: boolean;
empty?: boolean;
striped?: boolean;
content?: React.ReactNode;
className?: string;
isDonor?: boolean;
withIcon?: boolean;
}
export function DiskStateProgressBar({
diskAllocatedPercent = -1,
severity,
compact,
faded,
inactive,
empty,
content,
striped,
className,
isDonor,
withIcon,
}: DiskStateProgressBarProps) {
const [inverted] = useSetting<boolean | undefined>(SETTING_KEYS.INVERTED_DISKS);
const mods: Record<string, boolean | undefined> = {
inverted,
compact,
faded,
empty,
inactive,
striped,
};
if (isDonor) {
mods[DONOR_COLOR.toLocaleLowerCase()] = true;
} else {
const color = severity !== undefined && getSeverityColor(severity);
if (color) {
mods[color.toLocaleLowerCase()] = true;
}
}
const renderAllocatedPercent = () => {
if (compact) {
return <div className={b('fill-bar', mods)} style={{width: '100%'}} />;
}
// diskAllocatedPercent could be more than 100
let fillWidth = Math.min(diskAllocatedPercent, 100);
if (inverted) {
fillWidth = Math.max(100 - diskAllocatedPercent, 0);
}
if (diskAllocatedPercent >= 0) {
return <div className={b('fill-bar', mods)} style={{width: `${fillWidth}%`}} />;
}
return null;
};
const renderContent = () => {
if (content) {
return content;
}
if (!compact && diskAllocatedPercent >= 0) {
return <div className={b('title')}>{`${Math.floor(diskAllocatedPercent)}%`}</div>;
}
return null;
};
let iconElement: React.ReactNode = null;
if (withIcon) {
const icon = getVDiskStatusIcon(severity, isDonor);
if (icon) {
iconElement = <Icon className={b('icon')} data={icon} size={12} />;
}
}
const hasIcon = Boolean(iconElement);
const justifyContent = hasIcon ? 'space-between' : 'flex-end';
return (
<Flex
alignItems="center"
justifyContent={justifyContent}
className={b(mods, className)}
role="meter"
aria-label="Disk allocated space"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={diskAllocatedPercent}
>
{iconElement}
{renderAllocatedPercent()}
{renderContent()}
</Flex>
);
}