well, let's clean up some more

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-08-18 15:42:17 +02:00
parent e195112c5c
commit f6be17460d
17 changed files with 101 additions and 114 deletions
+4 -3
View File
@@ -25,9 +25,11 @@ import { LIGHTHOUSE_WEBSITE_URL_ANNOTATION } from '../constants';
import { AuditListForEntity } from './components/AuditList/AuditListForEntity';
import { MissingAnnotationEmptyState } from '@backstage/core-components';
/** @public */
export const isLighthouseAvailable = (entity: Entity) =>
Boolean(entity.metadata.annotations?.[LIGHTHOUSE_WEBSITE_URL_ANNOTATION]);
/** @public */
export const Router = () => (
<Routes>
<Route path="/" element={<AuditList />} />
@@ -36,9 +38,8 @@ export const Router = () => (
</Routes>
);
type Props = {};
export const EmbeddedRouter = (_props: Props) => {
/** @public */
export const EmbeddedRouter = () => {
const { entity } = useEntity();
if (!isLighthouseAvailable(entity)) {
+21 -5
View File
@@ -17,6 +17,7 @@
import { Config } from '@backstage/config';
import { createApiRef } from '@backstage/core-plugin-api';
/** @public */
export type LighthouseCategoryId =
| 'pwa'
| 'seo'
@@ -24,17 +25,20 @@ export type LighthouseCategoryId =
| 'accessibility'
| 'best-practices';
/** @public */
export interface LighthouseCategoryAbbr {
id: LighthouseCategoryId;
score: number;
title: string;
}
/** @public */
export interface LASListRequest {
offset?: number;
limit?: number;
}
/** @public */
export interface LASListResponse<Item> {
items: Item[];
total: number;
@@ -42,21 +46,25 @@ export interface LASListResponse<Item> {
limit: number;
}
interface AuditBase {
/** @public */
export interface AuditBase {
id: string;
url: string;
timeCreated: string;
}
/** @public */
export interface AuditRunning extends AuditBase {
status: 'RUNNING';
}
/** @public */
export interface AuditFailed extends AuditBase {
status: 'FAILED';
timeCompleted: string;
}
/** @public */
export interface AuditCompleted extends AuditBase {
status: 'COMPLETED';
timeCompleted: string;
@@ -64,16 +72,20 @@ export interface AuditCompleted extends AuditBase {
categories: Record<LighthouseCategoryId, LighthouseCategoryAbbr>;
}
/** @public */
export type Audit = AuditRunning | AuditFailed | AuditCompleted;
/** @public */
export interface Website {
url: string;
audits: Audit[];
lastAudit: Audit;
}
/** @public */
export type WebsiteListResponse = LASListResponse<Website>;
/** @public */
export interface TriggerAuditPayload {
url: string;
options: {
@@ -85,6 +97,7 @@ export interface TriggerAuditPayload {
};
}
/** @public */
export class FetchError extends Error {
get name(): string {
return this.constructor.name;
@@ -99,6 +112,7 @@ export class FetchError extends Error {
}
}
/** @public */
export type LighthouseApi = {
url: string;
getWebsiteList: (listOptions: LASListRequest) => Promise<WebsiteListResponse>;
@@ -107,10 +121,12 @@ export type LighthouseApi = {
getWebsiteByUrl: (websiteUrl: string) => Promise<Website>;
};
/** @public */
export const lighthouseApiRef = createApiRef<LighthouseApi>({
id: 'plugin.lighthouse.service',
});
/** @public */
export class LighthouseRestApi implements LighthouseApi {
static fromConfig(config: Config) {
return new LighthouseRestApi(config.getString('lighthouse.baseUrl'));
@@ -124,10 +140,10 @@ export class LighthouseRestApi implements LighthouseApi {
return await resp.json();
}
async getWebsiteList({
limit,
offset,
}: LASListRequest = {}): Promise<WebsiteListResponse> {
async getWebsiteList(
options: LASListRequest = {},
): Promise<WebsiteListResponse> {
const { limit, offset } = options;
const params = new URLSearchParams();
if (typeof limit === 'number') params.append('limit', limit.toString());
if (typeof offset === 'number') params.append('offset', offset.toString());
@@ -27,8 +27,8 @@ import {
StructuredMetadataTable,
} from '@backstage/core-components';
const LighthouseCategoryScoreStatus = ({ score }: { score: number }) => {
const scoreAsPercentage = Math.round(score * 100);
const LighthouseCategoryScoreStatus = (props: { score: number }) => {
const scoreAsPercentage = Math.round(props.score * 100);
switch (true) {
case scoreAsPercentage >= 90:
return (
@@ -56,20 +56,15 @@ const LighthouseCategoryScoreStatus = ({ score }: { score: number }) => {
}
};
const LighthouseAuditStatus = ({ audit }: { audit: Audit }) => (
const LighthouseAuditStatus = (props: { audit: Audit }) => (
<>
<AuditStatusIcon audit={audit} />
{audit.status.toLocaleUpperCase('en-US')}
<AuditStatusIcon audit={props.audit} />
{props.audit.status.toLocaleUpperCase('en-US')}
</>
);
const LighthouseAuditSummary = ({
audit,
dense = false,
}: {
audit: Audit;
dense?: boolean;
}) => {
const LighthouseAuditSummary = (props: { audit: Audit; dense?: boolean }) => {
const { audit, dense = false } = props;
const { url } = audit;
const flattenedCategoryData: Record<string, React.ReactNode> = {};
if (audit.status === 'COMPLETED') {
@@ -92,13 +87,12 @@ const LighthouseAuditSummary = ({
return <StructuredMetadataTable metadata={tableData} dense={dense} />;
};
export const LastLighthouseAuditCard = ({
dense = false,
variant,
}: {
/** @public */
export const LastLighthouseAuditCard = (props: {
dense?: boolean;
variant?: InfoCardVariants;
}) => {
const { dense = false, variant } = props;
const { value: website, loading, error } = useWebsiteForEntity();
let content;
+4
View File
@@ -40,6 +40,7 @@ export const entityContentRouteRef = createRouteRef({
id: 'lighthouse:entity-content',
});
/** @public */
export const lighthousePlugin = createPlugin({
id: 'lighthouse',
apis: [
@@ -55,6 +56,7 @@ export const lighthousePlugin = createPlugin({
},
});
/** @public */
export const LighthousePage = lighthousePlugin.provide(
createRoutableExtension({
name: 'LighthousePage',
@@ -63,6 +65,7 @@ export const LighthousePage = lighthousePlugin.provide(
}),
);
/** @public */
export const EntityLighthouseContent = lighthousePlugin.provide(
createRoutableExtension({
name: 'EntityLighthouseContent',
@@ -71,6 +74,7 @@ export const EntityLighthouseContent = lighthousePlugin.provide(
}),
);
/** @public */
export const EntityLastLighthouseAuditCard = lighthousePlugin.provide(
createComponentExtension({
name: 'EntityLastLighthouseAuditCard',