|
| 1 | +import { useParams } from "react-router-dom"; |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | +import MetricCard from "../../components/MetricCard"; |
| 4 | + |
| 5 | +type PR = { |
| 6 | + title: string; |
| 7 | + html_url: string; |
| 8 | + repository_url: string; |
| 9 | +}; |
| 10 | + |
| 11 | +export default function UserProfile() { |
| 12 | + const { username } = useParams(); |
| 13 | + const [profile, setProfile] = useState<any>(null); |
| 14 | + const [prs, setPRs] = useState<PR[]>([]); |
| 15 | + const [loading, setLoading] = useState(true); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + async function fetchData() { |
| 19 | + if (!username) return; |
| 20 | + |
| 21 | + const userRes = await fetch(`https://api.github.com/users/${username}`); |
| 22 | + const userData = await userRes.json(); |
| 23 | + setProfile(userData); |
| 24 | + |
| 25 | + const prsRes = await fetch(`https://api.github.com/search/issues?q=author:${username}+type:pr`); |
| 26 | + const prsData = await prsRes.json(); |
| 27 | + setPRs(prsData.items); |
| 28 | + setLoading(false); |
| 29 | + } |
| 30 | + |
| 31 | + fetchData(); |
| 32 | + }, [username]); |
| 33 | + |
| 34 | + if (loading) return <div className="text-center mt-10">Loading...</div>; |
| 35 | + |
| 36 | + return ( |
| 37 | + <div className="max-w-3xl mx-auto mt-10 p-4 bg-white shadow-xl rounded-xl"> |
| 38 | + {profile && ( |
| 39 | + <div className="text-center"> |
| 40 | + <img src={profile.avatar_url} className="w-24 h-24 mx-auto rounded-full" /> |
| 41 | + <h2 className="text-2xl font-bold mt-2">{profile.login}</h2> |
| 42 | + <p className="text-gray-600">{profile.bio}</p> |
| 43 | + </div> |
| 44 | + )} |
| 45 | + |
| 46 | + {/* GitHub Metrics Preview */} |
| 47 | + <h3 className="text-xl font-semibold mt-6 mb-2">GitHub Metrics</h3> |
| 48 | + <MetricCard username={username || ""} /> |
| 49 | + |
| 50 | + |
| 51 | + <h3 className="text-xl font-semibold mt-6 mb-2">Pull Requests</h3> |
| 52 | + <ul className="list-disc ml-6 space-y-2"> |
| 53 | + {prs.map((pr, i) => ( |
| 54 | + <li key={i}> |
| 55 | + <a href={pr.html_url} target="_blank" className="text-blue-600 hover:underline"> |
| 56 | + {pr.title} |
| 57 | + </a> |
| 58 | + </li> |
| 59 | + ))} |
| 60 | + </ul> |
| 61 | + </div> |
| 62 | + ); |
| 63 | +} |
0 commit comments