-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCell.tsx
More file actions
74 lines (65 loc) · 2.47 KB
/
Cell.tsx
File metadata and controls
74 lines (65 loc) · 2.47 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
62
63
64
65
66
67
68
69
70
71
72
73
74
import { stringify } from 'hightable'
import { asyncBufferFromUrl, parquetMetadataAsync } from 'hyparquet'
import { useEffect, useState } from 'react'
import { useConfig } from '../../hooks/useConfig.js'
import type { FileSource } from '../../lib/sources/types.js'
import { parquetDataFrame } from '../../lib/tableProvider.js'
import { cn } from '../../lib/utils.js'
import Breadcrumb from '../Breadcrumb/Breadcrumb.js'
import Layout from '../Layout/Layout.js'
import styles from '../TextView/TextView.module.css'
interface CellProps {
source: FileSource
row: number
col: number
}
const UNLOADED_CELL_PLACEHOLDER = '<the content has not been fetched yet>'
/**
* Cell viewer displays a single cell from a table.
*/
export default function CellView({ source, row, col }: CellProps) {
const [text, setText] = useState<string | undefined>()
const [progress, setProgress] = useState<number>()
const [error, setError] = useState<Error>()
const { customClass } = useConfig()
// File path from url
const { resolveUrl, requestInit, fileName } = source
// Load cell data
useEffect(() => {
async function loadCellData() {
try {
// TODO: handle first row > 100kb
setProgress(0.25)
const asyncBuffer = await asyncBufferFromUrl({ url: resolveUrl, requestInit })
const from = { url: resolveUrl, requestInit, byteLength: asyncBuffer.byteLength }
setProgress(0.5)
const metadata = await parquetMetadataAsync(asyncBuffer)
setProgress(0.75)
const df = parquetDataFrame(from, metadata)
const columnName = df.columnDescriptors[col]?.name
if (columnName === undefined) {
throw new Error(`Column name missing at index col=${col}`)
}
await df.fetch?.({ rowStart: row, rowEnd: row + 1, columns: [columnName] })
const cell = df.getCell({ row, column: columnName })
const text = cell === undefined ? UNLOADED_CELL_PLACEHOLDER : stringify(cell.value)
setText(text)
setError(undefined)
} catch (error) {
setError(error as Error)
setText(undefined)
} finally {
setProgress(undefined)
}
}
setProgress(0)
void loadCellData()
}, [resolveUrl, requestInit, col, row])
return (
<Layout progress={progress} error={error} title={fileName}>
<Breadcrumb source={source} />
{/* <Highlight text={text || ''} /> */}
<pre className={cn(styles.textView, customClass?.textView)}>{text}</pre>
</Layout>
)
}