diff --git a/app/page.tsx b/app/page.tsx index a1b5596..92b16fd 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,6 +1,7 @@ "use client"; -import { useMemo, useState } from "react"; +import { useMemo, useState, useEffect, Suspense } from "react"; +import { useSearchParams, useRouter } from "next/navigation"; import { CompareForm } from "../components/compare-form"; import { ResultDashboard } from "../components/result-dashboard"; import { DashboardSkeleton } from "../components/skeletons"; @@ -12,7 +13,9 @@ type ApiResponse = { error?: string; }; -export default function HomePage() { +function HomePageContent() { + const searchParams = useSearchParams(); + const router = useRouter(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [data, setData] = useState<{ @@ -20,7 +23,15 @@ export default function HomePage() { user2: UserResult; } | null>(null); + const initialUsername1 = searchParams.get("u1") ?? ""; + const initialUsername2 = searchParams.get("u2") ?? ""; + const handleCompare = async (u1: string, u2: string) => { + const urlParams = new URLSearchParams(); + if (u1) urlParams.set("u1", u1); + if (u2) urlParams.set("u2", u2); + router.replace(`?${urlParams.toString()}`, { scroll: false }); + setLoading(true); setError(null); setData(null); @@ -53,20 +64,87 @@ export default function HomePage() { } }; + useEffect(() => { + if (initialUsername1 && initialUsername2) { + handleCompare(initialUsername1, initialUsername2); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + const skeleton = useMemo(() => , []); const reset = () => { setData(null); setError(null); + router.replace("/", { scroll: false }); }; + const swapUsers = () => { if (!data) return; setData((d) => ({ user1: d!.user2, user2: d!.user1 })); - console.log("Swapped users", data); + const u1 = searchParams.get("u2") ?? ""; + const u2 = searchParams.get("u1") ?? ""; + if (u1 && u2) { + const params = new URLSearchParams(); + params.set("u1", u1); + params.set("u2", u2); + router.replace(`?${params.toString()}`, { scroll: false }); + } }; + + return ( +
+ + + {loading && skeleton} + {error && ( +
+ {error} +
+ )} + {data && } + {!loading && !error && !data && ( +
+ + + + + +

+ Enter two usernames to compare +

+

+ Compare GitHub developer metrics side by side. + Share the URL to save your comparison. +

+
+ )} +
+ ); +} + +export default function HomePage() { return (
- {" "}
@@ -74,50 +152,17 @@ export default function HomePage() { DevImpact
-
-
- - - {loading && skeleton} - {error && ( -
- {error} -
- )} - {data && } - {!loading && !error && !data && ( -
- - - - - -

Enter two usernames to compare

-

- Compare GitHub developer metrics side by side -

+ +
- )} -
+ } + > + +