From a992f863848271255f19b7105ed15ddde2c56c9a Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Tue, 12 Mar 2024 13:58:41 +0100 Subject: [PATCH] refactor: use generic auth cookie provider in techdocs Signed-off-by: Camila Belo --- plugins/techdocs/package.json | 1 + .../TechDocsAuthProvider.tsx | 103 ------------------ .../TechDocsReaderPage/TechDocsReaderPage.tsx | 12 +- 3 files changed, 8 insertions(+), 108 deletions(-) delete mode 100644 plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsAuthProvider.tsx diff --git a/plugins/techdocs/package.json b/plugins/techdocs/package.json index 533736c3bc..a51eb5f723 100644 --- a/plugins/techdocs/package.json +++ b/plugins/techdocs/package.json @@ -60,6 +60,7 @@ "@backstage/frontend-plugin-api": "workspace:^", "@backstage/integration": "workspace:^", "@backstage/integration-react": "workspace:^", + "@backstage/plugin-auth-react": "workspace:^", "@backstage/plugin-catalog-react": "workspace:^", "@backstage/plugin-search-common": "workspace:^", "@backstage/plugin-search-react": "workspace:^", diff --git a/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsAuthProvider.tsx b/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsAuthProvider.tsx deleted file mode 100644 index baeb809a6c..0000000000 --- a/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsAuthProvider.tsx +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 React, { ReactNode, useEffect, useState, useCallback } from 'react'; -import { ErrorPanel } from '@backstage/core-components'; -import { techdocsApiRef } from '@backstage/plugin-techdocs-react'; -import { useApi, useApp } from '@backstage/core-plugin-api'; -import useAsyncRetry from 'react-use/lib/useAsyncRetry'; -import Button from '@material-ui/core/Button'; - -type TechDocsRefreshCookieMessage = MessageEvent<{ - action: string; - payload: { - expiresAt: string; - }; -}>; - -function useTechDocsCookie() { - const techdocsApi = useApi(techdocsApiRef); - - const { retry, ...state } = useAsyncRetry(async () => { - return await techdocsApi.getCookie(); - }, [techdocsApi]); - - const refresh = useCallback( - (expiresAt: string) => { - // Randomize the refreshing margin to avoid all tabs refreshing at the same time - const refreshingMargin = (1 + 3 * Math.random()) * 60000; - const delay = Date.parse(expiresAt) - Date.now() - refreshingMargin; - const timeout = setTimeout(retry, delay); - return () => clearTimeout(timeout); - }, - [retry], - ); - - return { ...state, retry, refresh }; -} - -export function TechDocsAuthProvider({ children }: { children: ReactNode }) { - const app = useApp(); - const { Progress } = app.getComponents(); - - const [channel] = useState( - () => new BroadcastChannel('techdocs-cookie-refresh'), - ); - - const { loading, error, value, retry, refresh } = useTechDocsCookie(); - - useEffect(() => { - if (!value) return () => {}; - - channel.postMessage({ - action: 'TECHDOCS_COOKIE_REFRESHED', - payload: value, - }); - - let cancel = refresh(value.expiresAt); - - const handleMessage = (event: TechDocsRefreshCookieMessage): void => { - const { action, payload } = event.data; - if (action === 'TECHDOCS_COOKIE_REFRESHED') { - cancel(); - cancel = refresh(payload.expiresAt); - } - }; - - channel.addEventListener('message', handleMessage); - - return () => { - cancel(); - channel.removeEventListener('message', handleMessage); - }; - }, [value, refresh, channel]); - - if (error) { - return ( - - - - ); - } - - if (loading) { - return ; - } - - return children; -} diff --git a/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx b/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx index d7f3843ebb..2a366b7da4 100644 --- a/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx @@ -23,6 +23,7 @@ import { TECHDOCS_ADDONS_WRAPPER_KEY, TECHDOCS_ADDONS_KEY, TechDocsReaderPageProvider, + techdocsApiRef, } from '@backstage/plugin-techdocs-react'; import { TechDocsReaderPageRenderFunction } from '../../../types'; @@ -35,7 +36,8 @@ import { getComponentData, useRouteRefParams, } from '@backstage/core-plugin-api'; -import { TechDocsAuthProvider } from './TechDocsAuthProvider'; + +import { CookieAuthRefreshProvider } from '@backstage/plugin-auth-react'; /* An explanation for the multiple ways of customizing the TechDocs reader page @@ -178,17 +180,17 @@ export const TechDocsReaderPage = (props: TechDocsReaderPageProps) => { // As explained above, "page" is configuration 4 and is 1 return ( - + {(page as JSX.Element) || } - + ); } // As explained above, a render function is configuration 3 and React element is 2 return ( - + {({ metadata, entityMetadata, onReady }) => (
@@ -205,6 +207,6 @@ export const TechDocsReaderPage = (props: TechDocsReaderPageProps) => {
)}
-
+ ); };