|
| 1 | +// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +// ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +// ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | +import perspective from "@finos/perspective"; |
| 14 | +import perspective_viewer from "@finos/perspective-viewer"; |
| 15 | +import "@finos/perspective-viewer-datagrid"; |
| 16 | +import "@finos/perspective-viewer-d3fc"; |
| 17 | + |
| 18 | +// @ts-ignore |
| 19 | +import SERVER_WASM from "@finos/perspective/dist/wasm/perspective-server.wasm?url"; |
| 20 | + |
| 21 | +// @ts-ignore |
| 22 | +import CLIENT_WASM from "@finos/perspective-viewer/dist/wasm/perspective-viewer.wasm?url"; |
| 23 | + |
| 24 | +await Promise.all([ |
| 25 | + perspective.init_server(fetch(SERVER_WASM)), |
| 26 | + perspective_viewer.init_client(fetch(CLIENT_WASM)), |
| 27 | +]); |
| 28 | + |
| 29 | +import type * as psp from "@finos/perspective"; |
| 30 | +import type * as pspViewer from "@finos/perspective-viewer"; |
| 31 | + |
| 32 | +// @ts-ignore |
| 33 | +import SUPERSTORE_ARROW from "superstore-arrow/superstore.lz4.arrow?url"; |
| 34 | + |
| 35 | +const WORKER = await perspective.worker(); |
| 36 | + |
| 37 | +async function createNewSuperstoreTable(): Promise<psp.Table> { |
| 38 | + const req = fetch(SUPERSTORE_ARROW); |
| 39 | + const resp = await req; |
| 40 | + const buffer = await resp.arrayBuffer(); |
| 41 | + return await WORKER.table(buffer); |
| 42 | +} |
| 43 | + |
| 44 | +const CONFIG: pspViewer.ViewerConfigUpdate = { |
| 45 | + group_by: ["State"], |
| 46 | +}; |
| 47 | + |
| 48 | +import * as React from "react"; |
| 49 | +import { PerspectiveViewer } from "@finos/perspective-react"; |
| 50 | + |
| 51 | +import "@finos/perspective-viewer/dist/css/themes.css"; |
| 52 | +import "./index.css"; |
| 53 | + |
| 54 | +interface ToolbarState { |
| 55 | + mounted: boolean; |
| 56 | + table?: Promise<psp.Table>; |
| 57 | + config: pspViewer.ViewerConfigUpdate; |
| 58 | +} |
| 59 | + |
| 60 | +export const App: React.FC = () => { |
| 61 | + const [state, setState] = React.useState<ToolbarState>(() => ({ |
| 62 | + mounted: true, |
| 63 | + table: createNewSuperstoreTable(), |
| 64 | + config: { ...CONFIG }, |
| 65 | + })); |
| 66 | + |
| 67 | + React.useEffect(() => { |
| 68 | + return () => { |
| 69 | + state.table?.then((table) => table?.delete({ lazy: true })); |
| 70 | + }; |
| 71 | + }, []); |
| 72 | + |
| 73 | + const onConfigUpdate = (config: pspViewer.ViewerConfigUpdate) => { |
| 74 | + console.log("Config Update Event", config); |
| 75 | + setState({ ...state, config }); |
| 76 | + }; |
| 77 | + |
| 78 | + const onClick = (detail: pspViewer.PerspectiveClickEventDetail) => { |
| 79 | + console.log("Click Event,", detail); |
| 80 | + }; |
| 81 | + |
| 82 | + const onSelect = (detail: pspViewer.PerspectiveSelectEventDetail) => { |
| 83 | + console.log("Select Event", detail); |
| 84 | + }; |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className="container"> |
| 88 | + {state.mounted && ( |
| 89 | + <> |
| 90 | + <PerspectiveViewer table={state.table} /> |
| 91 | + <PerspectiveViewer |
| 92 | + table={state.table} |
| 93 | + config={state.config} |
| 94 | + onClick={onClick} |
| 95 | + onSelect={onSelect} |
| 96 | + onConfigUpdate={onConfigUpdate} |
| 97 | + /> |
| 98 | + </> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
0 commit comments