/
DiagnosisTable.tsx
204 lines (179 loc) · 5.27 KB
/
DiagnosisTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { Button } from 'antd'
import React, { useEffect, useMemo, useRef, useState, useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { LoadingOutlined } from '@ant-design/icons'
import client, { DiagnoseTableDef } from '@lib/client'
import { CardTable, DateTime } from '@lib/components'
import { useClientRequest, RequestFactory } from '@lib/utils/useClientRequest'
import { diagnosisColumns } from '../utils/tableColumns'
// FIXME: use better naming
// stableTimeRange: used to start diagnosing when triggering by clicking "Start" outside this component
// unstableTimeRange: used to start diagnosing when triggering by clicking "Start" inside this component
export interface IDiagnosisTableProps {
stableTimeRange: [number, number]
unstableTimeRange: [number, number]
kind: string
}
type ReqFnType = RequestFactory<DiagnoseTableDef>
// Modified from SearchResult.tsx
function Row({ renderer, props }) {
const [expanded, setExpanded] = useState(false)
const handleClick = useCallback(() => {
setExpanded((v) => !v)
}, [])
// https://stackoverflow.com/questions/53623294/how-to-conditionally-change-a-color-of-a-row-in-detailslist
const backgroundColor = props.item.is_sub ? 'lightcyan' : 'inhert'
return (
<div onClick={handleClick} style={{ cursor: 'pointer' }}>
{renderer({
...props,
styles: { root: { backgroundColor } },
item: { ...props.item, expanded },
})}
</div>
)
}
export default function DiagnosisTable({
stableTimeRange,
unstableTimeRange,
kind,
}: IDiagnosisTableProps) {
const { t } = useTranslation()
const [internalTimeRange, setInternalTimeRange] = useState<[number, number]>([
0,
0,
])
useEffect(() => setInternalTimeRange(stableTimeRange), [stableTimeRange])
function handleStart() {
setInternalTimeRange(unstableTimeRange)
}
const timeChanged = useMemo(
() =>
internalTimeRange[0] !== unstableTimeRange[0] ||
internalTimeRange[1] !== unstableTimeRange[1],
[internalTimeRange, unstableTimeRange]
)
const reqFn = useRef<ReqFnType | null>(null)
useEffect(() => {
reqFn.current = (reqConfig) =>
client.getInstance().diagnoseDiagnosisPost(
{
start_time: internalTimeRange[0],
end_time: internalTimeRange[1],
kind,
},
reqConfig
)
}, [internalTimeRange, kind])
const {
data,
isLoading,
error,
sendRequest,
} = useClientRequest(reqFn.current!, { immediate: false })
useEffect(() => {
if (internalTimeRange[0] !== 0) {
sendRequest()
}
}, [internalTimeRange, sendRequest])
////////////////
const allRows = useMemo(() => {
const _columnHeaders =
data?.column?.map((col) => col.toLocaleLowerCase()) || []
let _rows: any[] = []
data?.rows?.forEach((row, rowIdx) => {
// values (array)
let _newRow = { row_idx: rowIdx, is_sub: false, show_sub: false }
row.values?.forEach((v, v_idx) => {
const key = _columnHeaders[v_idx]
_newRow[key] = v
})
//subvalues (2 demensional array)
let _subRows: any[] = []
row.sub_values?.forEach((sub_v) => {
let _subRow = { row_idx: rowIdx, is_sub: true }
sub_v.forEach((v, idx) => {
const key = _columnHeaders[idx]
_subRow[key] = v
})
_subRows.push(_subRow)
})
_newRow['sub_rows'] = _subRows
_rows.push(_newRow)
})
return _rows
}, [data])
const [items, setItems] = useState(allRows)
useEffect(() => {
setItems(allRows)
}, [allRows])
const toggleShowSub = useCallback(
(rowIdx, showSub) => {
let newRows = [...items]
let curRowPos = newRows.findIndex(
(el) => el.row_idx === rowIdx && el.is_sub === false
)
if (curRowPos === -1) {
return
}
let curRow = newRows[curRowPos]
// update status
curRow.show_sub = showSub
if (showSub) {
// insert sub rows
newRows.splice(curRowPos + 1, 0, ...curRow.sub_rows)
} else {
// remove sub rows
newRows.splice(curRowPos + 1, curRow.sub_rows.length)
}
setItems(newRows)
},
[items]
)
const columns = useMemo(() => diagnosisColumns(items, toggleShowSub), [
items,
toggleShowSub,
])
////////////////
const renderRow = useCallback((props, defaultRender) => {
if (!props) {
return null
}
return <Row renderer={defaultRender!} props={props} />
}, [])
////////////////
function cardExtra() {
if (isLoading) {
return <LoadingOutlined />
}
if (timeChanged || error) {
return (
<Button onClick={handleStart}>{t('diagnose.generate.submit')}</Button>
)
}
return null
}
function subTitle() {
if (internalTimeRange[0] > 0) {
return (
<span>
<DateTime.Calendar unixTimestampMs={internalTimeRange[0] * 1000} /> ~{' '}
<DateTime.Calendar unixTimestampMs={internalTimeRange[1] * 1000} />
</span>
)
}
return null
}
return (
<CardTable
title={t(`diagnose.table_title.${kind}_diagnosis`)}
subTitle={subTitle()}
cardExtra={cardExtra()}
errors={[error]}
columns={columns}
items={items}
onRenderRow={renderRow}
extendLastColumn
/>
)
}