|
| 1 | +import {duration} from '@gravity-ui/date-utils'; |
| 2 | +import type {Column as DataTableColumn} from '@gravity-ui/react-data-table'; |
| 3 | +import {Text} from '@gravity-ui/uikit'; |
| 4 | + |
| 5 | +import {CellWithPopover} from '../../components/CellWithPopover/CellWithPopover'; |
| 6 | +import type {TOperation} from '../../types/api/operationList'; |
| 7 | +import {EStatusCode} from '../../types/api/operationList'; |
| 8 | +import {EMPTY_DATA_PLACEHOLDER, HOUR_IN_SECONDS, SECOND_IN_MS} from '../../utils/constants'; |
| 9 | +import {formatDateTime} from '../../utils/dataFormatters/dataFormatters'; |
| 10 | +import {parseProtobufTimestampToMs} from '../../utils/timeParsers'; |
| 11 | + |
| 12 | +import {COLUMNS_NAMES, COLUMNS_TITLES} from './constants'; |
| 13 | +import i18n from './i18n'; |
| 14 | + |
| 15 | +export function getColumns(): DataTableColumn<TOperation>[] { |
| 16 | + return [ |
| 17 | + { |
| 18 | + name: COLUMNS_NAMES.ID, |
| 19 | + header: COLUMNS_TITLES[COLUMNS_NAMES.ID], |
| 20 | + width: 340, |
| 21 | + render: ({row}) => { |
| 22 | + if (!row.id) { |
| 23 | + return EMPTY_DATA_PLACEHOLDER; |
| 24 | + } |
| 25 | + return ( |
| 26 | + <CellWithPopover placement={['top', 'bottom']} content={row.id}> |
| 27 | + {row.id} |
| 28 | + </CellWithPopover> |
| 29 | + ); |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: COLUMNS_NAMES.STATUS, |
| 34 | + header: COLUMNS_TITLES[COLUMNS_NAMES.STATUS], |
| 35 | + render: ({row}) => { |
| 36 | + if (!row.status) { |
| 37 | + return EMPTY_DATA_PLACEHOLDER; |
| 38 | + } |
| 39 | + return ( |
| 40 | + <Text color={row.status === EStatusCode.SUCCESS ? 'positive' : 'danger'}> |
| 41 | + {row.status} |
| 42 | + </Text> |
| 43 | + ); |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: COLUMNS_NAMES.CREATED_BY, |
| 48 | + header: COLUMNS_TITLES[COLUMNS_NAMES.CREATED_BY], |
| 49 | + render: ({row}) => { |
| 50 | + if (!row.created_by) { |
| 51 | + return EMPTY_DATA_PLACEHOLDER; |
| 52 | + } |
| 53 | + return row.created_by; |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: COLUMNS_NAMES.CREATE_TIME, |
| 58 | + header: COLUMNS_TITLES[COLUMNS_NAMES.CREATE_TIME], |
| 59 | + render: ({row}) => { |
| 60 | + if (!row.create_time) { |
| 61 | + return EMPTY_DATA_PLACEHOLDER; |
| 62 | + } |
| 63 | + return formatDateTime(parseProtobufTimestampToMs(row.create_time)); |
| 64 | + }, |
| 65 | + sortAccessor: (row) => |
| 66 | + row.create_time ? parseProtobufTimestampToMs(row.create_time) : 0, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: COLUMNS_NAMES.END_TIME, |
| 70 | + header: COLUMNS_TITLES[COLUMNS_NAMES.END_TIME], |
| 71 | + render: ({row}) => { |
| 72 | + if (!row.end_time) { |
| 73 | + return EMPTY_DATA_PLACEHOLDER; |
| 74 | + } |
| 75 | + return formatDateTime(parseProtobufTimestampToMs(row.end_time)); |
| 76 | + }, |
| 77 | + sortAccessor: (row) => |
| 78 | + row.end_time ? parseProtobufTimestampToMs(row.end_time) : Number.MAX_SAFE_INTEGER, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: COLUMNS_NAMES.DURATION, |
| 82 | + header: COLUMNS_TITLES[COLUMNS_NAMES.DURATION], |
| 83 | + render: ({row}) => { |
| 84 | + let durationValue = 0; |
| 85 | + if (!row.create_time) { |
| 86 | + return EMPTY_DATA_PLACEHOLDER; |
| 87 | + } |
| 88 | + const createTime = parseProtobufTimestampToMs(row.create_time); |
| 89 | + if (row.end_time) { |
| 90 | + const endTime = parseProtobufTimestampToMs(row.end_time); |
| 91 | + durationValue = endTime - createTime; |
| 92 | + } else { |
| 93 | + durationValue = Date.now() - createTime; |
| 94 | + } |
| 95 | + |
| 96 | + const durationFormatted = |
| 97 | + durationValue > HOUR_IN_SECONDS * SECOND_IN_MS |
| 98 | + ? duration(durationValue).format('hh:mm:ss') |
| 99 | + : duration(durationValue).format('mm:ss'); |
| 100 | + |
| 101 | + return row.end_time |
| 102 | + ? durationFormatted |
| 103 | + : i18n('label_duration-ongoing', {value: durationFormatted}); |
| 104 | + }, |
| 105 | + sortAccessor: (row) => { |
| 106 | + if (!row.create_time) { |
| 107 | + return 0; |
| 108 | + } |
| 109 | + const createTime = parseProtobufTimestampToMs(row.create_time); |
| 110 | + if (row.end_time) { |
| 111 | + const endTime = parseProtobufTimestampToMs(row.end_time); |
| 112 | + return endTime - createTime; |
| 113 | + } |
| 114 | + return Date.now() - createTime; |
| 115 | + }, |
| 116 | + }, |
| 117 | + ]; |
| 118 | +} |
0 commit comments