From 1d8d298bfb755f02682917a4750f749c3a9b3f60 Mon Sep 17 00:00:00 2001 From: Jeremy Asuncion Date: Fri, 29 Sep 2023 06:18:37 -0700 Subject: [PATCH 1/4] Log info level for cancelled page transition --- frontend/src/hooks/usePageTransitions.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/frontend/src/hooks/usePageTransitions.ts b/frontend/src/hooks/usePageTransitions.ts index 10d83646a..3e6112f22 100644 --- a/frontend/src/hooks/usePageTransitions.ts +++ b/frontend/src/hooks/usePageTransitions.ts @@ -22,6 +22,10 @@ interface RouteEvent { shallow: boolean; } +interface RouteError extends Error { + cancelled: boolean; +} + /** * Hook to manage page transitions effects and state. */ @@ -62,10 +66,13 @@ export function usePageTransitions() { pageTransitionsStore.loading = false; } - const onError = (error: Error, url: string, event: RouteEvent) => { - logger.error({ + const onError = (error: RouteError, url: string, event: RouteEvent) => { + const level = error.cancelled ? 'info' : 'error'; + + logger[level]({ message: 'Error loading route', error: getErrorMessage(error), + cancelled: error.cancelled, }); onFinishLoading(url, event); From 2a4579e308025fedd7730c85e57acfcdc358ff3c Mon Sep 17 00:00:00 2001 From: Jeremy Asuncion Date: Fri, 29 Sep 2023 06:19:11 -0700 Subject: [PATCH 2/4] check for plugin.name when creating plugin links --- .../src/components/HomePage/PluginCard.tsx | 4 ++++ .../SearchPage/PluginSearchResult.tsx | 2 +- frontend/src/utils/sitemap.server.ts | 24 ++++++++++--------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/frontend/src/components/HomePage/PluginCard.tsx b/frontend/src/components/HomePage/PluginCard.tsx index 21e0bff46..add5be2b2 100644 --- a/frontend/src/components/HomePage/PluginCard.tsx +++ b/frontend/src/components/HomePage/PluginCard.tsx @@ -73,6 +73,10 @@ export function PluginCard({ const { t } = useTranslation(['pluginData']); const plausible = usePlausible(); + if (!plugin.name) { + return null; + } + return ( {renderResult()} diff --git a/frontend/src/utils/sitemap.server.ts b/frontend/src/utils/sitemap.server.ts index b1db866b4..cd6f74439 100644 --- a/frontend/src/utils/sitemap.server.ts +++ b/frontend/src/utils/sitemap.server.ts @@ -73,17 +73,19 @@ async function getPluginEntries(): Promise { try { const data = await hubAPI.getPluginIndex(); - return data.map((plugin) => { - const url = `/plugins/${plugin.name}`; - const lastmod = new Date(plugin.release_date).toISOString(); - - return { - url, - lastmod, - name: plugin.display_name ?? plugin.name, - type: SitemapCategory.Plugin, - }; - }); + return data + .filter((plugin) => !!plugin.name) + .map((plugin) => { + const url = `/plugins/${plugin.name}`; + const lastmod = new Date(plugin.release_date).toISOString(); + + return { + url, + lastmod, + name: plugin.display_name ?? plugin.name, + type: SitemapCategory.Plugin, + }; + }); } catch (err) { logger.error({ message: 'Unable to fetch plugin list', From fec9be1d4c5514ee9f2245899bc878adbbbc7988 Mon Sep 17 00:00:00 2001 From: Jeremy Asuncion Date: Fri, 29 Sep 2023 06:23:31 -0700 Subject: [PATCH 3/4] move fetch spdx license data to SSR --- .../MetadataList/MetadataListMetadataItem.tsx | 29 ++----------------- frontend/src/context/plugin.tsx | 2 ++ frontend/src/pages/plugins/[name].tsx | 26 ++++++++++++++++- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/frontend/src/components/MetadataList/MetadataListMetadataItem.tsx b/frontend/src/components/MetadataList/MetadataListMetadataItem.tsx index 8039f1b17..71baaf961 100644 --- a/frontend/src/components/MetadataList/MetadataListMetadataItem.tsx +++ b/frontend/src/components/MetadataList/MetadataListMetadataItem.tsx @@ -2,17 +2,12 @@ import clsx from 'clsx'; import { isString } from 'lodash'; import { useTranslation } from 'next-i18next'; import { ReactNode, useMemo } from 'react'; -import { useQuery } from 'react-query'; import { Link } from '@/components/Link'; -import { MetadataKeys } from '@/context/plugin'; +import { MetadataKeys, usePluginState } from '@/context/plugin'; import { SUPPORTED_PYTHON_VERSIONS } from '@/store/search/filter.store'; -import { SpdxLicenseResponse } from '@/store/search/types'; import { PARAM_KEY_MAP, PARAM_VALUE_MAP } from '@/store/search/utils'; import { PluginType, PluginWriterSaveLayer } from '@/types'; -import { Logger } from '@/utils'; -import { getErrorMessage } from '@/utils/error'; -import { spdxLicenseDataAPI } from '@/utils/spdx'; import styles from './MetadataList.module.scss'; import { MetadataListTextItem } from './MetadataListTextItem'; @@ -102,8 +97,6 @@ const METADATA_FILTER_LINKS = new Set([ 'writerSaveLayers', ]); -const logger = new Logger('MetadataListMetadataItem.ts'); - /** * Component for rendering a metadata value. */ @@ -112,27 +105,9 @@ export function MetadataListMetadataItem({ className, metadataKey, }: Props) { + const { licenses } = usePluginState(); const valueLabel = useMetadataValueLabel(metadataKey, value); - // Fetch SDPX license data to check if current license is OSI Approved. - const { data: licenses } = useQuery( - ['spdx'], - async () => { - const { data } = await spdxLicenseDataAPI.get(''); - return data.licenses; - }, - { - enabled: metadataKey === 'license', - onError(err) { - logger.error({ - message: - 'Error fetching spdx license data for MetadataListMetadataItem', - error: getErrorMessage(err), - }); - }, - }, - ); - const isOsiApproved = useMemo( () => metadataKey === 'license' && diff --git a/frontend/src/context/plugin.tsx b/frontend/src/context/plugin.tsx index 354d1a894..a7f86699a 100644 --- a/frontend/src/context/plugin.tsx +++ b/frontend/src/context/plugin.tsx @@ -5,6 +5,7 @@ import { createContext, ReactNode, useContext } from 'react'; import { DeepPartial } from 'utility-types'; import { SUPPORTED_PYTHON_VERSIONS } from '@/store/search/filter.store'; +import { SpdxLicenseData } from '@/store/search/types'; import { HubDimension, PluginData, @@ -20,6 +21,7 @@ import { formatDate } from '@/utils'; * Shared state for plugin data. */ interface PluginState { + licenses?: SpdxLicenseData[]; plugin?: DeepPartial; repo: PluginRepoData; repoFetchError?: PluginRepoFetchError; diff --git a/frontend/src/pages/plugins/[name].tsx b/frontend/src/pages/plugins/[name].tsx index 58994c392..76bf0b9cf 100644 --- a/frontend/src/pages/plugins/[name].tsx +++ b/frontend/src/pages/plugins/[name].tsx @@ -8,10 +8,12 @@ import { PluginPage } from '@/components/PluginPage'; import { DEFAULT_REPO_DATA } from '@/constants/plugin'; import { useLoadingState } from '@/context/loading'; import { PluginStateProvider } from '@/context/plugin'; +import { SpdxLicenseResponse } from '@/store/search/types'; import { PluginData } from '@/types'; import { createUrl, fetchRepoData, FetchRepoDataResult, Logger } from '@/utils'; import { getErrorMessage } from '@/utils/error'; import { hubAPI } from '@/utils/HubAPIClient'; +import { spdxLicenseDataAPI } from '@/utils/spdx'; import { getServerSidePropsHandler } from '@/utils/ssr'; /** @@ -24,6 +26,7 @@ interface Params extends ParsedUrlQuery { interface BaseProps { error?: string; plugin?: PluginData; + licenses?: SpdxLicenseData[]; } type Props = FetchRepoDataResult & BaseProps; @@ -69,6 +72,20 @@ export const getServerSideProps = getServerSidePropsHandler({ }); } + try { + const { + data: { licenses }, + } = await spdxLicenseDataAPI.get(''); + props.licenses = licenses; + } catch (err) { + props.error = getErrorMessage(err); + + logger.error({ + message: 'Failed to fetch spdx license data', + error: props.error, + }); + } + return { props }; }, }); @@ -77,7 +94,13 @@ export const getServerSideProps = getServerSidePropsHandler({ * This page fetches plugin data from the hub API and renders it in the * PluginDetails component. */ -export default function Plugin({ error, plugin, repo, repoFetchError }: Props) { +export default function Plugin({ + error, + licenses, + plugin, + repo, + repoFetchError, +}: Props) { const isLoading = useLoadingState(); const [t] = useTranslation(['pageTitles', 'pluginPage']); @@ -127,6 +150,7 @@ export default function Plugin({ error, plugin, repo, repoFetchError }: Props) { <> {plugin ? ( Date: Fri, 29 Sep 2023 06:29:53 -0700 Subject: [PATCH 4/4] add missing import --- frontend/src/pages/plugins/[name].tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/pages/plugins/[name].tsx b/frontend/src/pages/plugins/[name].tsx index 76bf0b9cf..8ca7872e4 100644 --- a/frontend/src/pages/plugins/[name].tsx +++ b/frontend/src/pages/plugins/[name].tsx @@ -8,7 +8,7 @@ import { PluginPage } from '@/components/PluginPage'; import { DEFAULT_REPO_DATA } from '@/constants/plugin'; import { useLoadingState } from '@/context/loading'; import { PluginStateProvider } from '@/context/plugin'; -import { SpdxLicenseResponse } from '@/store/search/types'; +import { SpdxLicenseData, SpdxLicenseResponse } from '@/store/search/types'; import { PluginData } from '@/types'; import { createUrl, fetchRepoData, FetchRepoDataResult, Logger } from '@/utils'; import { getErrorMessage } from '@/utils/error';