|
| 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; |
0 commit comments