From 4461d87d5a4859d12387cc794e4ea1dfa91721d4 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 6 Oct 2023 16:43:24 +0200 Subject: [PATCH] frontend-plugin-api: removed new useRouteRef Signed-off-by: Patrik Oldsberg --- .changeset/empty-schools-check.md | 6 +++ .../app-next/src/examples/pagesPlugin.tsx | 3 +- .../src/extensions/CoreNav.tsx | 4 +- .../src/routing/RoutingContext.tsx | 52 ------------------- .../frontend-app-api/src/wiring/createApp.tsx | 16 ++---- packages/frontend-plugin-api/api-report.md | 3 -- packages/frontend-plugin-api/src/index.ts | 1 - .../frontend-plugin-api/src/routing/index.ts | 17 ------ .../src/routing/useRouteRef.ts | 38 -------------- 9 files changed, 14 insertions(+), 126 deletions(-) create mode 100644 .changeset/empty-schools-check.md delete mode 100644 packages/frontend-app-api/src/routing/RoutingContext.tsx delete mode 100644 packages/frontend-plugin-api/src/routing/index.ts delete mode 100644 packages/frontend-plugin-api/src/routing/useRouteRef.ts diff --git a/.changeset/empty-schools-check.md b/.changeset/empty-schools-check.md new file mode 100644 index 0000000000..c69bbaabb3 --- /dev/null +++ b/.changeset/empty-schools-check.md @@ -0,0 +1,6 @@ +--- +'@backstage/frontend-plugin-api': minor +'@backstage/frontend-app-api': minor +--- + +Removed support for the new `useRouteRef`. diff --git a/packages/app-next/src/examples/pagesPlugin.tsx b/packages/app-next/src/examples/pagesPlugin.tsx index 290c6b118e..1ec1cfa5a1 100644 --- a/packages/app-next/src/examples/pagesPlugin.tsx +++ b/packages/app-next/src/examples/pagesPlugin.tsx @@ -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' }); diff --git a/packages/frontend-app-api/src/extensions/CoreNav.tsx b/packages/frontend-app-api/src/extensions/CoreNav.tsx index 8e8aa638c0..3406c1d637 100644 --- a/packages/frontend-app-api/src/extensions/CoreNav.tsx +++ b/packages/frontend-app-api/src/extensions/CoreNav.tsx @@ -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 ; }; diff --git a/packages/frontend-app-api/src/routing/RoutingContext.tsx b/packages/frontend-app-api/src/routing/RoutingContext.tsx deleted file mode 100644 index 237c8aa5df..0000000000 --- a/packages/frontend-app-api/src/routing/RoutingContext.tsx +++ /dev/null @@ -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({ - resolve: () => () => '', -}); - -export class RouteResolver { - constructor(private readonly routePaths: Map) {} - - resolve(anyRouteRef: RouteRef<{}>): (() => string) | undefined { - const basePath = this.routePaths.get(anyRouteRef); - if (!basePath) { - return undefined; - } - return () => basePath; - } -} - -export function RoutingProvider(props: { - routePaths: Map; - children?: ReactNode; -}) { - return ( - - {props.children} - - ); -} diff --git a/packages/frontend-app-api/src/wiring/createApp.tsx b/packages/frontend-app-api/src/wiring/createApp.tsx index e40c943e23..02f3d17d7c 100644 --- a/packages/frontend-app-api/src/wiring/createApp.tsx +++ b/packages/frontend-app-api/src/wiring/createApp.tsx @@ -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: { - - - {/* TODO: set base path using the logic from AppRouter */} - {rootElements} - - + + {/* TODO: set base path using the logic from AppRouter */} + {rootElements} + diff --git a/packages/frontend-plugin-api/api-report.md b/packages/frontend-plugin-api/api-report.md index f6781be290..18baf88e5d 100644 --- a/packages/frontend-plugin-api/api-report.md +++ b/packages/frontend-plugin-api/api-report.md @@ -333,7 +333,4 @@ export type PortableSchema = { parse: (input: unknown) => TOutput; schema: JsonObject; }; - -// @public (undocumented) -export function useRouteRef(routeRef: RouteRef): () => string; ``` diff --git a/packages/frontend-plugin-api/src/index.ts b/packages/frontend-plugin-api/src/index.ts index 4e1a383899..512723b5d4 100644 --- a/packages/frontend-plugin-api/src/index.ts +++ b/packages/frontend-plugin-api/src/index.ts @@ -24,4 +24,3 @@ export * from './components'; export * from './extensions'; export * from './schema'; export * from './wiring'; -export * from './routing'; diff --git a/packages/frontend-plugin-api/src/routing/index.ts b/packages/frontend-plugin-api/src/routing/index.ts deleted file mode 100644 index 4506538fa0..0000000000 --- a/packages/frontend-plugin-api/src/routing/index.ts +++ /dev/null @@ -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'; diff --git a/packages/frontend-plugin-api/src/routing/useRouteRef.ts b/packages/frontend-plugin-api/src/routing/useRouteRef.ts deleted file mode 100644 index ad1c16ea8e..0000000000 --- a/packages/frontend-plugin-api/src/routing/useRouteRef.ts +++ /dev/null @@ -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): () => 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; -}