Skip to content

Commit bd427d7

Browse files
refactor(Storage): make EntitiesCount a component
1 parent 8f42776 commit bd427d7

File tree

6 files changed

+64
-19
lines changed

6 files changed

+64
-19
lines changed

Diff for: src/components/EntitiesCount/EntitiesCount.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {Label} from '@gravity-ui/uikit';
2+
import i18n from './i18n';
3+
4+
interface EntitiesCountProps {
5+
current: number | string;
6+
total?: number | string;
7+
label?: string;
8+
loading?: boolean;
9+
className?: string;
10+
}
11+
12+
export const EntitiesCount = ({total, current, label, loading, className}: EntitiesCountProps) => {
13+
let content = '';
14+
15+
if (label) {
16+
content += `${label}: `;
17+
}
18+
19+
if (loading) {
20+
content += '...';
21+
} else {
22+
content += `${current}`;
23+
24+
if (total && Number(total) !== Number(current)) {
25+
content += ` ${i18n('of')} ${total}`;
26+
}
27+
}
28+
29+
return (
30+
<Label theme="info" size="m" className={className}>
31+
{content}
32+
</Label>
33+
);
34+
};

Diff for: src/components/EntitiesCount/i18n/en.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"of": "of"
3+
}

Diff for: src/components/EntitiesCount/i18n/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {i18n, Lang} from '../../../utils/i18n';
2+
3+
import en from './en.json';
4+
import ru from './ru.json';
5+
6+
const COMPONENT = 'ydb-entities-count';
7+
8+
i18n.registerKeyset(Lang.En, COMPONENT, en);
9+
i18n.registerKeyset(Lang.Ru, COMPONENT, ru);
10+
11+
export default i18n.keyset(COMPONENT);

Diff for: src/components/EntitiesCount/i18n/ru.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"of": "из"
3+
}

Diff for: src/components/EntitiesCount/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './EntitiesCount';

Diff for: src/containers/Storage/Storage.js

+12-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33
import {connect} from 'react-redux';
44
import cn from 'bem-cn-lite';
55
import DataTable from '@yandex-cloud/react-data-table';
6-
import {RadioButton, Label} from '@gravity-ui/uikit';
6+
import {RadioButton} from '@gravity-ui/uikit';
77

88
import {Search} from '../../components/Search';
99
import {UsageFilter} from './UsageFilter';
@@ -12,6 +12,7 @@ import {NodesUptimeFilterValues} from '../../utils/nodes';
1212
import {TableSkeleton} from '../../components/TableSkeleton/TableSkeleton';
1313
import {UptimeFilter} from '../../components/UptimeFIlter';
1414
import {AccessDenied} from '../../components/Errors/403';
15+
import {EntitiesCount} from '../../components/EntitiesCount';
1516

1617
import {
1718
getStorageInfo,
@@ -230,22 +231,17 @@ class Storage extends React.Component {
230231
const {storageType, groupsCount, nodesCount, flatListStorageEntities, loading, wasLoaded} =
231232
this.props;
232233

233-
let label = `${storageType === StorageTypes.groups ? 'Groups' : 'Nodes'}: `;
234+
const entityName = storageType === StorageTypes.groups ? 'Groups' : 'Nodes';
234235
const count = storageType === StorageTypes.groups ? groupsCount : nodesCount;
235236

236-
if (loading && !wasLoaded) {
237-
label += '...';
238-
return label;
239-
}
240-
241-
// count.total can be missing in old versions
242-
if (flatListStorageEntities.length === Number(count.total) || !count.total) {
243-
label += flatListStorageEntities.length;
244-
} else {
245-
label += `${flatListStorageEntities.length} of ${count.total}`;
246-
}
247-
248-
return label;
237+
return (
238+
<EntitiesCount
239+
label={entityName}
240+
loading={loading && !wasLoaded}
241+
total={count.total}
242+
current={flatListStorageEntities.length}
243+
/>
244+
);
249245
}
250246

251247
renderControls() {
@@ -307,10 +303,7 @@ class Storage extends React.Component {
307303
disabled={usageFilterOptions.length === 0}
308304
/>
309305
)}
310-
311-
<Label theme="info" size="m">
312-
{this.renderEntitiesCount()}
313-
</Label>
306+
{this.renderEntitiesCount()}
314307
</div>
315308
);
316309
}

0 commit comments

Comments
 (0)