diff --git a/src/app/api/stats/route.ts b/src/app/api/stats/route.ts new file mode 100644 index 0000000..613efae --- /dev/null +++ b/src/app/api/stats/route.ts @@ -0,0 +1,42 @@ +import { NextRequest, NextResponse } from "next/server"; + +var lastGitRequest = new Date("2023-01-10T12:00:00Z"); +var stats = { stars: 0, forks: 0, contributors: 0 }; + +async function getStats() { + let page = 0; + let stars = 0; + let forks = 0; + let contributors = 0; + + while (true) { + const res = await fetch( + `https://api.github.com/users/orus-dev/repos?per_page=100&page=${page}` + ); + const repos = await res.json(); + + if (repos.length === 0) break; + + for (const repo of repos) { + stars += repo.stargazers_count; + forks += repo.forks_count; + const conRes = await fetch(repo.contributors_url); + contributors += (await conRes.json()).length; + } + + page++; + } + + return { stars, forks, contributors }; +} + +export async function GET(req: NextRequest) { + const now = new Date(); + + if (now.getTime() - lastGitRequest.getTime() > 60 * 60 * 1000) { + stats = await getStats(); + lastGitRequest = now; + } + + return NextResponse.json(stats); +} diff --git a/src/app/projects/page.tsx b/src/app/projects/page.tsx index c1904b9..52b7473 100644 --- a/src/app/projects/page.tsx +++ b/src/app/projects/page.tsx @@ -12,15 +12,31 @@ import { } from "lucide-react"; import WebStuff from "@/components/Web"; import projects from "@/lib/projects"; +import formatNumber from "@/lib/formatNumber"; export default function ProjectsPage() { const [mounted, setMounted] = useState(false); const [activeFilter, setActiveFilter] = useState("all"); + const [orgStats, setOrgStats] = useState({ + contributors: 0, + stars: 0, + forks: 0, + }); useEffect(() => { setMounted(true); }, []); + useEffect(() => { + async function fetchStats() { + const res = await fetch(`/api/stats`); + + setOrgStats(await res.json()); + } + + fetchStats(); + }, []); + const filters = [ { id: "all", label: "All Projects" }, { id: "security", label: "Security" }, @@ -221,9 +237,12 @@ export default function ProjectsPage() {