|
| 1 | +import {useCallback} from 'react'; |
| 2 | +import {useDispatch} from 'react-redux'; |
| 3 | +import cn from 'bem-cn-lite'; |
| 4 | + |
| 5 | +import {Loader, Button} from '@gravity-ui/uikit'; |
| 6 | + |
| 7 | +import type {EPathType} from '../../../../types/api/schema'; |
| 8 | +import {sendQuery, setQueryOptions} from '../../../../store/reducers/preview'; |
| 9 | +import {setShowPreview} from '../../../../store/reducers/schema/schema'; |
| 10 | +import {prepareQueryError} from '../../../../utils/query'; |
| 11 | +import {useAutofetcher, useTypedSelector} from '../../../../utils/hooks'; |
| 12 | + |
| 13 | +import {Icon} from '../../../../components/Icon'; |
| 14 | +import Fullscreen from '../../../../components/Fullscreen/Fullscreen'; |
| 15 | +import {QueryResultTable} from '../../../../components/QueryResultTable'; |
| 16 | +import EnableFullscreenButton from '../../../../components/EnableFullscreenButton/EnableFullscreenButton'; |
| 17 | + |
| 18 | +import {isTableType} from '../../utils/schema'; |
| 19 | + |
| 20 | +import i18n from '../i18n'; |
| 21 | + |
| 22 | +import './Preview.scss'; |
| 23 | + |
| 24 | +const b = cn('kv-preview'); |
| 25 | + |
| 26 | +interface PreviewProps { |
| 27 | + database: string; |
| 28 | + type: EPathType | undefined; |
| 29 | +} |
| 30 | + |
| 31 | +export const Preview = ({database, type}: PreviewProps) => { |
| 32 | + const dispatch = useDispatch(); |
| 33 | + |
| 34 | + const {data = {}, loading, error, wasLoaded} = useTypedSelector((state) => state.preview); |
| 35 | + const {autorefresh, currentSchemaPath} = useTypedSelector((state) => state.schema); |
| 36 | + const isFullscreen = useTypedSelector((state) => state.fullscreen); |
| 37 | + |
| 38 | + const sendQueryForPreview = useCallback( |
| 39 | + (isBackground) => { |
| 40 | + if (!isTableType(type)) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (!isBackground) { |
| 45 | + dispatch( |
| 46 | + setQueryOptions({ |
| 47 | + wasLoaded: false, |
| 48 | + data: undefined, |
| 49 | + }), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + const query = `--!syntax_v1\nselect * from \`${currentSchemaPath}\` limit 32`; |
| 54 | + |
| 55 | + dispatch( |
| 56 | + sendQuery({ |
| 57 | + query, |
| 58 | + database, |
| 59 | + action: 'execute-scan', |
| 60 | + }), |
| 61 | + ); |
| 62 | + }, |
| 63 | + [dispatch, database, currentSchemaPath, type], |
| 64 | + ); |
| 65 | + |
| 66 | + useAutofetcher(sendQueryForPreview, [sendQueryForPreview], autorefresh); |
| 67 | + |
| 68 | + const handleClosePreview = () => { |
| 69 | + dispatch(setShowPreview(false)); |
| 70 | + }; |
| 71 | + |
| 72 | + const renderHeader = () => { |
| 73 | + return ( |
| 74 | + <div className={b('header')}> |
| 75 | + <div className={b('title')}> |
| 76 | + {i18n('preview.title')}{' '} |
| 77 | + <div className={b('table-name')}>{currentSchemaPath}</div> |
| 78 | + </div> |
| 79 | + <div className={b('controls-left')}> |
| 80 | + <EnableFullscreenButton disabled={Boolean(error)} /> |
| 81 | + <Button |
| 82 | + view="flat-secondary" |
| 83 | + onClick={handleClosePreview} |
| 84 | + title={i18n('preview.close')} |
| 85 | + > |
| 86 | + <Icon name="close" viewBox={'0 0 16 16'} width={16} height={16} /> |
| 87 | + </Button> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | + }; |
| 92 | + |
| 93 | + if (loading && !wasLoaded) { |
| 94 | + return ( |
| 95 | + <div className={b('loader-container')}> |
| 96 | + <Loader size="m" /> |
| 97 | + </div> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + let message; |
| 102 | + |
| 103 | + if (!isTableType(type)) { |
| 104 | + message = <div className={b('message-container')}>{i18n('preview.not-available')}</div>; |
| 105 | + } else if (error) { |
| 106 | + message = <div className={b('message-container')}>{prepareQueryError(error)}</div>; |
| 107 | + } |
| 108 | + |
| 109 | + const content = message ?? ( |
| 110 | + <div className={b('result')}> |
| 111 | + <QueryResultTable data={data.result} columns={data.columns} /> |
| 112 | + </div> |
| 113 | + ); |
| 114 | + |
| 115 | + return ( |
| 116 | + <div className={b()}> |
| 117 | + {renderHeader()} |
| 118 | + {isFullscreen ? <Fullscreen>{content}</Fullscreen> : content} |
| 119 | + </div> |
| 120 | + ); |
| 121 | +}; |
0 commit comments