|
| 1 | +import {useLocation} from 'react-router'; |
| 2 | +import block from 'bem-cn-lite'; |
| 3 | + |
| 4 | +import type {TEvDescribeSchemeResult} from '../../../../types/api/schema'; |
| 5 | +import {useTypedSelector} from '../../../../utils/hooks'; |
| 6 | +import {createHref, parseQuery} from '../../../../routes'; |
| 7 | +import {formatCommonItem} from '../../../../components/InfoViewer/formatters'; |
| 8 | +import {InfoViewer, InfoViewerItem} from '../../../../components/InfoViewer'; |
| 9 | +import {ExternalLinkWithIcon} from '../../../../components/ExternalLinkWithIcon/ExternalLinkWithIcon'; |
| 10 | +import EntityStatus from '../../../../components/EntityStatus/EntityStatus'; |
| 11 | +import {ResponseError} from '../../../../components/Errors/ResponseError'; |
| 12 | + |
| 13 | +import {getEntityName} from '../../utils'; |
| 14 | + |
| 15 | +import i18n from '../i18n'; |
| 16 | +import './ExternalTable.scss'; |
| 17 | + |
| 18 | +const b = block('ydb-external-table-info'); |
| 19 | + |
| 20 | +const prepareExternalTableSummary = ( |
| 21 | + data: TEvDescribeSchemeResult, |
| 22 | + pathToDataSource: string, |
| 23 | +): InfoViewerItem[] => { |
| 24 | + const {CreateStep} = data.PathDescription?.Self || {}; |
| 25 | + const {SourceType, DataSourcePath} = data.PathDescription?.ExternalTableDescription || {}; |
| 26 | + |
| 27 | + const dataSourceName = DataSourcePath?.split('/').pop(); |
| 28 | + |
| 29 | + return [ |
| 30 | + {label: i18n('external-objects.source-type'), value: SourceType}, |
| 31 | + formatCommonItem('CreateStep', CreateStep), |
| 32 | + { |
| 33 | + label: i18n('external-objects.data-source'), |
| 34 | + value: DataSourcePath && ( |
| 35 | + <span title={DataSourcePath}> |
| 36 | + <ExternalLinkWithIcon title={dataSourceName || ''} url={pathToDataSource} /> |
| 37 | + </span> |
| 38 | + ), |
| 39 | + }, |
| 40 | + ]; |
| 41 | +}; |
| 42 | + |
| 43 | +const prepareExternalTableInfo = ( |
| 44 | + data: TEvDescribeSchemeResult, |
| 45 | + pathToDataSource: string, |
| 46 | +): InfoViewerItem[] => { |
| 47 | + const location = data.PathDescription?.ExternalTableDescription?.Location; |
| 48 | + |
| 49 | + return [ |
| 50 | + ...prepareExternalTableSummary(data, pathToDataSource), |
| 51 | + { |
| 52 | + label: i18n('external-objects.location'), |
| 53 | + value: ( |
| 54 | + <EntityStatus |
| 55 | + name={location} |
| 56 | + showStatus={false} |
| 57 | + hasClipboardButton |
| 58 | + clipboardButtonAlwaysVisible |
| 59 | + className={b('location')} |
| 60 | + /> |
| 61 | + ), |
| 62 | + }, |
| 63 | + ]; |
| 64 | +}; |
| 65 | + |
| 66 | +interface ExternalTableProps { |
| 67 | + data?: TEvDescribeSchemeResult; |
| 68 | + prepareData: (data: TEvDescribeSchemeResult, pathToDataSource: string) => InfoViewerItem[]; |
| 69 | +} |
| 70 | + |
| 71 | +const ExternalTable = ({data, prepareData}: ExternalTableProps) => { |
| 72 | + const location = useLocation(); |
| 73 | + const query = parseQuery(location); |
| 74 | + |
| 75 | + // embedded version could be located in some folder (e.g. host/some_folder/app_router_path) |
| 76 | + // window.location has the full pathname, while location from router ignores path to project |
| 77 | + const pathToDataSource = createHref(window.location.pathname, undefined, { |
| 78 | + ...query, |
| 79 | + schema: data?.PathDescription?.ExternalTableDescription?.DataSourcePath, |
| 80 | + }); |
| 81 | + |
| 82 | + const entityName = getEntityName(data?.PathDescription); |
| 83 | + |
| 84 | + const {error: schemaError} = useTypedSelector((state) => state.schema); |
| 85 | + |
| 86 | + if (schemaError) { |
| 87 | + return <ResponseError error={schemaError} />; |
| 88 | + } |
| 89 | + |
| 90 | + if (!data) { |
| 91 | + return <div className="error">No {entityName} data</div>; |
| 92 | + } |
| 93 | + |
| 94 | + return <InfoViewer title={entityName} info={prepareData(data, pathToDataSource)} />; |
| 95 | +}; |
| 96 | + |
| 97 | +export const ExternalTableInfo = ({data}: {data?: TEvDescribeSchemeResult}) => { |
| 98 | + return <ExternalTable data={data} prepareData={prepareExternalTableInfo} />; |
| 99 | +}; |
| 100 | + |
| 101 | +export const ExternalTableSummary = ({data}: {data?: TEvDescribeSchemeResult}) => { |
| 102 | + return <ExternalTable data={data} prepareData={prepareExternalTableSummary} />; |
| 103 | +}; |
0 commit comments