frontend-plugin-api: added PluginWrapperBlueprint + implementation
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -1868,6 +1868,59 @@ export interface PluginOptions<
|
||||
routes?: TRoutes;
|
||||
}
|
||||
|
||||
// @public
|
||||
export type PluginWrapperApi = {
|
||||
getPluginWrapper(pluginId: string):
|
||||
| ComponentType<{
|
||||
children: ReactNode;
|
||||
}>
|
||||
| undefined;
|
||||
};
|
||||
|
||||
// @public
|
||||
export const pluginWrapperApiRef: ApiRef<PluginWrapperApi>;
|
||||
|
||||
// @public
|
||||
export const PluginWrapperBlueprint: ExtensionBlueprint_2<{
|
||||
kind: 'plugin-wrapper';
|
||||
params: (params: {
|
||||
loader: () => Promise<{
|
||||
component: ComponentType<{
|
||||
children: ReactNode;
|
||||
}>;
|
||||
}>;
|
||||
}) => ExtensionBlueprintParams_2<{
|
||||
loader: () => Promise<{
|
||||
component: ComponentType<{
|
||||
children: ReactNode;
|
||||
}>;
|
||||
}>;
|
||||
}>;
|
||||
output: ExtensionDataRef_2<
|
||||
() => Promise<{
|
||||
component: ComponentType<{
|
||||
children: ReactNode;
|
||||
}>;
|
||||
}>,
|
||||
'core.plugin-wrapper.loader',
|
||||
{}
|
||||
>;
|
||||
inputs: {};
|
||||
config: {};
|
||||
configInput: {};
|
||||
dataRefs: {
|
||||
wrapper: ConfigurableExtensionDataRef_2<
|
||||
() => Promise<{
|
||||
component: ComponentType<{
|
||||
children: ReactNode;
|
||||
}>;
|
||||
}>,
|
||||
'core.plugin-wrapper.loader',
|
||||
{}
|
||||
>;
|
||||
};
|
||||
}>;
|
||||
|
||||
// @public (undocumented)
|
||||
export type PortableSchema<TOutput, TInput = TOutput> = {
|
||||
parse: (input: TInput) => TOutput;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ComponentType, ReactNode } from 'react';
|
||||
import { ApiRef, createApiRef } from '../system';
|
||||
|
||||
/**
|
||||
* The Plugin Wrapper API allows plugins to wrap their extensions with providers. This API is only intended for internal use by the Backstage frontend system. To provide contexts to plugin components, use `ExtensionBoundary` instead.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export type PluginWrapperApi = {
|
||||
/**
|
||||
* Returns a wrapper component for a specific plugin, or undefined if no wrappers exist. Do not use this API directly, instead use `ExtensionBoundary` to wrap your plugin components if needed.
|
||||
*/
|
||||
getPluginWrapper(
|
||||
pluginId: string,
|
||||
): ComponentType<{ children: ReactNode }> | undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* The API reference of {@link PluginWrapperApi}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const pluginWrapperApiRef: ApiRef<PluginWrapperApi> = createApiRef({
|
||||
id: 'core.plugin-wrapper',
|
||||
});
|
||||
@@ -49,3 +49,4 @@ export * from './RouteResolutionApi';
|
||||
export * from './StorageApi';
|
||||
export * from './AnalyticsApi';
|
||||
export * from './TranslationApi';
|
||||
export * from './PluginWrapperApi';
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ComponentType, ReactNode } from 'react';
|
||||
import {
|
||||
createExtensionBlueprint,
|
||||
createExtensionBlueprintParams,
|
||||
createExtensionDataRef,
|
||||
} from '../wiring';
|
||||
|
||||
const wrapperDataRef = createExtensionDataRef<
|
||||
() => Promise<{ component: ComponentType<{ children: ReactNode }> }>
|
||||
>().with({ id: 'core.plugin-wrapper.loader' });
|
||||
|
||||
/**
|
||||
* Creates extensions that wrap plugin extensions with providers.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const PluginWrapperBlueprint = createExtensionBlueprint({
|
||||
kind: 'plugin-wrapper',
|
||||
attachTo: { id: 'api:app/plugin-wrapper', input: 'wrappers' },
|
||||
output: [wrapperDataRef],
|
||||
dataRefs: {
|
||||
wrapper: wrapperDataRef,
|
||||
},
|
||||
defineParams(params: {
|
||||
loader: () => Promise<{
|
||||
component: ComponentType<{ children: ReactNode }>;
|
||||
}>;
|
||||
}) {
|
||||
return createExtensionBlueprintParams(
|
||||
params as {
|
||||
loader: () => Promise<{
|
||||
component: ComponentType<{ children: ReactNode }>;
|
||||
}>;
|
||||
},
|
||||
);
|
||||
},
|
||||
*factory(params) {
|
||||
yield wrapperDataRef(params.loader);
|
||||
},
|
||||
});
|
||||
@@ -21,6 +21,7 @@ export {
|
||||
export { ApiBlueprint } from './ApiBlueprint';
|
||||
export { AppRootElementBlueprint } from './AppRootElementBlueprint';
|
||||
export { AppRootWrapperBlueprint } from './AppRootWrapperBlueprint';
|
||||
export { PluginWrapperBlueprint } from './PluginWrapperBlueprint';
|
||||
export { IconBundleBlueprint } from './IconBundleBlueprint';
|
||||
export {
|
||||
NavContentBlueprint,
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
ReactNode,
|
||||
Suspense,
|
||||
useEffect,
|
||||
useMemo,
|
||||
lazy as reactLazy,
|
||||
} from 'react';
|
||||
import { AnalyticsContext, useAnalytics } from '../analytics';
|
||||
@@ -27,6 +28,10 @@ import { ErrorApiBoundary } from './ErrorApiBoundary';
|
||||
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
|
||||
import { routableExtensionRenderedEvent } from '../../../core-plugin-api/src/analytics/Tracker';
|
||||
import { AppNode, ErrorApi, errorApiRef, useApi } from '../apis';
|
||||
import {
|
||||
PluginWrapperApi,
|
||||
pluginWrapperApiRef,
|
||||
} from '../apis/definitions/PluginWrapperApi';
|
||||
import { coreExtensionData } from '../wiring';
|
||||
import { AppNodeProvider } from './AppNodeProvider';
|
||||
import { Progress } from './DefaultSwappableComponents';
|
||||
@@ -39,6 +44,13 @@ function useOptionalErrorApi(): ErrorApi | undefined {
|
||||
}
|
||||
}
|
||||
|
||||
function useOptionalPluginWrapperApi(): PluginWrapperApi | undefined {
|
||||
try {
|
||||
return useApi(pluginWrapperApiRef);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
type RouteTrackerProps = PropsWithChildren<{
|
||||
enabled?: boolean;
|
||||
}>;
|
||||
@@ -78,11 +90,18 @@ export function ExtensionBoundary(props: ExtensionBoundaryProps) {
|
||||
);
|
||||
|
||||
const plugin = node.spec.plugin;
|
||||
const pluginId = plugin.id ?? 'app';
|
||||
|
||||
const pluginWrapperApi = useOptionalPluginWrapperApi();
|
||||
|
||||
const PluginWrapper = useMemo(() => {
|
||||
return pluginWrapperApi?.getPluginWrapper(pluginId);
|
||||
}, [pluginWrapperApi, pluginId]);
|
||||
|
||||
// Skipping "routeRef" attribute in the new system, the extension "id" should provide more insight
|
||||
const attributes = {
|
||||
extensionId: node.spec.id,
|
||||
pluginId: plugin.id ?? 'app',
|
||||
pluginId,
|
||||
};
|
||||
|
||||
let content = (
|
||||
@@ -91,6 +110,10 @@ export function ExtensionBoundary(props: ExtensionBoundaryProps) {
|
||||
</AnalyticsContext>
|
||||
);
|
||||
|
||||
if (PluginWrapper) {
|
||||
content = <PluginWrapper>{content}</PluginWrapper>;
|
||||
}
|
||||
|
||||
if (props.errorPresentation === 'error-api') {
|
||||
content = (
|
||||
<ErrorApiBoundary node={node} errorApi={errorApi}>
|
||||
|
||||
Reference in New Issue
Block a user