-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
![image](https://github.com/cnpm/cnpmweb/assets/5574625/1da9f4d4-f0ae-4eef-8f10-9e5a5b6c9f36) > closes #68 * 添加「下载趋势」相关信息 * 分别展示当前版本和左右版本下载信息
- Loading branch information
Showing
4 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useRecentDownloads } from '@/hooks/useRecentDownloads'; | ||
import React from 'react'; | ||
import { Empty } from 'antd'; | ||
|
||
import { Line } from 'react-chartjs-2'; | ||
import "chart.js/auto"; | ||
import { scales } from 'chart.js/auto'; | ||
|
||
|
||
type RecentDownloadsContentProps = { | ||
pkgName: string; | ||
version: string; | ||
}; | ||
|
||
export function RecentDownloads({ pkgName, version }: RecentDownloadsContentProps) { | ||
const { data: res, isLoading } = useRecentDownloads(pkgName, version); | ||
if (isLoading) { | ||
return <Empty description="暂无数据" />; | ||
} | ||
|
||
return ( | ||
<Line | ||
options={{ | ||
scales: { | ||
x: { | ||
display: false, | ||
}, | ||
}, | ||
plugins: { | ||
tooltip: { | ||
enabled: true, | ||
}, | ||
}, | ||
}} | ||
data={{ | ||
labels: res!.versions?.[version]?.map((_) => _.day), | ||
datasets: [ | ||
{ | ||
fill: true, | ||
label: '所有版本', | ||
data: res!.downloads?.map((_) => _.downloads), | ||
borderColor: 'rgb(53, 162, 235, 0.7)', | ||
backgroundColor: 'rgba(53, 162, 235, 0.4)', | ||
}, | ||
{ | ||
fill: true, | ||
label: version, | ||
data: res!.versions?.[version]?.map((_) => _.downloads), | ||
borderColor: 'rgb(53, 162, 235, 0.8)', | ||
backgroundColor: 'rgba(53, 162, 235, 0.7)', | ||
}, | ||
], | ||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { REGISTRY } from '@/config'; | ||
import dayjs from 'dayjs'; | ||
import useSwr from 'swr'; | ||
|
||
type DownloadRes = { | ||
downloads: { day: string; downloads: number; }[]; | ||
versions?: { | ||
[version: string]: { day: string; downloads: number; }[]; | ||
}; | ||
}; | ||
|
||
// https://registry.npmmirror.com/downloads/range/2023-01-01:2023-12-28/antd | ||
function getUrl(pkgName: string, range = 8) { | ||
const today = dayjs(); | ||
const todayStr = today.format('YYYY-MM-DD'); | ||
const lastWeekStr = today.subtract(range, 'day').format('YYYY-MM-DD'); | ||
return `${REGISTRY}/downloads/range/${lastWeekStr}:${todayStr}/${pkgName}`; | ||
}; | ||
export const useRecentDownloads = (pkgName: string, version: string, range?: number) => { | ||
return useSwr<DownloadRes>(`recent_downloads: ${pkgName}_${version}`, async () => { | ||
return fetch(`${getUrl(pkgName, range)}`) | ||
.then((res) => res.json()); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters