frontend-plugin-api: removed new useRouteRef
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@backstage/frontend-plugin-api': minor
|
||||
'@backstage/frontend-app-api': minor
|
||||
---
|
||||
|
||||
Removed support for the new `useRouteRef`.
|
||||
@@ -19,9 +19,8 @@ import { Link } from '@backstage/core-components';
|
||||
import {
|
||||
createPageExtension,
|
||||
createPlugin,
|
||||
useRouteRef,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { createRouteRef } from '@backstage/core-plugin-api';
|
||||
import { useRouteRef, createRouteRef } from '@backstage/core-plugin-api';
|
||||
import { Route, Routes } from 'react-router-dom';
|
||||
|
||||
const indexRouteRef = createRouteRef({ id: 'index' });
|
||||
|
||||
@@ -19,9 +19,9 @@ import {
|
||||
createExtension,
|
||||
coreExtensionData,
|
||||
createExtensionInput,
|
||||
useRouteRef,
|
||||
NavTarget,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { useRouteRef } from '@backstage/core-plugin-api';
|
||||
import { makeStyles } from '@material-ui/core';
|
||||
import {
|
||||
Sidebar,
|
||||
@@ -66,7 +66,7 @@ const SidebarLogo = () => {
|
||||
|
||||
const SidebarNavItem = (props: NavTarget) => {
|
||||
const { icon: Icon, title, routeRef } = props;
|
||||
const to = useRouteRef(routeRef)();
|
||||
const to = useRouteRef(routeRef)({});
|
||||
// TODO: Support opening modal, for example, the search one
|
||||
return <SidebarItem to={to} icon={Icon} text={title} />;
|
||||
};
|
||||
|
||||
@@ -1,52 +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 { RouteRef } from '@backstage/core-plugin-api';
|
||||
import React, { createContext, ReactNode } from 'react';
|
||||
|
||||
export interface RoutingContextType {
|
||||
resolve(
|
||||
routeRef: RouteRef,
|
||||
options: { pathname: string },
|
||||
): (() => string) | undefined;
|
||||
}
|
||||
|
||||
export const RoutingContext = createContext<RoutingContextType>({
|
||||
resolve: () => () => '',
|
||||
});
|
||||
|
||||
export class RouteResolver {
|
||||
constructor(private readonly routePaths: Map<RouteRef, string>) {}
|
||||
|
||||
resolve(anyRouteRef: RouteRef<{}>): (() => string) | undefined {
|
||||
const basePath = this.routePaths.get(anyRouteRef);
|
||||
if (!basePath) {
|
||||
return undefined;
|
||||
}
|
||||
return () => basePath;
|
||||
}
|
||||
}
|
||||
|
||||
export function RoutingProvider(props: {
|
||||
routePaths: Map<RouteRef, string>;
|
||||
children?: ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<RoutingContext.Provider value={new RouteResolver(props.routePaths)}>
|
||||
{props.children}
|
||||
</RoutingContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -34,7 +34,6 @@ import {
|
||||
mergeExtensionParameters,
|
||||
readAppExtensionParameters,
|
||||
} from './parameters';
|
||||
import { RoutingProvider } from '../routing/RoutingContext';
|
||||
import {
|
||||
AnyApiFactory,
|
||||
ApiHolder,
|
||||
@@ -74,7 +73,7 @@ import { defaultConfigLoaderSync } from '../../../core-app-api/src/app/defaultCo
|
||||
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
|
||||
import { overrideBaseUrlConfigs } from '../../../core-app-api/src/app/overrideBaseUrlConfigs';
|
||||
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
|
||||
import { RoutingProvider as LegacyRoutingProvider } from '../../../core-app-api/src/routing/RoutingProvider';
|
||||
import { RoutingProvider } from '../../../core-app-api/src/routing/RoutingProvider';
|
||||
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
|
||||
import {
|
||||
apis as defaultApis,
|
||||
@@ -308,15 +307,10 @@ export function createApp(options: {
|
||||
<ApiProvider apis={apiHolder}>
|
||||
<AppContextProvider appContext={appContext}>
|
||||
<AppThemeProvider>
|
||||
<LegacyRoutingProvider
|
||||
{...routeInfo}
|
||||
routeBindings={new Map(/* TODO */)}
|
||||
>
|
||||
<RoutingProvider routePaths={routeInfo.routePaths}>
|
||||
{/* TODO: set base path using the logic from AppRouter */}
|
||||
<BrowserRouter>{rootElements}</BrowserRouter>
|
||||
</RoutingProvider>
|
||||
</LegacyRoutingProvider>
|
||||
<RoutingProvider {...routeInfo} routeBindings={new Map(/* TODO */)}>
|
||||
{/* TODO: set base path using the logic from AppRouter */}
|
||||
<BrowserRouter>{rootElements}</BrowserRouter>
|
||||
</RoutingProvider>
|
||||
</AppThemeProvider>
|
||||
</AppContextProvider>
|
||||
</ApiProvider>
|
||||
|
||||
@@ -333,7 +333,4 @@ export type PortableSchema<TOutput> = {
|
||||
parse: (input: unknown) => TOutput;
|
||||
schema: JsonObject;
|
||||
};
|
||||
|
||||
// @public (undocumented)
|
||||
export function useRouteRef(routeRef: RouteRef<any>): () => string;
|
||||
```
|
||||
|
||||
@@ -24,4 +24,3 @@ export * from './components';
|
||||
export * from './extensions';
|
||||
export * from './schema';
|
||||
export * from './wiring';
|
||||
export * from './routing';
|
||||
|
||||
@@ -1,17 +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.
|
||||
*/
|
||||
|
||||
export { useRouteRef } from './useRouteRef';
|
||||
@@ -1,38 +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 { RouteRef } from '@backstage/core-plugin-api';
|
||||
// eslint-disable-next-line @backstage/no-forbidden-package-imports
|
||||
import { RoutingContext } from '@backstage/frontend-app-api/src/routing/RoutingContext';
|
||||
import { useContext, useMemo } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
|
||||
/** @public */
|
||||
export function useRouteRef(routeRef: RouteRef<any>): () => string {
|
||||
const { pathname } = useLocation();
|
||||
const resolver = useContext(RoutingContext);
|
||||
|
||||
const routeFunc = useMemo(
|
||||
() => resolver && resolver.resolve(routeRef, { pathname }),
|
||||
[resolver, routeRef, pathname],
|
||||
);
|
||||
|
||||
if (!routeFunc) {
|
||||
throw new Error(`Failed to resolve routeRef ${routeRef}`);
|
||||
}
|
||||
|
||||
return routeFunc;
|
||||
}
|
||||
Reference in New Issue
Block a user