Skip to content

Commit

Permalink
feat: result sync
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jan 2, 2025
1 parent c19c1c5 commit ac8f2e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/hooks/useManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion src/pages/package/[...slug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,6 +99,16 @@ export default function PackagePage({}: {}) {
const needSync = data?.needSync;

if (error) {
if (error instanceof NotFoundError && pkgName) {
return (
<Result
status="404"
title={`未查询到${pkgName}`}
subTitle="这可能是由于包尚未同步到本地镜像站导致"
extra={<Sync pkgName={pkgName} />}
/>
);
}
return <Result status="error" title="Error" subTitle={error?.message || '系统错误'} />;
}

Expand Down

0 comments on commit ac8f2e9

Please sign in to comment.