refactor: extract techdocs auth to a new file

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-03-12 10:39:10 +01:00
parent 125d635b13
commit 07b79f44aa
2 changed files with 104 additions and 74 deletions
@@ -0,0 +1,101 @@
/*
* 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';
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 (
<ErrorPanel error={error}>
<Button variant="outlined" onClick={retry}>
Retry
</Button>
</ErrorPanel>
);
}
if (loading) {
return <Progress />;
}
return children;
}
@@ -14,23 +14,15 @@
* limitations under the License.
*/
import React, {
ReactNode,
Children,
ReactElement,
useEffect,
useState,
useCallback,
} from 'react';
import React, { ReactNode, Children, ReactElement } from 'react';
import { useOutlet } from 'react-router-dom';
import { ErrorPanel, Page } from '@backstage/core-components';
import { Page } from '@backstage/core-components';
import { CompoundEntityRef } from '@backstage/catalog-model';
import {
TECHDOCS_ADDONS_WRAPPER_KEY,
TECHDOCS_ADDONS_KEY,
TechDocsReaderPageProvider,
techdocsApiRef,
} from '@backstage/plugin-techdocs-react';
import { TechDocsReaderPageRenderFunction } from '../../../types';
@@ -41,12 +33,9 @@ import { TechDocsReaderPageSubheader } from '../TechDocsReaderPageSubheader';
import { rootDocsRouteRef } from '../../../routes';
import {
getComponentData,
useApi,
useApp,
useRouteRefParams,
} from '@backstage/core-plugin-api';
import useAsyncRetry from 'react-use/lib/useAsyncRetry';
import { Button } from '@material-ui/core';
import { TechDocsAuthProvider } from './TechDocsAuthProvider';
/* An explanation for the multiple ways of customizing the TechDocs reader page
@@ -155,66 +144,6 @@ export const TechDocsReaderLayout = (props: TechDocsReaderLayoutProps) => {
);
};
function TechDocsAuthProvider({ children }: { children: ReactNode }) {
const app = useApp();
const { Progress } = app.getComponents();
const [channel] = useState(new BroadcastChannel('techdocs-cookie-refresh'));
const techdocsApi = useApi(techdocsApiRef);
const { loading, error, value, retry } = useAsyncRetry(async () => {
return await techdocsApi.getCookie();
}, [techdocsApi]);
const startCookieRefresh = useCallback(
(expiresAt: string) => {
// Randomize the refreshing margin to avoid all tabs refreshing at the same time
const refreshingMargin = (1 + 3 * Math.random()) * 60_000;
const delay = Date.parse(expiresAt) - Date.now() - refreshingMargin;
const timeout = setTimeout(retry, delay);
return () => clearTimeout(timeout);
},
[retry],
);
useEffect(() => {
if (!value) return () => {};
channel.postMessage({ action: 'COOKIE_REFRESHED', payload: value });
let stopCookieRefresh = startCookieRefresh(value.expiresAt);
const handleMessage = (
event: MessageEvent<{ action: string; payload: { expiresAt: string } }>,
): void => {
const { action, payload } = event.data;
if (action === 'COOKIE_REFRESHED') {
stopCookieRefresh();
stopCookieRefresh = startCookieRefresh(payload.expiresAt);
}
};
channel.addEventListener('message', handleMessage);
return () => {
stopCookieRefresh();
channel.removeEventListener('message', handleMessage);
};
}, [value, channel, startCookieRefresh]);
if (error) {
return (
<ErrorPanel error={error}>
<Button variant="outlined" onClick={retry}>
Retry
</Button>
</ErrorPanel>
);
}
if (loading) {
return <Progress />;
}
return children;
}
/**
* @public
*/