|
| 1 | +import {ArrowsRotateRight} from '@gravity-ui/icons'; |
| 2 | +import type {Column as DataTableColumn} from '@gravity-ui/react-data-table'; |
| 3 | +import {Icon, Label, Text} from '@gravity-ui/uikit'; |
| 4 | +import {skipToken} from '@reduxjs/toolkit/query'; |
| 5 | + |
| 6 | +import {ButtonWithConfirmDialog} from '../../../../components/ButtonWithConfirmDialog/ButtonWithConfirmDialog'; |
| 7 | +import {EntityStatus} from '../../../../components/EntityStatus/EntityStatus'; |
| 8 | +import {ResponseError} from '../../../../components/Errors/ResponseError'; |
| 9 | +import {InternalLink} from '../../../../components/InternalLink'; |
| 10 | +import {ResizeableDataTable} from '../../../../components/ResizeableDataTable/ResizeableDataTable'; |
| 11 | +import {TableSkeleton} from '../../../../components/TableSkeleton/TableSkeleton'; |
| 12 | +import routes, {createHref} from '../../../../routes'; |
| 13 | +import {selectTabletsWithFqdn, tabletsApi} from '../../../../store/reducers/tablets'; |
| 14 | +import {ETabletState} from '../../../../types/api/tablet'; |
| 15 | +import type {TTabletStateInfo} from '../../../../types/api/tablet'; |
| 16 | +import type {TabletsApiRequestParams} from '../../../../types/store/tablets'; |
| 17 | +import {cn} from '../../../../utils/cn'; |
| 18 | +import {DEFAULT_TABLE_SETTINGS} from '../../../../utils/constants'; |
| 19 | +import {calcUptime} from '../../../../utils/dataFormatters/dataFormatters'; |
| 20 | +import {useTypedDispatch, useTypedSelector} from '../../../../utils/hooks'; |
| 21 | +import {mapTabletStateToLabelTheme} from '../../../../utils/tablet'; |
| 22 | +import {getDefaultNodePath} from '../../../Node/NodePages'; |
| 23 | + |
| 24 | +import i18n from './i18n'; |
| 25 | + |
| 26 | +const b = cn('tablets-table'); |
| 27 | + |
| 28 | +const columns: DataTableColumn<TTabletStateInfo & {fqdn?: string}>[] = [ |
| 29 | + { |
| 30 | + name: 'Type', |
| 31 | + get header() { |
| 32 | + return i18n('Type'); |
| 33 | + }, |
| 34 | + render: ({row}) => { |
| 35 | + return ( |
| 36 | + <span> |
| 37 | + {row.Type} {row.Leader ? <Text color="secondary">leader</Text> : ''} |
| 38 | + </span> |
| 39 | + ); |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'TabletId', |
| 44 | + get header() { |
| 45 | + return i18n('Tablet'); |
| 46 | + }, |
| 47 | + render: ({row}) => { |
| 48 | + const tabletPath = |
| 49 | + row.TabletId && |
| 50 | + createHref(routes.tablet, {id: row.TabletId}, {nodeId: row.NodeId, type: row.Type}); |
| 51 | + |
| 52 | + return <InternalLink to={tabletPath}>{row.TabletId}</InternalLink>; |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'State', |
| 57 | + get header() { |
| 58 | + return i18n('State'); |
| 59 | + }, |
| 60 | + render: ({row}) => { |
| 61 | + return <Label theme={mapTabletStateToLabelTheme(row.State)}>{row.State}</Label>; |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'NodeId', |
| 66 | + get header() { |
| 67 | + return i18n('Node ID'); |
| 68 | + }, |
| 69 | + render: ({row}) => { |
| 70 | + const nodePath = row.NodeId === undefined ? undefined : getDefaultNodePath(row.NodeId); |
| 71 | + return <InternalLink to={nodePath}>{row.NodeId}</InternalLink>; |
| 72 | + }, |
| 73 | + align: 'right', |
| 74 | + }, |
| 75 | + { |
| 76 | + name: 'FQDN', |
| 77 | + get header() { |
| 78 | + return i18n('Node FQDN'); |
| 79 | + }, |
| 80 | + render: ({row}) => { |
| 81 | + if (!row.fqdn) { |
| 82 | + return <span>—</span>; |
| 83 | + } |
| 84 | + return <EntityStatus name={row.fqdn} showStatus={false} hasClipboardButton />; |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: 'Generation', |
| 89 | + get header() { |
| 90 | + return i18n('Generation'); |
| 91 | + }, |
| 92 | + align: 'right', |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'Uptime', |
| 96 | + get header() { |
| 97 | + return i18n('Uptime'); |
| 98 | + }, |
| 99 | + render: ({row}) => { |
| 100 | + return calcUptime(row.ChangeTime); |
| 101 | + }, |
| 102 | + sortAccessor: (row) => -Number(row.ChangeTime), |
| 103 | + align: 'right', |
| 104 | + }, |
| 105 | + { |
| 106 | + name: 'Actions', |
| 107 | + sortable: false, |
| 108 | + resizeable: false, |
| 109 | + header: '', |
| 110 | + render: ({row}) => { |
| 111 | + return <TabletActions {...row} />; |
| 112 | + }, |
| 113 | + }, |
| 114 | +]; |
| 115 | + |
| 116 | +function TabletActions(tablet: TTabletStateInfo) { |
| 117 | + const isDisabledRestart = tablet.State === ETabletState.Stopped; |
| 118 | + const dispatch = useTypedDispatch(); |
| 119 | + return ( |
| 120 | + <ButtonWithConfirmDialog |
| 121 | + buttonView="outlined" |
| 122 | + dialogContent={i18n('dialog.kill')} |
| 123 | + onConfirmAction={() => { |
| 124 | + return window.api.killTablet(tablet.TabletId); |
| 125 | + }} |
| 126 | + onConfirmActionSuccess={() => { |
| 127 | + dispatch(tabletsApi.util.invalidateTags(['All'])); |
| 128 | + }} |
| 129 | + buttonDisabled={isDisabledRestart} |
| 130 | + > |
| 131 | + <Icon data={ArrowsRotateRight} /> |
| 132 | + </ButtonWithConfirmDialog> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +interface TabletsProps { |
| 137 | + path?: string; |
| 138 | + className?: string; |
| 139 | +} |
| 140 | + |
| 141 | +export function Tablets({path, className}: TabletsProps) { |
| 142 | + const {autorefresh} = useTypedSelector((state) => state.schema); |
| 143 | + |
| 144 | + let params: TabletsApiRequestParams | typeof skipToken = skipToken; |
| 145 | + if (path) { |
| 146 | + params = {path}; |
| 147 | + } |
| 148 | + const {currentData, isFetching, error} = tabletsApi.useGetTabletsInfoQuery(params, { |
| 149 | + pollingInterval: autorefresh, |
| 150 | + }); |
| 151 | + |
| 152 | + const loading = isFetching && currentData === undefined; |
| 153 | + const tablets = useTypedSelector((state) => selectTabletsWithFqdn(state, path || '')); |
| 154 | + |
| 155 | + if (loading) { |
| 156 | + return <TableSkeleton />; |
| 157 | + } |
| 158 | + if (error) { |
| 159 | + return <ResponseError error={error} />; |
| 160 | + } |
| 161 | + |
| 162 | + return ( |
| 163 | + <div className={b(null, className)}> |
| 164 | + <ResizeableDataTable |
| 165 | + columns={columns} |
| 166 | + data={tablets} |
| 167 | + settings={DEFAULT_TABLE_SETTINGS} |
| 168 | + emptyDataMessage={i18n('noTabletsData')} |
| 169 | + /> |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments