|
1 |
| -import React, {useRef, useState} from 'react'; |
2 |
| -import cn from 'bem-cn-lite'; |
3 |
| -import _ from 'lodash'; |
4 |
| -import {Button, Popup} from '@gravity-ui/uikit'; |
| 1 | +import {useDispatch} from 'react-redux'; |
| 2 | +import block from 'bem-cn-lite'; |
| 3 | + |
| 4 | +import DataTable, {Column} from '@gravity-ui/react-data-table'; |
5 | 5 |
|
6 | 6 | import TruncatedQuery from '../../../../components/TruncatedQuery/TruncatedQuery';
|
| 7 | +import {setQueryTab} from '../../../../store/reducers/tenant/tenant'; |
| 8 | +import {TENANT_QUERY_TABS_ID} from '../../../../store/reducers/tenant/constants'; |
7 | 9 | import {useTypedSelector} from '../../../../utils/hooks';
|
| 10 | +import {MAX_QUERY_HEIGHT, QUERY_TABLE_SETTINGS} from '../../utils/constants'; |
8 | 11 |
|
9 |
| -import './QueriesHistory.scss'; |
| 12 | +import i18n from '../i18n'; |
10 | 13 |
|
11 |
| -const b = cn('kv-queries-history'); |
| 14 | +import './QueriesHistory.scss'; |
12 | 15 |
|
13 |
| -const MAX_QUERY_HEIGHT = 3; |
| 16 | +const b = block('ydb-queries-history'); |
14 | 17 |
|
15 | 18 | interface QueriesHistoryProps {
|
16 | 19 | changeUserInput: (value: {input: string}) => void;
|
17 | 20 | }
|
18 | 21 |
|
19 |
| -function QueriesHistory(props: QueriesHistoryProps) { |
20 |
| - const [isHistoryVisible, setIsHistoryVisible] = useState(false); |
21 |
| - const history = useTypedSelector((state) => state.executeQuery.history.queries) ?? []; |
22 |
| - const anchor = useRef(null); |
| 22 | +function QueriesHistory({changeUserInput}: QueriesHistoryProps) { |
| 23 | + const dispatch = useDispatch(); |
23 | 24 |
|
24 |
| - const onShowHistoryClick = () => { |
25 |
| - setIsHistoryVisible(true); |
26 |
| - }; |
27 |
| - |
28 |
| - const onCloseHistory = () => { |
29 |
| - setIsHistoryVisible(false); |
30 |
| - }; |
| 25 | + const queriesHistory = useTypedSelector((state) => state.executeQuery.history.queries) ?? []; |
| 26 | + const reversedHistory = [...queriesHistory].reverse(); |
31 | 27 |
|
32 | 28 | const onQueryClick = (queryText: string) => {
|
33 |
| - const {changeUserInput} = props; |
34 |
| - return () => { |
35 |
| - changeUserInput({input: queryText}); |
36 |
| - onCloseHistory(); |
37 |
| - }; |
| 29 | + changeUserInput({input: queryText}); |
| 30 | + dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery)); |
38 | 31 | };
|
39 | 32 |
|
40 |
| - const renderSavedQueries = () => { |
41 |
| - const reversedHistory = ([] as string[]).concat(history).reverse(); |
42 |
| - return ( |
43 |
| - <Popup |
44 |
| - className={b('popup-wrapper')} |
45 |
| - anchorRef={anchor} |
46 |
| - open={isHistoryVisible} |
47 |
| - placement={['bottom-end']} |
48 |
| - onClose={onCloseHistory} |
49 |
| - > |
50 |
| - <div className={b()}> |
51 |
| - {reversedHistory.length === 0 ? ( |
52 |
| - <div className={b('empty')}>History is empty</div> |
53 |
| - ) : ( |
54 |
| - <React.Fragment> |
55 |
| - <div className={b('saved-queries-row', {header: true})}> |
56 |
| - <div className={b('query-body', {header: true})}> |
57 |
| - <span>QueryText</span> |
58 |
| - </div> |
59 |
| - </div> |
60 |
| - <div> |
61 |
| - {reversedHistory.map((query, index) => { |
62 |
| - return ( |
63 |
| - <div |
64 |
| - className={b('saved-queries-row')} |
65 |
| - onClick={onQueryClick(query)} |
66 |
| - key={index} |
67 |
| - > |
68 |
| - <div className={b('query-body')}> |
69 |
| - <TruncatedQuery |
70 |
| - value={query} |
71 |
| - maxQueryHeight={MAX_QUERY_HEIGHT} |
72 |
| - /> |
73 |
| - </div> |
74 |
| - </div> |
75 |
| - ); |
76 |
| - })} |
77 |
| - </div> |
78 |
| - </React.Fragment> |
79 |
| - )} |
| 33 | + const columns: Column<string>[] = [ |
| 34 | + { |
| 35 | + name: 'queryText', |
| 36 | + header: 'Query Text', |
| 37 | + render: ({row: query}) => ( |
| 38 | + <div className={b('query')}> |
| 39 | + <TruncatedQuery value={query} maxQueryHeight={MAX_QUERY_HEIGHT} /> |
80 | 40 | </div>
|
81 |
| - </Popup> |
82 |
| - ); |
83 |
| - }; |
| 41 | + ), |
| 42 | + sortable: false, |
| 43 | + }, |
| 44 | + ]; |
84 | 45 |
|
85 | 46 | return (
|
86 |
| - <React.Fragment> |
87 |
| - <Button ref={anchor} onClick={onShowHistoryClick}> |
88 |
| - Query history |
89 |
| - </Button> |
90 |
| - {isHistoryVisible && renderSavedQueries()} |
91 |
| - </React.Fragment> |
| 47 | + <div className={b()}> |
| 48 | + <DataTable |
| 49 | + theme="yandex-cloud" |
| 50 | + columns={columns} |
| 51 | + data={reversedHistory} |
| 52 | + settings={QUERY_TABLE_SETTINGS} |
| 53 | + emptyDataMessage={i18n('history.empty')} |
| 54 | + onRowClick={(row) => onQueryClick(row)} |
| 55 | + /> |
| 56 | + </div> |
92 | 57 | );
|
93 | 58 | }
|
94 | 59 |
|
|
0 commit comments