|
| 1 | +import nodesRightIcon from '@gravity-ui/icons/svgs/nodes-right.svg'; |
| 2 | +import databaseIcon from '@gravity-ui/icons/svgs/database.svg'; |
| 3 | + |
| 4 | +import type { |
| 5 | + BreadcrumbsOptions, |
| 6 | + ClusterBreadcrumbsOptions, |
| 7 | + NodeBreadcrumbsOptions, |
| 8 | + Page, |
| 9 | + TabletBreadcrumbsOptions, |
| 10 | + TabletsBreadcrumbsOptions, |
| 11 | + TenantBreadcrumbsOptions, |
| 12 | +} from '../../store/reducers/header/types'; |
| 13 | +import routes, {createHref} from '../../routes'; |
| 14 | + |
| 15 | +import {getClusterPath} from '../Cluster/utils'; |
| 16 | +import {getTenantPath} from '../Tenant/TenantPages'; |
| 17 | +import {getDefaultNodePath} from '../Node/NodePages'; |
| 18 | + |
| 19 | +const prepareTenantName = (tenantName: string) => { |
| 20 | + return tenantName.startsWith('/') ? tenantName.slice(1) : tenantName; |
| 21 | +}; |
| 22 | + |
| 23 | +export interface RawBreadcrumbItem { |
| 24 | + text: string; |
| 25 | + link?: string; |
| 26 | + icon?: SVGIconData; |
| 27 | +} |
| 28 | + |
| 29 | +const getClusterBreadcrumbs = ( |
| 30 | + options: ClusterBreadcrumbsOptions, |
| 31 | + query = {}, |
| 32 | +): RawBreadcrumbItem[] => { |
| 33 | + const {clusterName, clusterTab} = options; |
| 34 | + |
| 35 | + return [ |
| 36 | + { |
| 37 | + text: clusterName || 'Cluster', |
| 38 | + link: getClusterPath(clusterTab, query), |
| 39 | + icon: nodesRightIcon, |
| 40 | + }, |
| 41 | + ]; |
| 42 | +}; |
| 43 | + |
| 44 | +const getTenantBreadcrumbs = ( |
| 45 | + options: TenantBreadcrumbsOptions, |
| 46 | + query = {}, |
| 47 | +): RawBreadcrumbItem[] => { |
| 48 | + const {tenantName} = options; |
| 49 | + |
| 50 | + const text = tenantName ? prepareTenantName(tenantName) : 'Tenant'; |
| 51 | + const link = tenantName ? getTenantPath({...query, name: tenantName}) : undefined; |
| 52 | + |
| 53 | + return [...getClusterBreadcrumbs(options, query), {text, link, icon: databaseIcon}]; |
| 54 | +}; |
| 55 | + |
| 56 | +const getNodeBreadcrumbs = (options: NodeBreadcrumbsOptions, query = {}): RawBreadcrumbItem[] => { |
| 57 | + const {tenantName, nodeId} = options; |
| 58 | + |
| 59 | + let breadcrumbs: RawBreadcrumbItem[]; |
| 60 | + |
| 61 | + // Compute nodes have tenantName, storage nodes doesn't |
| 62 | + const isStorageNode = !tenantName; |
| 63 | + |
| 64 | + if (isStorageNode) { |
| 65 | + breadcrumbs = getClusterBreadcrumbs(options, query); |
| 66 | + } else { |
| 67 | + breadcrumbs = getTenantBreadcrumbs(options, query); |
| 68 | + } |
| 69 | + |
| 70 | + const text = nodeId ? `Node ${nodeId}` : 'Node'; |
| 71 | + const link = nodeId ? getDefaultNodePath(nodeId, query) : undefined; |
| 72 | + |
| 73 | + breadcrumbs.push({text, link}); |
| 74 | + |
| 75 | + return breadcrumbs; |
| 76 | +}; |
| 77 | + |
| 78 | +const getTabletsBreadcrubms = ( |
| 79 | + options: TabletsBreadcrumbsOptions, |
| 80 | + query = {}, |
| 81 | +): RawBreadcrumbItem[] => { |
| 82 | + const {tenantName, nodeIds, state, type} = options; |
| 83 | + |
| 84 | + let breadcrumbs: RawBreadcrumbItem[]; |
| 85 | + |
| 86 | + // Cluster system tablets don't have tenantName |
| 87 | + if (tenantName) { |
| 88 | + breadcrumbs = getTenantBreadcrumbs(options, query); |
| 89 | + } else { |
| 90 | + breadcrumbs = getClusterBreadcrumbs(options, query); |
| 91 | + } |
| 92 | + |
| 93 | + const link = createHref(routes.tabletsFilters, undefined, { |
| 94 | + nodeIds, |
| 95 | + state, |
| 96 | + type, |
| 97 | + path: tenantName, |
| 98 | + }); |
| 99 | + |
| 100 | + breadcrumbs.push({text: 'Tablets', link}); |
| 101 | + |
| 102 | + return breadcrumbs; |
| 103 | +}; |
| 104 | + |
| 105 | +const getTabletBreadcrubms = ( |
| 106 | + options: TabletBreadcrumbsOptions, |
| 107 | + query = {}, |
| 108 | +): RawBreadcrumbItem[] => { |
| 109 | + const {tabletId} = options; |
| 110 | + |
| 111 | + const breadcrumbs = getTabletsBreadcrubms(options, query); |
| 112 | + |
| 113 | + breadcrumbs.push({ |
| 114 | + text: tabletId || 'Tablet', |
| 115 | + }); |
| 116 | + |
| 117 | + return breadcrumbs; |
| 118 | +}; |
| 119 | + |
| 120 | +export const getBreadcrumbs = ( |
| 121 | + page: Page, |
| 122 | + options: BreadcrumbsOptions, |
| 123 | + rawBreadcrumbs: RawBreadcrumbItem[] = [], |
| 124 | + query = {}, |
| 125 | +) => { |
| 126 | + switch (page) { |
| 127 | + case 'cluster': { |
| 128 | + return [...rawBreadcrumbs, ...getClusterBreadcrumbs(options, query)]; |
| 129 | + } |
| 130 | + case 'tenant': { |
| 131 | + return [...rawBreadcrumbs, ...getTenantBreadcrumbs(options, query)]; |
| 132 | + } |
| 133 | + case 'node': { |
| 134 | + return [...rawBreadcrumbs, ...getNodeBreadcrumbs(options, query)]; |
| 135 | + } |
| 136 | + case 'tablets': { |
| 137 | + return [...rawBreadcrumbs, ...getTabletsBreadcrubms(options, query)]; |
| 138 | + } |
| 139 | + case 'tablet': { |
| 140 | + return [...rawBreadcrumbs, ...getTabletBreadcrubms(options, query)]; |
| 141 | + } |
| 142 | + default: { |
| 143 | + return rawBreadcrumbs; |
| 144 | + } |
| 145 | + } |
| 146 | +}; |
0 commit comments