From d32a911c438be1d84d0b3a8d69ef94072ed675a5 Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Wed, 9 Sep 2020 11:45:27 +0200 Subject: [PATCH] Refactor to new routing api --- packages/app/src/App.tsx | 3 ++ plugins/lighthouse/src/Router.tsx | 30 +++++++++++++++++++ .../components/AuditList/AuditListTable.tsx | 6 ++-- .../src/components/AuditList/index.tsx | 9 +++--- .../src/components/AuditView/index.tsx | 11 +++---- .../src/components/CreateAudit/index.tsx | 4 +-- plugins/lighthouse/src/index.ts | 1 + plugins/lighthouse/src/plugin.ts | 30 +++++++++++++------ 8 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 plugins/lighthouse/src/Router.tsx diff --git a/packages/app/src/App.tsx b/packages/app/src/App.tsx index 3d4f297e8e..97acb6cecb 100644 --- a/packages/app/src/App.tsx +++ b/packages/app/src/App.tsx @@ -30,6 +30,7 @@ import { Router as CatalogRouter } from '@backstage/plugin-catalog'; import { Router as DocsRouter } from '@backstage/plugin-techdocs'; import { Router as GraphiQLRouter } from '@backstage/plugin-graphiql'; import { Router as TechRadarRouter } from '@backstage/plugin-tech-radar'; +import { Router as LighthouseRouter } from '@backstage/plugin-lighthouse'; import { Route, Routes, Navigate } from 'react-router'; import { EntityPage } from './components/catalog/EntityPage'; @@ -68,6 +69,8 @@ const AppRoutes = () => ( element={} /> } /> + } /> + {...deprecatedAppRoutes} ); diff --git a/plugins/lighthouse/src/Router.tsx b/plugins/lighthouse/src/Router.tsx new file mode 100644 index 0000000000..46843504fd --- /dev/null +++ b/plugins/lighthouse/src/Router.tsx @@ -0,0 +1,30 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 from 'react'; +import { Routes, Route } from 'react-router-dom'; +import { rootRouteRef, viewAuditRouteRef, createAuditRouteRef } from './plugin'; +import AuditList from './components/AuditList'; +import AuditView from './components/AuditView'; +import CreateAudit from './components/CreateAudit'; + +export const Router = () => ( + + } /> + } /> + } /> + +); diff --git a/plugins/lighthouse/src/components/AuditList/AuditListTable.tsx b/plugins/lighthouse/src/components/AuditList/AuditListTable.tsx index e52805000a..92d61439a2 100644 --- a/plugins/lighthouse/src/components/AuditList/AuditListTable.tsx +++ b/plugins/lighthouse/src/components/AuditList/AuditListTable.tsx @@ -23,7 +23,7 @@ import { CATEGORY_LABELS, buildSparklinesDataForItem, } from '../../utils'; -import { Link } from '@material-ui/core'; +import { Link } from 'react-router-dom'; import AuditStatusIcon from '../AuditStatusIcon'; const columns: TableColumn[] = [ @@ -94,9 +94,7 @@ export const AuditListTable: FC<{ items: Website[] }> = ({ items }) => { return { websiteUrl: ( - - {website.url} - + {website.url} ), ...trendlines, lastReport: ( diff --git a/plugins/lighthouse/src/components/AuditList/index.tsx b/plugins/lighthouse/src/components/AuditList/index.tsx index c83ce9f641..992af855d3 100644 --- a/plugins/lighthouse/src/components/AuditList/index.tsx +++ b/plugins/lighthouse/src/components/AuditList/index.tsx @@ -16,8 +16,8 @@ import React, { useState, useMemo, FC, ReactNode } from 'react'; import { useLocalStorage, useAsync } from 'react-use'; -import { useNavigate } from 'react-router-dom'; -import { Grid, Button } from '@material-ui/core'; +import { useNavigate, Link } from 'react-router-dom'; +import { Grid, Button, Typography } from '@material-ui/core'; import Alert from '@material-ui/lab/Alert'; import Pagination from '@material-ui/lab/Pagination'; import { @@ -37,6 +37,7 @@ import { useQuery } from '../../utils'; import LighthouseSupportButton from '../SupportButton'; import LighthouseIntro, { LIGHTHOUSE_INTRO_LOCAL_STORAGE } from '../Intro'; import AuditListTable from './AuditListTable'; +import { createAuditRouteRef } from '../../plugin'; export const LIMIT = 10; @@ -77,7 +78,7 @@ const AuditList: FC<{}> = () => { page={page} count={pageCount} onChange={(_event: Event, newPage: number) => { - navigate(`/lighthouse?page=${newPage}`); + navigate(`?page=${newPage}`); }} /> )} @@ -111,7 +112,7 @@ const AuditList: FC<{}> = () => { diff --git a/plugins/lighthouse/src/components/AuditView/index.tsx b/plugins/lighthouse/src/components/AuditView/index.tsx index 12606482bf..17b152c89b 100644 --- a/plugins/lighthouse/src/components/AuditView/index.tsx +++ b/plugins/lighthouse/src/components/AuditView/index.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ import React, { useState, useEffect, ReactNode, FC } from 'react'; -import { Link, useParams } from 'react-router-dom'; +import { Link, generatePath, useParams, useNavigate } from 'react-router-dom'; import { useAsync } from 'react-use'; import { makeStyles, @@ -75,7 +75,7 @@ const AuditLinkList: FC = ({ button component={Link} replace - to={`/lighthouse/audit/${audit.id}`} + to={`audit/${audit.id}`} > @@ -116,6 +116,7 @@ const ConnectedAuditView: FC<{}> = () => { const lighthouseApi = useApi(lighthouseApiRef); const params = useParams() as { id: string }; const classes = useStyles(); + const navigate = useNavigate(); const { loading, error, value: nextValue } = useAsync( async () => await lighthouseApi.getWebsiteForAuditId(params.id), @@ -154,7 +155,7 @@ const ConnectedAuditView: FC<{}> = () => { ); } - let createAuditButtonUrl = '/lighthouse/create-audit'; + let createAuditButtonUrl = 'create-audit'; if (value?.url) { createAuditButtonUrl += `?url=${encodeURIComponent(value.url)}`; } @@ -176,9 +177,9 @@ const ConnectedAuditView: FC<{}> = () => { diff --git a/plugins/lighthouse/src/components/CreateAudit/index.tsx b/plugins/lighthouse/src/components/CreateAudit/index.tsx index a699e148c8..36bd97485a 100644 --- a/plugins/lighthouse/src/components/CreateAudit/index.tsx +++ b/plugins/lighthouse/src/components/CreateAudit/index.tsx @@ -78,7 +78,7 @@ const CreateAudit: FC<{}> = () => { }, }, }); - navigate('/lighthouse'); + navigate('..'); } catch (err) { errorApi.post(err); } finally { @@ -154,7 +154,7 @@ const CreateAudit: FC<{}> = () => {