|
| 1 | +import { ReactNode } from 'react' |
| 2 | +import Page, { PageProps } from './Page.js' |
| 3 | +import Welcome from './Welcome.js' |
| 4 | + |
| 5 | +import { DataFrame, rowCache } from 'hightable' |
| 6 | +import { FileMetaData, byteLengthFromUrl, parquetMetadataAsync, parquetSchema } from 'hyparquet' |
| 7 | +import { useCallback, useEffect, useState } from 'react' |
| 8 | +import Dropzone from './Dropzone.js' |
| 9 | +import Layout from './Layout.js' |
| 10 | +import { asyncBufferFrom } from './utils.js' |
| 11 | +import { parquetQueryWorker } from './workers/parquetWorkerClient.js' |
| 12 | +import { AsyncBufferFrom, Row } from './workers/types.js' |
| 13 | + |
| 14 | +export default function App(): ReactNode { |
| 15 | + const params = new URLSearchParams(location.search) |
| 16 | + const url = params.get('key') ?? undefined |
| 17 | + |
| 18 | + const [error, setError] = useState<Error>() |
| 19 | + const [pageProps, setPageProps] = useState<PageProps>() |
| 20 | + |
| 21 | + const setUnknownError = useCallback((e: unknown) => { |
| 22 | + setError(e instanceof Error ? e : new Error(String(e))) |
| 23 | + }, []) |
| 24 | + |
| 25 | + const onUrlDrop = useCallback( |
| 26 | + (url: string) => { |
| 27 | + // Add key=url to query string |
| 28 | + const params = new URLSearchParams(location.search) |
| 29 | + params.set('key', url) |
| 30 | + history.pushState({}, '', `${location.pathname}?${params}`) |
| 31 | + byteLengthFromUrl(url).then(byteLength => setAsyncBuffer(url, { url, byteLength })).catch(setUnknownError) |
| 32 | + }, |
| 33 | + [setUnknownError], |
| 34 | + ) |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (!pageProps && url) { |
| 38 | + onUrlDrop(url) |
| 39 | + } |
| 40 | + }, [ url, pageProps, onUrlDrop]) |
| 41 | + |
| 42 | + function onFileDrop(file: File) { |
| 43 | + // Clear query string |
| 44 | + history.pushState({}, '', location.pathname) |
| 45 | + setAsyncBuffer(file.name, { file, byteLength: file.size }).catch(setUnknownError) |
| 46 | + } |
| 47 | + |
| 48 | + async function setAsyncBuffer(name: string, from: AsyncBufferFrom) { |
| 49 | + const asyncBuffer = await asyncBufferFrom(from) |
| 50 | + const metadata = await parquetMetadataAsync(asyncBuffer) |
| 51 | + const df = rowCache(parquetDataFrame(from, metadata)) |
| 52 | + setPageProps({ metadata, df, name, byteLength: from.byteLength, setError }) |
| 53 | + } |
| 54 | + |
| 55 | + return <Layout error={error}> |
| 56 | + <Dropzone |
| 57 | + onError={(e) => { setError(e) }} |
| 58 | + onFileDrop={onFileDrop} |
| 59 | + onUrlDrop={onUrlDrop}> |
| 60 | + {pageProps ? <Page {...pageProps} /> : <Welcome />} |
| 61 | + </Dropzone> |
| 62 | + </Layout> |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Convert a parquet file into a dataframe. |
| 67 | + */ |
| 68 | +function parquetDataFrame(from: AsyncBufferFrom, metadata: FileMetaData): DataFrame { |
| 69 | + const { children } = parquetSchema(metadata) |
| 70 | + return { |
| 71 | + header: children.map(child => child.element.name), |
| 72 | + numRows: Number(metadata.num_rows), |
| 73 | + /** |
| 74 | + * @param {number} rowStart |
| 75 | + * @param {number} rowEnd |
| 76 | + * @param {string} orderBy |
| 77 | + * @returns {Promise<any[][]>} |
| 78 | + */ |
| 79 | + rows(rowStart: number, rowEnd: number, orderBy: string): Promise<Row[]> { |
| 80 | + console.log(`reading rows ${rowStart}-${rowEnd}`, orderBy) |
| 81 | + return parquetQueryWorker({ from, metadata, rowStart, rowEnd, orderBy }) |
| 82 | + }, |
| 83 | + sortable: true, |
| 84 | + } |
| 85 | +} |
0 commit comments