From ac8f2e98893489d773b8bd5a64e4876dd84821a3 Mon Sep 17 00:00:00 2001 From: elrrrrrrr Date: Thu, 2 Jan 2025 14:56:17 +0800 Subject: [PATCH] feat: result sync --- src/hooks/useManifest.ts | 9 ++++++++- src/pages/package/[...slug]/index.tsx | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/hooks/useManifest.ts b/src/hooks/useManifest.ts index 2446f53..f386647 100644 --- a/src/hooks/useManifest.ts +++ b/src/hooks/useManifest.ts @@ -3,6 +3,13 @@ import useSwr from 'swr'; import dayjs from 'dayjs'; import { REGISTRY } from '@/config'; +export class NotFoundError extends Error { + constructor(message: string) { + super(message); + this.name = 'NotFoundError'; + } +} + export interface NpmPackageVersion { name: string; version: string; @@ -70,7 +77,7 @@ export function useInfo(pkgName: string | undefined) { const target = `${REGISTRY}/${pkgName}`; const res = await fetch(target.toString()); if (res.status === 404) { - throw new Error(`Not Found ${pkgName}`); + throw new NotFoundError(`Not Found ${pkgName}`); } if (!res.ok) { diff --git a/src/pages/package/[...slug]/index.tsx b/src/pages/package/[...slug]/index.tsx index 2d01863..d026179 100644 --- a/src/pages/package/[...slug]/index.tsx +++ b/src/pages/package/[...slug]/index.tsx @@ -6,13 +6,14 @@ import PageTrends from '@/slugs/trends'; import PageDeps from '@/slugs/deps'; import 'antd/dist/reset.css'; import CustomTabs from '@/components/CustomTabs'; -import { PackageManifest, useInfo, useSpec } from '@/hooks/useManifest'; +import { NotFoundError, PackageManifest, useInfo, useSpec } from '@/hooks/useManifest'; import Footer from '@/components/Footer'; import { useRouter } from 'next/router'; import { useMemo } from 'react'; import { Result, Spin } from 'antd'; import Header from '@/components/Header'; import { useTheme } from '@/hooks/useTheme'; +import Sync from '@/components/Sync'; const DEFAULT_TYPE = 'home'; const ThemeProvider = _ThemeProvider as any; @@ -98,6 +99,16 @@ export default function PackagePage({}: {}) { const needSync = data?.needSync; if (error) { + if (error instanceof NotFoundError && pkgName) { + return ( + } + /> + ); + } return ; }