|
10 | 10 | // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
|
11 | 11 | // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
|
12 | 12 |
|
13 |
| -import * as React from "react"; |
14 |
| -import { createRoot } from "react-dom/client"; |
15 |
| - |
16 |
| -import * as psp from "@finos/perspective-react"; |
17 |
| -import { |
18 |
| - PerspectiveProvider, |
19 |
| - PerspectiveViewer, |
20 |
| -} from "@finos/perspective-react"; |
21 |
| -import perspective, { Table } from "@finos/perspective"; |
| 13 | +import perspective from "@finos/perspective"; |
22 | 14 | import perspective_viewer from "@finos/perspective-viewer";
|
23 |
| - |
24 |
| -// Import perspective viewer plugins to register them on the web component. |
25 | 15 | import "@finos/perspective-viewer-datagrid";
|
26 | 16 | import "@finos/perspective-viewer-d3fc";
|
27 | 17 |
|
28 |
| -// Refer to bootstrapping link above |
29 |
| -import SERVER_WASM from "@finos/perspective/dist/wasm/perspective-server.wasm"; |
30 |
| -import CLIENT_WASM from "@finos/perspective-viewer/dist/wasm/perspective-viewer.wasm"; |
| 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"; |
31 | 23 |
|
32 | 24 | await Promise.all([
|
33 | 25 | perspective.init_server(fetch(SERVER_WASM)),
|
34 | 26 | perspective_viewer.init_client(fetch(CLIENT_WASM)),
|
35 | 27 | ]);
|
36 | 28 |
|
37 |
| -// Now WASM has booted and we can construct a web worker to host our tables. |
38 |
| -const worker = await perspective.worker(); |
| 29 | +import type * as psp from "@finos/perspective"; |
| 30 | +import type * as pspViewer from "@finos/perspective-viewer"; |
39 | 31 |
|
40 |
| -const Root: React.FC = () => { |
41 |
| - return ( |
42 |
| - <PerspectiveProvider> |
43 |
| - <App /> |
44 |
| - </PerspectiveProvider> |
45 |
| - ); |
| 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 | 46 | };
|
47 | 47 |
|
48 |
| -const App: React.FC = () => { |
49 |
| - const actions = psp.useActions(); |
50 |
| - const [selectedTable, setSelectedTable] = |
51 |
| - React.useState<string>("my_table"); |
52 |
| - // DON'T MOVE THIS WITHOUT UPDATING {docs/md/how_to/javascript/react.md} |
53 |
| - // This is just for testing and shouldn't be visible in our docs. |
| 48 | +import * as React from "react"; |
| 49 | +import { PerspectiveViewer } from "@finos/perspective-react"; |
54 | 50 |
|
55 |
| - React.useEffect(() => { |
56 |
| - (window as any).__PSP_REACT_TEST_HARNESS__ = { |
57 |
| - worker, |
58 |
| - actions, |
59 |
| - setSelectedTable, |
60 |
| - }; |
61 |
| - }, []); |
| 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 | +} |
62 | 59 |
|
63 |
| - // DON'T MOVE THIS WITHOUT UPDATING {docs/md/how_to/javascript/react.md} |
| 60 | +export const App: React.FC = () => { |
| 61 | + const [state, setState] = React.useState<ToolbarState>(() => ({ |
| 62 | + mounted: true, |
| 63 | + table: createNewSuperstoreTable(), |
| 64 | + config: { ...CONFIG }, |
| 65 | + })); |
64 | 66 |
|
65 | 67 | React.useEffect(() => {
|
66 |
| - actions.addTable({ |
67 |
| - table: "my_table", |
68 |
| - promise: worker.table({ x: [1, 2, 3] }), |
69 |
| - }); |
70 | 68 | return () => {
|
71 |
| - actions.removeTable({ table: "my_table" }); |
| 69 | + state.table?.then((table) => table?.delete({ lazy: true })); |
72 | 70 | };
|
73 | 71 | }, []);
|
74 | 72 |
|
75 |
| - return <PerspectiveViewer selectedTable={selectedTable} />; |
76 |
| -}; |
| 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 | + }; |
77 | 81 |
|
78 |
| -createRoot(document.getElementById("root")!).render(<Root />); |
| 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