Skip to content

Commit 84f1d90

Browse files
committed
refactor: migrtae ProgressViewer to ts
1 parent 060635e commit 84f1d90

File tree

3 files changed

+193
-2
lines changed

3 files changed

+193
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.progress-viewer {
2+
position: relative;
3+
4+
display: flex;
5+
overflow: hidden;
6+
justify-content: center;
7+
align-items: center;
8+
9+
min-width: 120px;
10+
height: 23px;
11+
padding: 0 4px;
12+
13+
font-size: var(--yc-text-body-2-font-size);
14+
white-space: nowrap;
15+
16+
color: var(--yc-color-text-light-primary);
17+
border-radius: 2px;
18+
background: var(--yc-color-base-generic);
19+
20+
&__line {
21+
position: absolute;
22+
top: 0;
23+
left: 0;
24+
25+
height: 100%;
26+
27+
&_bg_scarlet {
28+
background: var(--yc-color-infographics-danger-heavy);
29+
}
30+
&_bg_apple {
31+
background: var(--yc-color-infographics-positive-heavy);
32+
}
33+
&_bg_saffron {
34+
background: var(--yc-color-infographics-warning-heavy);
35+
}
36+
}
37+
38+
&__text {
39+
position: relative;
40+
z-index: 1;
41+
&_text_contrast0 {
42+
color: var(--yc-color-text-light-primary);
43+
}
44+
&_text_contrast70 {
45+
color: var(--yc-color-text-complementary);
46+
}
47+
}
48+
49+
&_size_xs {
50+
height: 20px;
51+
52+
font-size: var(--yc-text-body-2-font-size);
53+
line-height: var(--yc-text-body-2-line-height);
54+
}
55+
56+
&_size_s {
57+
height: 28px;
58+
59+
font-size: var(--yc-text-body-1-font-size);
60+
line-height: 28px;
61+
}
62+
&_size_m {
63+
height: 32px;
64+
65+
font-size: var(--yc-text-body-2-font-size);
66+
line-height: 32px;
67+
}
68+
69+
&_size_ns {
70+
height: 24px;
71+
72+
font-size: 13px;
73+
line-height: var(--yc-text-subheader-3-line-height);
74+
}
75+
76+
&_size_n {
77+
height: 36px;
78+
79+
font-size: var(--yc-text-body-1-font-size);
80+
line-height: 36px;
81+
}
82+
83+
&_size_l {
84+
height: 38px;
85+
86+
font-size: var(--yc-text-subheader-3-font-size);
87+
line-height: 38px;
88+
}
89+
90+
&_size_head {
91+
font-size: var(--yc-text-body-1-font-size);
92+
line-height: 36px;
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import cn from 'bem-cn-lite';
2+
3+
import {ValueOf} from '../../types/common';
4+
5+
import './ProgressViewer.scss';
6+
7+
const b = cn('progress-viewer');
8+
9+
export const PROGRESS_VIEWER_SIZE_IDS = {
10+
xs: 'xs',
11+
s: 's',
12+
ns: 'ns',
13+
m: 'm',
14+
n: 'n',
15+
l: 'l',
16+
head: 'head',
17+
} as const;
18+
19+
type ProgressViewerSize = ValueOf<typeof PROGRESS_VIEWER_SIZE_IDS>;
20+
21+
/*
22+
23+
Описание props:
24+
1) value - величина прогресса
25+
2) capacity - предельно возможный прогресс
26+
3) formatValues - функция форматирования value и capacity
27+
4) percents - отображать ли заполненость в виде процентов
28+
5) className - кастомный класс
29+
6) colorizeProgress - менять ли цвет полосы прогресса в зависимости от его величины
30+
7) inverseColorize - инвертировать ли цвета при разукрашивании прогресса
31+
*/
32+
33+
interface ProgressViewerProps {
34+
value?: number | string;
35+
capacity?: number | string;
36+
formatValues?: (value?: number, total?: number) => (string | undefined)[];
37+
percents?: boolean;
38+
className?: string;
39+
size?: ProgressViewerSize;
40+
colorizeProgress?: boolean;
41+
inverseColorize?: boolean;
42+
}
43+
44+
export function ProgressViewer({
45+
value,
46+
capacity = 100,
47+
formatValues,
48+
percents,
49+
className,
50+
size = PROGRESS_VIEWER_SIZE_IDS.xs,
51+
colorizeProgress,
52+
inverseColorize,
53+
}: ProgressViewerProps) {
54+
let fillWidth = Math.round((parseFloat(String(value)) / parseFloat(String(capacity))) * 100);
55+
fillWidth = fillWidth > 100 ? 100 : fillWidth;
56+
57+
let valueText = String(Math.round(Number(value))),
58+
capacityText = capacity,
59+
divider = '/';
60+
let formattedValue: string | undefined;
61+
let formattedCapacity: string | undefined;
62+
if (formatValues) {
63+
[formattedValue, formattedCapacity] = formatValues(Number(value), Number(capacity));
64+
} else if (percents) {
65+
valueText = fillWidth + '%';
66+
capacityText = '';
67+
divider = '';
68+
}
69+
70+
let bg = inverseColorize ? 'scarlet' : 'apple';
71+
if (colorizeProgress) {
72+
if (fillWidth > 60 && fillWidth <= 80) {
73+
bg = 'saffron';
74+
} else if (fillWidth > 80) {
75+
bg = inverseColorize ? 'apple' : 'scarlet';
76+
}
77+
}
78+
79+
const lineStyle = {
80+
width: fillWidth + '%',
81+
};
82+
83+
const text = fillWidth > 60 ? 'contrast0' : 'contrast70';
84+
85+
if (!isNaN(fillWidth)) {
86+
return (
87+
<div className={b({size}, className)}>
88+
<div className={b('line', {bg})} style={lineStyle}></div>
89+
<span className={b('text', {text})}>{`${formattedValue || valueText} ${divider} ${
90+
formattedCapacity || capacityText
91+
}`}</span>
92+
</div>
93+
);
94+
}
95+
96+
return <div className={`${b({size})} ${className} error`}>no data</div>;
97+
}

src/store/reducers/tenants/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {TTenant} from '../../../types/api/tenant';
22
import {formatBytes} from '../../../utils/bytesParsers';
3-
import {formatCPUToLocal} from '../../../utils/dataFormatters/dataFormatters';
3+
import {formatCPUWithText} from '../../../utils/dataFormatters/dataFormatters';
44
import {isNumeric} from '../../../utils/utils';
55
import {METRIC_STATUS} from './contants';
66

@@ -158,7 +158,7 @@ export const formatTenantMetrics = ({
158158
storage?: number;
159159
memory?: number;
160160
}) => ({
161-
cpu: formatCPUToLocal(cpu),
161+
cpu: formatCPUWithText(cpu),
162162
storage: formatBytes({value: storage, significantDigits: 2}) || undefined,
163163
memory: formatBytes({value: memory, significantDigits: 2}) || undefined,
164164
});

0 commit comments

Comments
 (0)