|
1 | 1 | import type {NavigationTreeNodeType} from 'ydb-ui-components'; |
2 | 2 | import {EPathSubType, EPathType} from '../../../types/api/schema'; |
3 | 3 |
|
4 | | -const mapTablePathSubTypeToNavigationTreeType = (subType?: EPathSubType) => { |
5 | | - switch (subType) { |
6 | | - case EPathSubType.EPathSubTypeSyncIndexImplTable: |
7 | | - case EPathSubType.EPathSubTypeAsyncIndexImplTable: |
8 | | - return 'index_table'; |
9 | | - default: |
10 | | - return 'table'; |
11 | | - } |
| 4 | +// this file contains verbose mappings that are typed in a way that ensures |
| 5 | +// correctness when a new node type or a new path type is added |
| 6 | +// TS will error if a new entity is added but not mapped here |
| 7 | + |
| 8 | +const pathSubTypeToNodeType: Record<EPathSubType, NavigationTreeNodeType | undefined> = { |
| 9 | + [EPathSubType.EPathSubTypeSyncIndexImplTable]: 'index_table', |
| 10 | + [EPathSubType.EPathSubTypeAsyncIndexImplTable]: 'index_table', |
| 11 | + |
| 12 | + [EPathSubType.EPathSubTypeStreamImpl]: undefined, |
| 13 | + [EPathSubType.EPathSubTypeEmpty]: undefined, |
| 14 | +}; |
| 15 | + |
| 16 | +const pathTypeToNodeType: Record<EPathType, NavigationTreeNodeType | undefined> = { |
| 17 | + [EPathType.EPathTypeInvalid]: undefined, |
| 18 | + |
| 19 | + [EPathType.EPathTypeSubDomain]: 'database', |
| 20 | + [EPathType.EPathTypeExtSubDomain]: 'database', |
| 21 | + |
| 22 | + [EPathType.EPathTypeDir]: 'directory', |
| 23 | + [EPathType.EPathTypeColumnStore]: 'directory', |
| 24 | + |
| 25 | + [EPathType.EPathTypeTable]: 'table', |
| 26 | + |
| 27 | + [EPathType.EPathTypeTableIndex]: 'index', |
| 28 | + |
| 29 | + [EPathType.EPathTypeColumnTable]: 'column_table', |
| 30 | + |
| 31 | + [EPathType.EPathTypeCdcStream]: 'topic', |
12 | 32 | }; |
13 | 33 |
|
14 | 34 | export const mapPathTypeToNavigationTreeType = ( |
15 | 35 | type: EPathType = EPathType.EPathTypeDir, |
16 | 36 | subType?: EPathSubType, |
17 | 37 | defaultType: NavigationTreeNodeType = 'directory' |
18 | | -): NavigationTreeNodeType => { |
19 | | - switch (type) { |
20 | | - case EPathType.EPathTypeSubDomain: |
21 | | - case EPathType.EPathTypeExtSubDomain: |
22 | | - return 'database'; |
23 | | - case EPathType.EPathTypeTable: |
24 | | - return mapTablePathSubTypeToNavigationTreeType(subType); |
25 | | - case EPathType.EPathTypeColumnTable: |
26 | | - return 'column_table'; |
27 | | - case EPathType.EPathTypeDir: |
28 | | - case EPathType.EPathTypeColumnStore: |
29 | | - return 'directory'; |
30 | | - case EPathType.EPathTypeTableIndex: |
31 | | - return 'index'; |
32 | | - case EPathType.EPathTypeCdcStream: |
33 | | - return 'topic'; |
34 | | - default: |
35 | | - return defaultType; |
36 | | - } |
| 38 | +): NavigationTreeNodeType => |
| 39 | + (subType && pathSubTypeToNodeType[subType]) || pathTypeToNodeType[type] || defaultType; |
| 40 | + |
| 41 | +// ==================== |
| 42 | + |
| 43 | +const pathTypeToIsTable: Record<EPathType, boolean> = { |
| 44 | + [EPathType.EPathTypeTable]: true, |
| 45 | + [EPathType.EPathTypeColumnTable]: true, |
| 46 | + |
| 47 | + [EPathType.EPathTypeInvalid]: false, |
| 48 | + [EPathType.EPathTypeDir]: false, |
| 49 | + [EPathType.EPathTypeSubDomain]: false, |
| 50 | + [EPathType.EPathTypeTableIndex]: false, |
| 51 | + [EPathType.EPathTypeExtSubDomain]: false, |
| 52 | + [EPathType.EPathTypeColumnStore]: false, |
| 53 | + [EPathType.EPathTypeCdcStream]: false, |
37 | 54 | }; |
38 | 55 |
|
39 | | -export const isTableType = (type?: EPathType) => |
40 | | - mapPathTypeToNavigationTreeType(type) === 'table'; |
| 56 | +export const isTableType = (pathType?: EPathType) => |
| 57 | + (pathType && pathTypeToIsTable[pathType]) ?? false; |
| 58 | + |
| 59 | +// ==================== |
| 60 | + |
| 61 | +const pathSubTypeToIsIndexImpl: Record<EPathSubType, boolean> = { |
| 62 | + [EPathSubType.EPathSubTypeSyncIndexImplTable]: true, |
| 63 | + [EPathSubType.EPathSubTypeAsyncIndexImplTable]: true, |
| 64 | + |
| 65 | + [EPathSubType.EPathSubTypeStreamImpl]: false, |
| 66 | + [EPathSubType.EPathSubTypeEmpty]: false, |
| 67 | +}; |
41 | 68 |
|
42 | 69 | export const isIndexTable = (subType?: EPathSubType) => |
43 | | - mapTablePathSubTypeToNavigationTreeType(subType) === 'index_table'; |
| 70 | + (subType && pathSubTypeToIsIndexImpl[subType]) ?? false; |
| 71 | + |
| 72 | +// ==================== |
| 73 | + |
| 74 | +const pathTypeToIsColumn: Record<EPathType, boolean> = { |
| 75 | + [EPathType.EPathTypeColumnStore]: true, |
| 76 | + [EPathType.EPathTypeColumnTable]: true, |
| 77 | + |
| 78 | + [EPathType.EPathTypeInvalid]: false, |
| 79 | + [EPathType.EPathTypeDir]: false, |
| 80 | + [EPathType.EPathTypeTable]: false, |
| 81 | + [EPathType.EPathTypeSubDomain]: false, |
| 82 | + [EPathType.EPathTypeTableIndex]: false, |
| 83 | + [EPathType.EPathTypeExtSubDomain]: false, |
| 84 | + [EPathType.EPathTypeCdcStream]: false, |
| 85 | +}; |
44 | 86 |
|
45 | 87 | export const isColumnEntityType = (type?: EPathType) => |
46 | | - type === EPathType.EPathTypeColumnStore || |
47 | | - type === EPathType.EPathTypeColumnTable; |
| 88 | + (type && pathTypeToIsColumn[type]) ?? false; |
0 commit comments