|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useMemo, useState } from "react"; |
| 4 | +import { CompareForm } from "../components/compare-form"; |
| 5 | +import { ResultDashboard } from "../components/result-dashboard"; |
| 6 | +import { DashboardSkeleton } from "../components/skeletons"; |
| 7 | + |
| 8 | + |
| 9 | +type UserResult = { |
| 10 | + username: string; |
| 11 | + repoScore: number; |
| 12 | + prScore: number; |
| 13 | + contributionScore: number; |
| 14 | + finalScore: number; |
| 15 | + topRepos: { name?: string; stars?: number; score?: number }[]; |
| 16 | + topPullRequests: { repo?: string; stars?: number; score?: number }[]; |
| 17 | + insights?: string[]; |
| 18 | +}; |
| 19 | + |
| 20 | +type ApiResponse = { |
| 21 | + success: boolean; |
| 22 | + users?: UserResult[]; |
| 23 | + error?: string; |
| 24 | +}; |
| 25 | + |
| 26 | +export default function HomePage() { |
| 27 | + const [loading, setLoading] = useState(false); |
| 28 | + const [error, setError] = useState<string | null>(null); |
| 29 | + const [data, setData] = useState<{ user1: UserResult; user2: UserResult } | null>( |
| 30 | + null |
| 31 | + ); |
| 32 | + |
| 33 | + const handleCompare = async (u1: string, u2: string) => { |
| 34 | + setLoading(true); |
| 35 | + setError(null); |
| 36 | + setData(null); |
| 37 | + try { |
| 38 | + const params = new URLSearchParams(); |
| 39 | + params.append("username", u1); |
| 40 | + params.append("username", u2); |
| 41 | + const res = await fetch(`/api/compare?${params.toString()}`); |
| 42 | + const body: ApiResponse = await res.json(); |
| 43 | + if (!body.success || !body.users || body.users.length < 2) { |
| 44 | + throw new Error(body.error || "Comparison failed"); |
| 45 | + } |
| 46 | + setData({ user1: body.users[0], user2: body.users[1] }); |
| 47 | + } catch (err: any) { |
| 48 | + setError(err.message || "Failed to fetch"); |
| 49 | + } finally { |
| 50 | + setLoading(false); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + const skeleton = useMemo(() => <DashboardSkeleton />, []); |
| 55 | + |
| 56 | + return ( |
| 57 | + <main className="min-h-screen"> |
| 58 | + <div className="max-w-6xl mx-auto px-4 py-10 space-y-6" > |
| 59 | + <div className="flex items-center justify-between gap-4"> |
| 60 | + <div className="flex items-center gap-3"> |
| 61 | + <div> |
| 62 | + <p className="text-sm text-slate-500">GitHub Developer Compare</p> |
| 63 | + <h1 className="text-3xl font-semibold text-slate-900"> |
| 64 | + Compare two developers |
| 65 | + </h1> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + |
| 69 | + </div> |
| 70 | + |
| 71 | + |
| 72 | + <CompareForm |
| 73 | + onSubmit={handleCompare} |
| 74 | + loading={loading} |
| 75 | + /> |
| 76 | + |
| 77 | + {loading && skeleton} |
| 78 | + {error && ( |
| 79 | + <div className="card p-4 text-sm text-red-600 bg-red-50 border border-red-100"> |
| 80 | + {error} |
| 81 | + </div> |
| 82 | + )} |
| 83 | + {data && <ResultDashboard user1={data.user1} user2={data.user2} />} |
| 84 | + </div> |
| 85 | + </main> |
| 86 | + ); |
| 87 | +} |
0 commit comments