Skip to content

Commit

Permalink
refactor: move app mode provider to auth react
Browse files Browse the repository at this point in the history
Signed-off-by: Camila Belo <camilaibs@gmail.com>
  • Loading branch information
camilaibs committed Mar 21, 2024
1 parent 2abd125 commit 0224cfe
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 20 deletions.
3 changes: 1 addition & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@
"@types/react": "*",
"@types/react-dom": "*",
"@types/zen-observable": "^0.8.0",
"cross-env": "^7.0.0",
"msw": "^1.0.0"
"cross-env": "^7.0.0"
},
"bundled": true
}
6 changes: 3 additions & 3 deletions packages/app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ import { DevToolsPage } from '@backstage/plugin-devtools';
import { customDevToolsPage } from './components/devtools/CustomDevToolsPage';
import { CatalogUnprocessedEntitiesPage } from '@backstage/plugin-catalog-unprocessed-entities';
import { NotificationsPage } from '@backstage/plugin-notifications';
import { AppMode } from './AppMode';
import { ExperimentalAppProtection } from '@backstage/plugin-auth-react';

const app = createApp({
apis,
Expand Down Expand Up @@ -283,10 +283,10 @@ export default app.createRoot(
<AlertDisplay transientTimeoutMs={2500} />
<OAuthRequestDialog />
<AppRouter>
<AppMode>
<ExperimentalAppProtection>
<VisitListener />
<Root>{routes}</Root>
</AppMode>
</ExperimentalAppProtection>
</AppRouter>
</>,
);
5 changes: 5 additions & 0 deletions plugins/auth-react/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export type CookieAuthRefreshProviderProps = {
children: ReactNode;
};

// @public
export function ExperimentalAppProtection(props: {
children: ReactNode;
}): JSX.Element;

// @public
export function RedirectToRoot(): React_2.JSX.Element | null;

Expand Down
3 changes: 2 additions & 1 deletion plugins/auth-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"@backstage/test-utils": "workspace:^",
"@testing-library/jest-dom": "^6.0.0",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.0.0"
"@testing-library/user-event": "^14.0.0",
"msw": "^1.0.0"
},
"peerDependencies": {
"react": "^16.13.1 || ^17.0.0 || ^18.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import {
renderInTestApp,
setupRequestMockHandlers,
} from '@backstage/test-utils';
import { AppMode } from './AppMode';
import { ExperimentalAppProtection } from './ExperimentalAppProtection';
import {
configApiRef,
discoveryApiRef,
fetchApiRef,
} from '@backstage/core-plugin-api';

describe('AppMode', () => {
describe('ExperimentalAppProtection', () => {
const worker = setupServer();
setupRequestMockHandlers(worker);

Expand Down Expand Up @@ -62,7 +62,7 @@ describe('AppMode', () => {
[fetchApiRef, fetchApiMock],
]}
>
<AppMode>Test content</AppMode>
<ExperimentalAppProtection>Test content</ExperimentalAppProtection>
</TestApiProvider>,
);
expect(screen.getByTestId('progress')).toBeVisible();
Expand All @@ -79,7 +79,7 @@ describe('AppMode', () => {
[fetchApiRef, fetchApiMock],
]}
>
<AppMode>Test content</AppMode>
<ExperimentalAppProtection>Test content</ExperimentalAppProtection>
</TestApiProvider>,
);
await waitFor(() =>
Expand All @@ -98,7 +98,7 @@ describe('AppMode', () => {
[fetchApiRef, fetchApiMock],
]}
>
<AppMode>Test content</AppMode>
<ExperimentalAppProtection>Test content</ExperimentalAppProtection>
</TestApiProvider>,
);
await waitFor(() =>
Expand All @@ -109,7 +109,9 @@ describe('AppMode', () => {

it('should render the children also when the public index is available', async () => {
fetchApiMock.fetch.mockResolvedValueOnce({ ok: true });
await renderInTestApp(<AppMode>Test content</AppMode>);
await renderInTestApp(
<ExperimentalAppProtection>Test content</ExperimentalAppProtection>,
);
await waitFor(() =>
expect(screen.getByText('Test content')).toBeInTheDocument(),
);
Expand Down Expand Up @@ -138,7 +140,7 @@ describe('AppMode', () => {
[discoveryApiRef, discoveryApiMock],
]}
>
<AppMode>Test content</AppMode>
<ExperimentalAppProtection>Test content</ExperimentalAppProtection>
</TestApiProvider>,
);
await waitFor(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,41 @@
*/

import React, { ReactNode } from 'react';
import useAsync from 'react-use/lib/useAsync';
import {
configApiRef,
fetchApiRef,
useApi,
useApp,
} from '@backstage/core-plugin-api';
import { CookieAuthRefreshProvider } from '@backstage/plugin-auth-react';
import { useAsync, useMountEffect } from '@react-hookz/web';

export function AppMode(props: { children: ReactNode }): JSX.Element {
/**
* @public
* A provider that will protect the app when running in public experimental mode.
*/
export function ExperimentalAppProtection(props: {
children: ReactNode;
}): JSX.Element {
const { children } = props;
const fetchApi = useApi(fetchApiRef);
const configApi = useApi(configApiRef);
const Components = useApp().getComponents();

const { loading, error, value } = useAsync(async () => {
const [state, actions] = useAsync(async () => {
const baseUrl = configApi.getString('backend.baseUrl');
const response = await fetchApi.fetch(`${baseUrl}/public/index.html`);
return response.ok;
}, [fetchApi, configApi]);
});

useMountEffect(actions.execute);

if (loading) {
if (state.status === 'not-executed' || state.status === 'loading') {
return <Components.Progress />;
}

// Request failed, or the public index is not available
if (error || !value) {
if (state.status === 'error' || !state.result) {
return <>{children}</>;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/

export { ExperimentalAppProtection } from './ExperimentalAppProtection';
1 change: 1 addition & 0 deletions plugins/auth-react/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

export * from './RedirectToRoot';
export * from './CookieAuthRefreshProvider';
export * from './ExperimentalAppProtection';
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4960,6 +4960,7 @@ __metadata:
"@testing-library/react": ^14.0.0
"@testing-library/user-event": ^14.0.0
"@types/react": ^16.13.1 || ^17.0.0 || ^18.0.0
msw: ^1.0.0
peerDependencies:
react: ^16.13.1 || ^17.0.0 || ^18.0.0
languageName: unknown
Expand Down Expand Up @@ -27309,7 +27310,6 @@ __metadata:
"@vitejs/plugin-react": ^4.0.4
cross-env: ^7.0.0
history: ^5.0.0
msw: ^1.0.0
react: ^18.0.2
react-dom: ^18.0.2
react-router: ^6.3.0
Expand Down

0 comments on commit 0224cfe

Please sign in to comment.