refactor: replace components context in app
Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import { AnyApiRef } from '@backstage/core-plugin-api';
|
||||
import { ApiFactory } from '@backstage/core-plugin-api';
|
||||
import { ApiHolder } from '@backstage/core-plugin-api';
|
||||
import { ApiRef } from '@backstage/core-plugin-api';
|
||||
import { ApiRef as ApiRef_2 } from '@backstage/core-plugin-api/src/apis/system/types';
|
||||
import { ApiRefConfig } from '@backstage/core-plugin-api';
|
||||
import { AppTheme } from '@backstage/core-plugin-api';
|
||||
import { AppThemeApi } from '@backstage/core-plugin-api';
|
||||
@@ -26,7 +27,6 @@ import { BackstagePlugin as BackstagePlugin_2 } from '@backstage/core-plugin-api
|
||||
import { BackstageUserIdentity } from '@backstage/core-plugin-api';
|
||||
import { bitbucketAuthApiRef } from '@backstage/core-plugin-api';
|
||||
import { bitbucketServerAuthApiRef } from '@backstage/core-plugin-api';
|
||||
import { ComponentRef as ComponentRef_2 } from '@backstage/frontend-plugin-api';
|
||||
import { ComponentType } from 'react';
|
||||
import { ConfigApi } from '@backstage/core-plugin-api';
|
||||
import { configApiRef } from '@backstage/core-plugin-api';
|
||||
@@ -293,15 +293,14 @@ export type ComponentRef<T> = {
|
||||
T: T;
|
||||
};
|
||||
|
||||
// @public (undocumented)
|
||||
export type ComponentsContextValue = Record<string, ComponentType<any>>;
|
||||
// @public
|
||||
export interface ComponentsApi {
|
||||
// (undocumented)
|
||||
getComponent<T>(ref: ComponentRef<T>): T;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export function ComponentsProvider(
|
||||
props: PropsWithChildren<{
|
||||
value: ComponentsContextValue;
|
||||
}>,
|
||||
): React_2.JSX.Element;
|
||||
// @public
|
||||
export const componentsApiRef: ApiRef_2<ComponentsApi>;
|
||||
|
||||
export { ConfigApi };
|
||||
|
||||
@@ -910,12 +909,6 @@ export { useApi };
|
||||
|
||||
export { useApiHolder };
|
||||
|
||||
// @public (undocumented)
|
||||
export function useComponent<
|
||||
P extends {},
|
||||
T extends ComponentRef_2<ComponentType<P>>,
|
||||
>(ref: T): T['T'];
|
||||
|
||||
// @public
|
||||
export function useRouteRef<
|
||||
TOptional extends boolean,
|
||||
|
||||
@@ -24,7 +24,7 @@ import { createApiRef } from '../system';
|
||||
*/
|
||||
export interface ComponentsApi {
|
||||
// TODO: Should component refs also provide the default implementation so that we're guaranteed to get a component?
|
||||
getComponent<T>(ref: ComponentRef<T>): T | undefined;
|
||||
getComponent<T>(ref: ComponentRef<T>): T;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 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 { ComponentRef } from '@backstage/frontend-plugin-api';
|
||||
import {
|
||||
createVersionedContext,
|
||||
createVersionedValueMap,
|
||||
useVersionedContext,
|
||||
} from '@backstage/version-bridge';
|
||||
import React from 'react';
|
||||
import { ComponentType, PropsWithChildren } from 'react';
|
||||
|
||||
/** @public */
|
||||
export type ComponentsContextValue = Record<string, ComponentType<any>>;
|
||||
|
||||
const ComponentsContext = createVersionedContext<{ 1: ComponentsContextValue }>(
|
||||
'components-context',
|
||||
);
|
||||
|
||||
/** @public */
|
||||
export function ComponentsProvider(
|
||||
props: PropsWithChildren<{ value: ComponentsContextValue }>,
|
||||
) {
|
||||
const { value, children } = props;
|
||||
return (
|
||||
<ComponentsContext.Provider value={createVersionedValueMap({ 1: value })}>
|
||||
{children}
|
||||
</ComponentsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export function useComponent<
|
||||
P extends {},
|
||||
T extends ComponentRef<ComponentType<P>>,
|
||||
>(ref: T): T['T'] {
|
||||
const components = useVersionedContext<{ 1: ComponentsContextValue }>(
|
||||
'components-context',
|
||||
);
|
||||
const component = components?.atVersion(1)?.[ref.id];
|
||||
if (!component) {
|
||||
throw new Error(`No implementation available for ${ref.id}`);
|
||||
}
|
||||
return component;
|
||||
}
|
||||
@@ -14,12 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export {
|
||||
ComponentsProvider,
|
||||
useComponent,
|
||||
type ComponentsContextValue,
|
||||
} from './ComponentsContext';
|
||||
|
||||
export {
|
||||
coreProgressComponentRef,
|
||||
coreBootErrorPageComponentRef,
|
||||
|
||||
Reference in New Issue
Block a user