|
| 1 | +import React, {Component, FC, ReactNode} from 'react'; |
| 2 | +import * as plugins from '../modules/plugins'; |
| 3 | +import {PLUGIN_COMPONENT_POSITIONS, PluginComponentPosition, PluginDescription} from '@/types'; |
| 4 | +import ErrorBoundary from './error-boundary'; |
| 5 | + |
| 6 | +interface ExtensionPointProps { |
| 7 | + name: string; |
| 8 | + children?: React.ReactNode; |
| 9 | +} |
| 10 | + |
| 11 | +interface ExtensionPointComponent { |
| 12 | + PluginComponent: FC; |
| 13 | + name: string; |
| 14 | + point: string; |
| 15 | + position: PluginComponentPosition; |
| 16 | + config: PluginDescription; |
| 17 | +} |
| 18 | + |
| 19 | +type ExtensionPointComponentUnchecked = |
| 20 | + Omit<ExtensionPointComponent, 'point' | 'position'> |
| 21 | + & Partial<Pick<ExtensionPointComponent, 'point' | 'position'>> |
| 22 | + |
| 23 | +export default class ExtensionPoint<T extends ExtensionPointProps> extends Component<T> { |
| 24 | + render(): ReactNode { |
| 25 | + const loadedPluginConfigs = plugins.getLoadedConfigs(); |
| 26 | + |
| 27 | + if (loadedPluginConfigs.length) { |
| 28 | + const {name: pointName, children: reportComponent, ...componentProps} = this.props; |
| 29 | + const pluginComponents = getExtensionPointComponents(loadedPluginConfigs, pointName); |
| 30 | + return getComponentsComposition(pluginComponents, reportComponent, componentProps); |
| 31 | + } |
| 32 | + |
| 33 | + return this.props.children; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function getComponentsComposition(pluginComponents: ExtensionPointComponent[], reportComponent: ReactNode, componentProps: any): ReactNode { |
| 38 | + let currentComponent = reportComponent; |
| 39 | + |
| 40 | + for (const {PluginComponent, position, config} of pluginComponents) { |
| 41 | + currentComponent = composeComponents(PluginComponent, componentProps, currentComponent, position, config); |
| 42 | + } |
| 43 | + |
| 44 | + return currentComponent; |
| 45 | +} |
| 46 | + |
| 47 | +function composeComponents(PluginComponent: FC, pluginProps: any, currentComponent: ReactNode, position: PluginComponentPosition, config: PluginDescription): ReactNode { |
| 48 | + switch (position) { |
| 49 | + case 'wrap': |
| 50 | + return <ErrorBoundary fallback={currentComponent}> |
| 51 | + <PluginComponent {...pluginProps}> |
| 52 | + {currentComponent} |
| 53 | + </PluginComponent> |
| 54 | + </ErrorBoundary>; |
| 55 | + case 'before': |
| 56 | + return <> |
| 57 | + <ErrorBoundary> |
| 58 | + <PluginComponent {...pluginProps}/> |
| 59 | + </ErrorBoundary> |
| 60 | + {currentComponent} |
| 61 | + </>; |
| 62 | + case 'after': |
| 63 | + return <> |
| 64 | + {currentComponent} |
| 65 | + <ErrorBoundary> |
| 66 | + <PluginComponent {...pluginProps}/> |
| 67 | + </ErrorBoundary> |
| 68 | + </>; |
| 69 | + default: |
| 70 | + console.error(`${getComponentSpec(config)} unexpected position "${position}" specified.`); |
| 71 | + return currentComponent; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function getExtensionPointComponents(loadedPluginConfigs: PluginDescription[], pointName: string): ExtensionPointComponent[] { |
| 76 | + return loadedPluginConfigs |
| 77 | + .map<ExtensionPointComponentUnchecked>(pluginDescription => { |
| 78 | + try { |
| 79 | + const PluginComponent = plugins.getPluginField<FC>(pluginDescription.name, pluginDescription.component); |
| 80 | + |
| 81 | + return { |
| 82 | + PluginComponent, |
| 83 | + name: pluginDescription.name, |
| 84 | + point: getComponentPoint(PluginComponent, pluginDescription), |
| 85 | + position: getComponentPosition(PluginComponent, pluginDescription), |
| 86 | + config: pluginDescription |
| 87 | + }; |
| 88 | + } catch (err) { |
| 89 | + console.error(err); |
| 90 | + return {} as ExtensionPointComponentUnchecked; |
| 91 | + } |
| 92 | + }) |
| 93 | + .filter((component: ExtensionPointComponentUnchecked): component is ExtensionPointComponent => { |
| 94 | + return Boolean(component.point && component.position && component.point === pointName); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +function getComponentPoint(component: FC, config: PluginDescription): string | undefined { |
| 99 | + const result = getComponentConfigField(component, config, 'point'); |
| 100 | + |
| 101 | + if (typeof result !== 'string') { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + return result as string; |
| 106 | +} |
| 107 | + |
| 108 | +function getComponentPosition(component: FC, config: PluginDescription): PluginComponentPosition | undefined { |
| 109 | + const result = getComponentConfigField(component, config, 'position'); |
| 110 | + |
| 111 | + if (typeof result !== 'string') { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (!PLUGIN_COMPONENT_POSITIONS.includes(result as PluginComponentPosition)) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + return result as PluginComponentPosition; |
| 120 | +} |
| 121 | + |
| 122 | +function getComponentConfigField(component: any, config: any, field: string): unknown | null { |
| 123 | + if (component[field] && config[field] && component[field] !== config[field]) { |
| 124 | + console.error(`${getComponentSpec(config)} "${field}" field does not match the one from the config: "${component[field]}" vs "${config[field]}".`); |
| 125 | + return null; |
| 126 | + } else if (!component[field] && !config[field]) { |
| 127 | + console.error(`${getComponentSpec(config)} "${field}" field is not set.`); |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + return component[field] || config[field]; |
| 132 | +} |
| 133 | + |
| 134 | +function getComponentSpec(pluginDescription: PluginDescription): string { |
| 135 | + return `Component "${pluginDescription.component}" of "${pluginDescription.name}" plugin`; |
| 136 | +} |
0 commit comments