|
| 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 type * as psp from "@finos/perspective"; |
| 14 | +import type * as pspWorkspace from "@finos/perspective-workspace"; |
| 15 | +import { |
| 16 | + PerspectiveWorkspaceConfig, |
| 17 | + ViewerConfigUpdateExt, |
| 18 | +} from "@finos/perspective-workspace"; |
| 19 | + |
| 20 | +import * as utils from "./utils"; |
| 21 | + |
| 22 | +import * as React from "react"; |
| 23 | + |
| 24 | +interface PerspectiveWorkspaceProps { |
| 25 | + tables: Record<string, Promise<psp.Table>>; |
| 26 | + layout: PerspectiveWorkspaceConfig; |
| 27 | + onLayoutUpdate?: (detail: { |
| 28 | + layout: PerspectiveWorkspaceConfig; |
| 29 | + tables: Record<string, psp.Table | Promise<psp.Table>>; |
| 30 | + }) => void; |
| 31 | + onNewView?: (detail: { |
| 32 | + config: ViewerConfigUpdateExt; |
| 33 | + widget: pspWorkspace.PerspectiveViewerWidget; |
| 34 | + }) => void; |
| 35 | + onToggleGlobalFilter?: (detail: { |
| 36 | + widget: pspWorkspace.PerspectiveViewerWidget; |
| 37 | + isGlobalFilter: boolean; |
| 38 | + }) => void; |
| 39 | + id?: string; |
| 40 | + className?: string; |
| 41 | + style?: React.CSSProperties | undefined; |
| 42 | +} |
| 43 | + |
| 44 | +const PerspectiveWorkspaceImpl = React.forwardRef< |
| 45 | + pspWorkspace.HTMLPerspectiveWorkspaceElement | undefined, |
| 46 | + PerspectiveWorkspaceProps |
| 47 | +>( |
| 48 | + ( |
| 49 | + { |
| 50 | + tables, |
| 51 | + layout, |
| 52 | + onLayoutUpdate = () => {}, |
| 53 | + onNewView = () => {}, |
| 54 | + onToggleGlobalFilter = () => {}, |
| 55 | + id, |
| 56 | + className, |
| 57 | + style, |
| 58 | + }, |
| 59 | + ref |
| 60 | + ) => { |
| 61 | + const [workspace, setWorkspace] = |
| 62 | + React.useState<pspWorkspace.HTMLPerspectiveWorkspaceElement>(); |
| 63 | + |
| 64 | + React.useImperativeHandle(ref, () => workspace, [workspace]); |
| 65 | + |
| 66 | + React.useEffect(() => { |
| 67 | + if (!workspace) return; |
| 68 | + workspace.restore(layout); |
| 69 | + }, [workspace, layout]); |
| 70 | + |
| 71 | + React.useEffect(() => { |
| 72 | + if (!workspace) return; |
| 73 | + workspace.replaceTables(tables); |
| 74 | + }, [workspace, tables]); |
| 75 | + |
| 76 | + utils.usePspListener(workspace, "workspace-new-view", onNewView); |
| 77 | + utils.usePspListener( |
| 78 | + workspace, |
| 79 | + "workspace-layout-update", |
| 80 | + onLayoutUpdate |
| 81 | + ); |
| 82 | + utils.usePspListener( |
| 83 | + workspace, |
| 84 | + "workspace-toggle-global-filter", |
| 85 | + onToggleGlobalFilter |
| 86 | + ); |
| 87 | + |
| 88 | + return ( |
| 89 | + <perspective-workspace |
| 90 | + ref={(r) => setWorkspace(r ?? undefined)} |
| 91 | + id={id} |
| 92 | + className={className} |
| 93 | + style={style} |
| 94 | + ></perspective-workspace> |
| 95 | + ); |
| 96 | + } |
| 97 | +); |
| 98 | + |
| 99 | +export const PerspectiveWorkspace = React.memo(PerspectiveWorkspaceImpl); |
0 commit comments