MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / ShowDownloads

Function ShowDownloads

website/src/components/ShowDownloads.tsx:14–75  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12};
13
14export default function ShowDownloads() {
15 const [stats, setStats] = useState<PypiStats | null>(null);
16 const [error, setError] = useState<string | null>(null);
17
18 useEffect(() => {
19 async function fetchStats() {
20 try {
21 const res = await fetch("/api/pypi/packages/codegraphcontext/recent");
22
23 if (!res.ok) {
24 throw new Error(`API error: ${res.status}`);
25 }
26
27 const data = await res.json();
28
29 // Save last successful monthly downloads
30 if (data?.data?.last_month) {
31 localStorage.setItem(
32 LAST_MONTH_KEY,
33 data.data.last_month.toString()
34 );
35 }
36
37 setStats(data);
38 } catch (err: unknown) {
39 // Trying to use last saved value
40 const savedLastMonth = localStorage.getItem(LAST_MONTH_KEY);
41
42 if (savedLastMonth) {
43 setStats({
44 data: {
45 last_day: 0,
46 last_week: 0,
47 last_month: Number(savedLastMonth),
48 },
49 package: "codegraphcontext",
50 type: "fallback",
51 });
52 } else {
53 setError((err as Error).message);
54 }
55 }
56}
57
58 fetchStats();
59 }, []);
60
61 if (error) return <p className="text-red-500">Error: {error}</p>;
62 if (!stats) return <p>Loading stats...</p>;
63
64 return (
65 <div>
66 {stats?.data ? (
67 <>
68 <p>Last month downloads: {stats.data.last_month.toLocaleString()}+</p>
69 </>
70 ) : (
71 <p>No data available yet for this package</p>

Callers

nothing calls this directly

Calls 1

fetchStatsFunction · 0.85

Tested by

no test coverage detected