()
| 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 | }, []); |
no test coverage detected