Skip to content

Commit 348f2d2

Browse files
refactor(Describe): rewrite in TS and hooks
1 parent 56525a9 commit 348f2d2

File tree

4 files changed

+83
-132
lines changed

4 files changed

+83
-132
lines changed

src/containers/Tenant/Diagnostics/Describe/Describe.js

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {useCallback} from 'react';
2+
import {useDispatch} from 'react-redux';
3+
import cn from 'bem-cn-lite';
4+
// @ts-ignore
5+
import JSONTree from 'react-json-inspector';
6+
import 'react-json-inspector/json-inspector.css';
7+
8+
import {Loader} from '@gravity-ui/uikit';
9+
10+
import {prepareQueryError} from '../../../../utils/query';
11+
import {useAutofetcher, useTypedSelector} from '../../../../utils/hooks';
12+
import {getDescribe} from '../../../../store/reducers/describe';
13+
14+
import './Describe.scss';
15+
16+
const b = cn('kv-describe');
17+
18+
const expandMap = new Map();
19+
20+
interface IDescribeProps {
21+
tenant: string;
22+
}
23+
24+
const Describe = ({tenant}: IDescribeProps) => {
25+
const dispatch = useDispatch();
26+
27+
const {currentDescribe, error, loading, wasLoaded} = useTypedSelector(
28+
(state) => state.describe,
29+
);
30+
31+
const {autorefresh, currentSchemaPath} = useTypedSelector((state) => state.schema);
32+
33+
const fetchData = useCallback(() => {
34+
const path = currentSchemaPath || tenant;
35+
36+
dispatch(getDescribe({path}));
37+
}, [currentSchemaPath, tenant, dispatch]);
38+
39+
useAutofetcher(fetchData, [fetchData], autorefresh);
40+
41+
if (loading && !wasLoaded) {
42+
return (
43+
<div className={b('loader-container')}>
44+
<Loader size="m" />
45+
</div>
46+
);
47+
}
48+
49+
if (error) {
50+
return <div className={b('message-container', 'error')}>{prepareQueryError(error)}</div>;
51+
}
52+
53+
if (!loading && !currentDescribe) {
54+
return <div className={b('message-container')}>Empty</div>;
55+
}
56+
57+
return (
58+
<div className={b()}>
59+
<div className={b('result')}>
60+
<JSONTree
61+
data={currentDescribe}
62+
className={b('tree')}
63+
onClick={({path}: {path: string}) => {
64+
const newValue = !(expandMap.get(path) || false);
65+
expandMap.set(path, newValue);
66+
}}
67+
searchOptions={{
68+
debounceTime: 300,
69+
}}
70+
isExpanded={(keypath: string) => {
71+
return expandMap.get(keypath) || false;
72+
}}
73+
/>
74+
</div>
75+
</div>
76+
);
77+
};
78+
79+
export default Describe;

src/store/reducers/describe.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const describe: Reducer<
5151
...state,
5252
error: action.error,
5353
loading: false,
54+
wasLoaded: true,
5455
};
5556
}
5657
default:

src/types/api/error.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface IResponseError {
2-
status: number;
3-
statusText: string;
2+
data?: unknown;
3+
status?: number;
4+
statusText?: string;
45
}

0 commit comments

Comments
 (0)