1- import React , { ForwardRefExoticComponent } from 'react ' ;
1+ import Log from '@deephaven/log ' ;
22import {
3- AuthPlugin ,
4- AuthPluginComponent ,
3+ type PluginModule ,
4+ type AuthPlugin ,
5+ type AuthPluginComponent ,
56 isAuthPlugin ,
6- } from '@deephaven/auth-plugins' ;
7- import Log from '@deephaven/log' ;
8- import RemoteComponent from './RemoteComponent' ;
7+ LegacyAuthPlugin ,
8+ LegacyPlugin ,
9+ Plugin ,
10+ PluginType ,
11+ isLegacyAuthPlugin ,
12+ isLegacyPlugin ,
13+ } from '@deephaven/plugin' ;
914import loadRemoteModule from './loadRemoteModule' ;
1015
1116const log = Log . module ( '@deephaven/app-utils.PluginUtils' ) ;
1217
13- // A PluginModule. This interface should have new fields added to it from different levels of plugins.
14- // eslint-disable-next-line @typescript-eslint/no-empty-interface
15- export interface PluginModule { }
16-
1718export type PluginModuleMap = Map < string , PluginModule > ;
1819
1920export type PluginManifestPluginInfo = {
@@ -24,46 +25,14 @@ export type PluginManifestPluginInfo = {
2425
2526export type PluginManifest = { plugins : PluginManifestPluginInfo [ ] } ;
2627
27- /**
28- * Load a component plugin from the server.
29- * @param baseURL Base URL of the plugin server
30- * @param pluginName Name of the component plugin to load
31- * @returns A lazily loaded JSX.Element from the plugin
32- */
33- export function loadComponentPlugin (
34- baseURL : URL ,
35- pluginName : string
36- ) : ForwardRefExoticComponent < React . RefAttributes < unknown > > {
37- const pluginUrl = new URL ( `${ pluginName } .js` , baseURL ) ;
38- // eslint-disable-next-line @typescript-eslint/no-explicit-any
39- const Plugin : any = React . forwardRef ( ( props , ref ) => (
40- < RemoteComponent
41- url = { pluginUrl . href }
42- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43- render = { ( { err, Component } : { err : unknown ; Component : any } ) => {
44- if ( err != null && err !== '' ) {
45- const errorMessage = `Error loading plugin ${ pluginName } from ${ pluginUrl } due to ${ err } ` ;
46- log . error ( errorMessage ) ;
47- return < div className = "error-message" > { `${ errorMessage } ` } </ div > ;
48- }
49- // eslint-disable-next-line react/jsx-props-no-spreading
50- return < Component ref = { ref } { ...props } /> ;
51- } }
52- />
53- ) ) ;
54- Plugin . pluginName = pluginName ;
55- Plugin . displayName = 'Plugin' ;
56- return Plugin ;
57- }
58-
5928/**
6029 * Imports a commonjs plugin module from the provided URL
6130 * @param pluginUrl The URL of the plugin to load
6231 * @returns The loaded module
6332 */
6433export async function loadModulePlugin (
6534 pluginUrl : string
66- ) : Promise < PluginModule > {
35+ ) : Promise < LegacyPlugin | { default : Plugin } > {
6736 const myModule = await loadRemoteModule ( pluginUrl ) ;
6837 return myModule ;
6938}
@@ -86,7 +55,7 @@ export async function loadJson(jsonUrl: string): Promise<PluginManifest> {
8655}
8756
8857/**
89- * Load all plugin modules available.
58+ * Load all plugin modules available based on the manifest file at the provided base URL
9059 * @param modulePluginsUrl The base URL of the module plugins to load
9160 * @returns A map from the name of the plugin to the plugin module that was loaded
9261 */
@@ -102,7 +71,7 @@ export async function loadModulePlugins(
10271 }
10372
10473 log . debug ( 'Plugin manifest loaded:' , manifest ) ;
105- const pluginPromises : Promise < PluginModule > [ ] = [ ] ;
74+ const pluginPromises : Promise < LegacyPlugin | { default : Plugin } > [ ] = [ ] ;
10675 for ( let i = 0 ; i < manifest . plugins . length ; i += 1 ) {
10776 const { name, main } = manifest . plugins [ i ] ;
10877 const pluginMainUrl = `${ modulePluginsUrl } /${ name } /${ main } ` ;
@@ -115,7 +84,10 @@ export async function loadModulePlugins(
11584 const module = pluginModules [ i ] ;
11685 const { name } = manifest . plugins [ i ] ;
11786 if ( module . status === 'fulfilled' ) {
118- pluginMap . set ( name , module . value ) ;
87+ pluginMap . set (
88+ name ,
89+ isLegacyPlugin ( module . value ) ? module . value : module . value . default
90+ ) ;
11991 } else {
12092 log . error ( `Unable to load plugin ${ name } ` , module . reason ) ;
12193 }
@@ -147,28 +119,30 @@ export function getAuthHandlers(
147119export function getAuthPluginComponent (
148120 pluginMap : PluginModuleMap ,
149121 authConfigValues : Map < string , string > ,
150- corePlugins ?: Map < string , AuthPlugin >
122+ corePlugins = new Map < string , AuthPlugin | LegacyAuthPlugin > ( )
151123) : AuthPluginComponent {
152124 const authHandlers = getAuthHandlers ( authConfigValues ) ;
153- // Filter out all the plugins that are auth plugins, and then map them to [pluginName, AuthPlugin] pairs
154- // Uses some pretty disgusting casting, because TypeScript wants to treat it as an (string | AuthPlugin)[] array instead
125+ // User plugins take priority over core plugins
155126 const authPlugins = (
156- [ ...pluginMap . entries ( ) ] . filter (
157- ( [ , plugin ] : [ string , { AuthPlugin ?: AuthPlugin } ] ) =>
158- isAuthPlugin ( plugin . AuthPlugin )
159- ) as [ string , { AuthPlugin : AuthPlugin } ] [ ]
160- ) . map ( ( [ name , plugin ] ) => [ name , plugin . AuthPlugin ] ) as [
161- string ,
162- AuthPlugin ,
163- ] [ ] ;
164-
165- // Add all the core plugins in priority
166- authPlugins . push ( ...( corePlugins ?? [ ] ) ) ;
127+ [ ...pluginMap . entries ( ) , ...corePlugins . entries ( ) ] . filter (
128+ ( [ , plugin ] ) => isAuthPlugin ( plugin ) || isLegacyAuthPlugin ( plugin )
129+ ) as [ string , AuthPlugin | LegacyAuthPlugin ] [ ]
130+ ) . map ( ( [ name , plugin ] ) => {
131+ if ( isLegacyAuthPlugin ( plugin ) ) {
132+ return {
133+ type : PluginType . AUTH_PLUGIN ,
134+ name,
135+ component : plugin . AuthPlugin . Component ,
136+ isAvailable : plugin . AuthPlugin . isAvailable ,
137+ } ;
138+ }
167139
168- // Filter the available auth plugins
140+ return plugin ;
141+ } ) ;
169142
170- const availableAuthPlugins = authPlugins . filter ( ( [ name , authPlugin ] ) =>
171- authPlugin . isAvailable ( authHandlers , authConfigValues )
143+ // Filter the available auth plugins
144+ const availableAuthPlugins = authPlugins . filter ( ( { isAvailable } ) =>
145+ isAvailable ( authHandlers , authConfigValues )
172146 ) ;
173147
174148 if ( availableAuthPlugins . length === 0 ) {
@@ -178,12 +152,12 @@ export function getAuthPluginComponent(
178152 } else if ( availableAuthPlugins . length > 1 ) {
179153 log . warn (
180154 'More than one login plugin available, will use the first one: ' ,
181- availableAuthPlugins . map ( ( [ name ] ) => name ) . join ( ', ' )
155+ availableAuthPlugins . map ( ( { name } ) => name ) . join ( ', ' )
182156 ) ;
183157 }
184158
185- const [ loginPluginName , NewLoginPlugin ] = availableAuthPlugins [ 0 ] ;
186- log . info ( 'Using LoginPlugin' , loginPluginName ) ;
159+ const { name , component } = availableAuthPlugins [ 0 ] ;
160+ log . info ( 'Using LoginPlugin' , name ) ;
187161
188- return NewLoginPlugin . Component ;
162+ return component ;
189163}
0 commit comments