|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {useHistory, useLocation} from 'react-router-dom'; |
| 4 | + |
| 5 | +import {parseQuery} from '../../../../../routes'; |
| 6 | +import {changeUserInput, setIsDirty} from '../../../../../store/reducers/query/query'; |
| 7 | +import { |
| 8 | + TENANT_PAGE, |
| 9 | + TENANT_PAGES_IDS, |
| 10 | + TENANT_QUERY_TABS_ID, |
| 11 | +} from '../../../../../store/reducers/tenant/constants'; |
| 12 | +import type {KeyValueRow} from '../../../../../types/api/query'; |
| 13 | +import {useTypedDispatch} from '../../../../../utils/hooks'; |
| 14 | +import {TenantTabsGroups, getTenantPath} from '../../../TenantPages'; |
| 15 | +import {createQueryInfoItems} from '../utils'; |
| 16 | + |
| 17 | +import {NotFoundContainer} from './NotFoundContainer'; |
| 18 | +import {QueryDetails} from './QueryDetails'; |
| 19 | + |
| 20 | +interface QueryDetailsDrawerContentProps { |
| 21 | + // Three cases: |
| 22 | + // 1. row is KeyValueRow and we can show it. |
| 23 | + // 2. row is null and we can show not found container. |
| 24 | + // 3. row is undefined and we can show nothing. |
| 25 | + row?: KeyValueRow | null; |
| 26 | + onClose: () => void; |
| 27 | +} |
| 28 | + |
| 29 | +export const QueryDetailsDrawerContent = ({row, onClose}: QueryDetailsDrawerContentProps) => { |
| 30 | + const dispatch = useTypedDispatch(); |
| 31 | + const location = useLocation(); |
| 32 | + const history = useHistory(); |
| 33 | + |
| 34 | + const handleOpenInEditor = React.useCallback(() => { |
| 35 | + if (row) { |
| 36 | + const input = row.QueryText as string; |
| 37 | + dispatch(changeUserInput({input})); |
| 38 | + dispatch(setIsDirty(false)); |
| 39 | + |
| 40 | + const queryParams = parseQuery(location); |
| 41 | + |
| 42 | + const queryPath = getTenantPath({ |
| 43 | + ...queryParams, |
| 44 | + [TENANT_PAGE]: TENANT_PAGES_IDS.query, |
| 45 | + [TenantTabsGroups.queryTab]: TENANT_QUERY_TABS_ID.newQuery, |
| 46 | + }); |
| 47 | + |
| 48 | + history.push(queryPath); |
| 49 | + } |
| 50 | + }, [dispatch, history, location, row]); |
| 51 | + |
| 52 | + if (row) { |
| 53 | + return ( |
| 54 | + <QueryDetails |
| 55 | + queryText={row.QueryText as string} |
| 56 | + infoItems={createQueryInfoItems(row)} |
| 57 | + onOpenInEditor={handleOpenInEditor} |
| 58 | + /> |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + return <NotFoundContainer onClose={onClose} />; |
| 63 | +}; |
0 commit comments