-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCellPanel.tsx
More file actions
61 lines (55 loc) · 1.84 KB
/
CellPanel.tsx
File metadata and controls
61 lines (55 loc) · 1.84 KB
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
import { DataFrame, stringify } from 'hightable'
import { useEffect, useState } from 'react'
import ContentHeader from './ContentHeader.js'
interface ViewerProps {
df: DataFrame
row: number
col: number
setProgress: (progress: number) => void
setError: (error: Error) => void
onClose: () => void
}
/**
* Cell viewer displays a single cell from a table.
*/
export default function CellPanel({ df, row, col, setProgress, setError, onClose }: ViewerProps) {
const [text, setText] = useState<string | undefined>()
// Load cell data
useEffect(() => {
async function loadCellData() {
try {
setProgress(0.5)
const asyncRows = df.rows({ start: row, end: row + 1 })
if (asyncRows.length > 1 || !(0 in asyncRows)) {
throw new Error(`Expected 1 row, got ${asyncRows.length}`)
}
const asyncRow = asyncRows[0]
// Await cell data
const columnName = df.header[col]
if (columnName === undefined) {
throw new Error(`Column name missing at index col=${col}`)
}
const asyncCell = asyncRow.cells[columnName]
if (asyncCell === undefined) {
throw new Error(`Cell missing at column ${columnName}`)
}
/* TODO(SL): use the same implementation of stringify, here and in Cell.tsx */
const text = await asyncCell.then(cell => stringify(cell as unknown) ?? '{}')
setText(text)
} catch (error) {
setError(error as Error)
} finally {
setProgress(1)
}
}
void loadCellData()
}, [df, col, row, setProgress, setError])
const headers = <>
<button className="slideClose" onClick={onClose}> </button>
<span>column `{df.header[col]}`</span>
<span>row {row + 1}</span>
</>
return <ContentHeader headers={headers}>
<code className="text">{text}</code>
</ContentHeader>
}