Skip to content

Commit

Permalink
refactor: Convert core DashboardPlugins to new config type (deephaven…
Browse files Browse the repository at this point in the history
…#1519)

Fixes deephaven#1507 

Didn't convert `ConsolePlugin` yet. The `notebooksUrl` prop was added
for markdown notebooks, but uses a Vite env variable to set it
  • Loading branch information
mattrunyon authored Sep 25, 2023
1 parent 3e834de commit 1442ace
Show file tree
Hide file tree
Showing 30 changed files with 418 additions and 285 deletions.
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/app-utils/src/components/AppBootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RefreshTokenBootstrap,
useBroadcastLoginListener,
} from '@deephaven/jsapi-components';
import { type DashboardPlugin } from '@deephaven/plugin';
import FontBootstrap from './FontBootstrap';
import PluginsBootstrap from './PluginsBootstrap';
import AuthBootstrap from './AuthBootstrap';
Expand All @@ -21,6 +22,9 @@ export type AppBootstrapProps = {
/** URL of the plugins to load. */
pluginsUrl: string;

/** The core plugins to load. */
getCorePlugins?: () => Promise<DashboardPlugin[]>;

/** Font class names to load. */
fontClassNames?: string[];

Expand All @@ -37,6 +41,7 @@ export type AppBootstrapProps = {
export function AppBootstrap({
fontClassNames,
pluginsUrl,
getCorePlugins,
serverUrl,
children,
}: AppBootstrapProps): JSX.Element {
Expand All @@ -51,7 +56,7 @@ export function AppBootstrap({
useBroadcastLoginListener(onLogin, onLogout);
return (
<FontBootstrap fontClassNames={fontClassNames}>
<PluginsBootstrap pluginsUrl={pluginsUrl}>
<PluginsBootstrap getCorePlugins={getCorePlugins} pluginsUrl={pluginsUrl}>
<ClientBootstrap
serverUrl={serverUrl}
options={clientOptions}
Expand Down
13 changes: 11 additions & 2 deletions packages/app-utils/src/components/PluginsBootstrap.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type DashboardPlugin } from '@deephaven/plugin';
import React, { createContext, useEffect, useState } from 'react';
import { PluginModuleMap, loadModulePlugins } from '../plugins';

Expand All @@ -9,6 +10,9 @@ export type PluginsBootstrapProps = {
*/
pluginsUrl: string;

/** The core plugins to load. */
getCorePlugins?: () => Promise<DashboardPlugin[]>;

/**
* The children to render wrapped with the PluginsContext.
* Note that it renders the children even if the plugins aren't loaded yet.
Expand All @@ -21,24 +25,29 @@ export type PluginsBootstrapProps = {
*/
export function PluginsBootstrap({
pluginsUrl,
getCorePlugins,
children,
}: PluginsBootstrapProps): JSX.Element {
const [plugins, setPlugins] = useState<PluginModuleMap | null>(null);
useEffect(
function initPlugins() {
let isCanceled = false;
async function loadPlugins(): Promise<void> {
const corePlugins = (await getCorePlugins?.()) ?? [];
const pluginModules = await loadModulePlugins(pluginsUrl);
if (!isCanceled) {
setPlugins(pluginModules);
const corePluginPairs = corePlugins.map(
plugin => [plugin.name, plugin] as const
);
setPlugins(new Map([...corePluginPairs, ...pluginModules]));
}
}
loadPlugins();
return () => {
isCanceled = true;
};
},
[pluginsUrl]
[pluginsUrl, getCorePlugins]
);

return (
Expand Down
1 change: 1 addition & 0 deletions packages/app-utils/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './usePlugins';
export * from './useConnection';
export * from './useServerConfig';
export * from './useUser';
export * from './useLoadTablePlugin';
42 changes: 42 additions & 0 deletions packages/app-utils/src/components/useLoadTablePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useCallback } from 'react';
import {
type TablePluginComponent,
isTablePlugin,
isLegacyTablePlugin,
} from '@deephaven/plugin';
import Log from '@deephaven/log';
import usePlugins from './usePlugins';

const log = Log.module('@deephaven/app-utils/useTablePlugin');

/**
* Creates a table plugin loader function.
* @returns A function to load a Table plugin element by name
*/
export function useLoadTablePlugin(): (name: string) => TablePluginComponent {
const plugins = usePlugins();

const plugin = useCallback(
(name: string) => {
// First check if we have any plugin modules loaded that match the TablePlugin.
const pluginModule = plugins.get(name);
if (pluginModule != null) {
if (isTablePlugin(pluginModule)) {
return pluginModule.component;
}
if (isLegacyTablePlugin(pluginModule)) {
return pluginModule.TablePlugin;
}
}

const errorMessage = `Unable to find table plugin ${name}.`;
log.error(errorMessage);
throw new Error(errorMessage);
},
[plugins]
);

return plugin;
}

export default useLoadTablePlugin;
9 changes: 7 additions & 2 deletions packages/code-studio/src/AppRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import React from 'react';
import { Provider } from 'react-redux';
import { MonacoUtils } from '@deephaven/console';
import { store } from '@deephaven/redux';
import { DownloadServiceWorkerUtils } from '@deephaven/iris-grid';
import MonacoWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import AppRouter from './main/AppRouter';
import DownloadServiceWorkerUtils from './DownloadServiceWorkerUtils';

export function AppRoot(): JSX.Element {
DownloadServiceWorkerUtils.registerOnLoaded();
DownloadServiceWorkerUtils.register(
new URL(
`${import.meta.env.BASE_URL ?? ''}download/serviceWorker.js`,
window.location.href
)
);
MonacoUtils.init({ getWorker: () => new MonacoWorker() });

// disable annoying dnd-react warnings
Expand Down
31 changes: 30 additions & 1 deletion packages/code-studio/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,39 @@ const pluginsURL = new URL(
document.baseURI
);

// Lazy load the configs because it breaks initial page loads otherwise
async function getCorePlugins() {
const dashboardCorePlugins = await import(
'@deephaven/dashboard-core-plugins'
);
const {
GridPluginConfig,
PandasPluginConfig,
ChartPluginConfig,
ChartBuilderPluginConfig,
FilterPluginConfig,
MarkdownPluginConfig,
LinkerPluginConfig,
} = dashboardCorePlugins;
return [
GridPluginConfig,
PandasPluginConfig,
ChartPluginConfig,
ChartBuilderPluginConfig,
FilterPluginConfig,
MarkdownPluginConfig,
LinkerPluginConfig,
];
}

ReactDOM.render(
<ApiBootstrap apiUrl={apiURL.href} setGlobally>
<Suspense fallback={<LoadingOverlay />}>
<AppBootstrap serverUrl={apiURL.origin} pluginsUrl={pluginsURL.href}>
<AppBootstrap
getCorePlugins={getCorePlugins}
serverUrl={apiURL.origin}
pluginsUrl={pluginsURL.href}
>
<AppRoot />
</AppBootstrap>
</Suspense>
Expand Down
Loading

0 comments on commit 1442ace

Please sign in to comment.