From 265942c5f594ac49ab31f0b83f8da69d0bca950c Mon Sep 17 00:00:00 2001 From: Yash Dewasthale Date: Wed, 2 Dec 2026 15:03:29 +0530 Subject: [PATCH] feat(github-integration): implement GitHub star functionality and UI components - Added a new API route to handle starring a GitHub repository, including authentication and error handling. - Created a reusable GithubStarLink component for initiating star requests from the UI. - Updated the HeroSection and Navbar components to utilize the new GithubStarLink for improved user interaction with GitHub. --- apps/web/app/api/github/star/route.ts | 36 +++++++++++++++++++ .../components/homepage/github-star-link.tsx | 18 ++++++++++ apps/web/components/homepage/hero.tsx | 6 ++-- apps/web/components/homepage/navbar.tsx | 19 ++++++++-- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 apps/web/app/api/github/star/route.ts create mode 100644 apps/web/components/homepage/github-star-link.tsx diff --git a/apps/web/app/api/github/star/route.ts b/apps/web/app/api/github/star/route.ts new file mode 100644 index 00000000..4db39b84 --- /dev/null +++ b/apps/web/app/api/github/star/route.ts @@ -0,0 +1,36 @@ +import { auth } from "@/lib/auth" +import { getGithubTokenForUser } from "@/modules/github/lib/github" + +export const runtime = "nodejs" +export const dynamic = "force-dynamic" + +export async function POST(request: Request) { + const origin = request.headers.get("origin") + if (!origin || origin !== new URL(request.url).origin || request.headers.get("sec-fetch-site") === "cross-site") { + return Response.json({ error: "Invalid request origin." }, { status: 403 }) + } + + const session = await auth.api.getSession({ headers: request.headers }) + if (!session?.user) { + return Response.json({ error: "Sign in with GitHub to star the repository." }, { status: 401 }) + } + + try { + const token = await getGithubTokenForUser(session.user.id) + const response = await fetch("https://api.github.com/user/starred/yashdev9274/superCli", { + method: "PUT", + headers: { + Authorization: `Bearer ${token}`, + Accept: "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "Content-Length": "0", + }, + signal: AbortSignal.timeout(10000), + cache: "no-store", + }) + if (!response.ok) throw new Error("GitHub rejected the star request") + return Response.json({ starred: true }, { headers: { "Cache-Control": "no-store" } }) + } catch { + return Response.json({ error: "Could not star the repository. Check your GitHub connection and try again, or star it on GitHub." }, { status: 502, headers: { "Cache-Control": "no-store" } }) + } +} diff --git a/apps/web/components/homepage/github-star-link.tsx b/apps/web/components/homepage/github-star-link.tsx new file mode 100644 index 00000000..f4642050 --- /dev/null +++ b/apps/web/components/homepage/github-star-link.tsx @@ -0,0 +1,18 @@ +"use client" + +import type { AnchorHTMLAttributes } from "react" + +export function GithubStarLink({ children, onClick, ...props }: AnchorHTMLAttributes) { + return ( + { + navigator.sendBeacon("/api/github/star") + onClick?.(event) + }} + > + {children} + + ) +} diff --git a/apps/web/components/homepage/hero.tsx b/apps/web/components/homepage/hero.tsx index 3a56835c..6efe13c9 100644 --- a/apps/web/components/homepage/hero.tsx +++ b/apps/web/components/homepage/hero.tsx @@ -1,6 +1,7 @@ import { ArrowDown, ArrowUpRight } from "lucide-react" import styles from "@/app/(landing)/nova-landing.module.css" +import { GithubStarLink } from "./github-star-link" export default function HeroSection() { return ( @@ -40,12 +41,11 @@ export default function HeroSection() { > Start building - Explore on GitHub - +
diff --git a/apps/web/components/homepage/navbar.tsx b/apps/web/components/homepage/navbar.tsx index 590ec13a..487699b7 100644 --- a/apps/web/components/homepage/navbar.tsx +++ b/apps/web/components/homepage/navbar.tsx @@ -5,6 +5,7 @@ import { Button } from "../ui/button"; import { usePathname } from "next/navigation"; import ProductsDropdown from "./products-dropdown"; import { useEffect, useState } from "react"; +import { GithubStarLink } from "./github-star-link"; const DOCS_URL = process.env.NEXT_PUBLIC_DOCS_URL || "http://localhost:3001/docs/intro"; @@ -272,14 +273,17 @@ const Navbar = () => { const baseClasses = "relative text-[15px] font-medium text-white after:absolute after:bottom-[-3px] after:left-0 after:h-px after:w-0 after:bg-foreground after:transition-[width] after:duration-200 hover:text-foreground hover:after:w-full"; - return item.external ? ( + return item.label === "GitHub" ? ( + + GitHub + + ) : item.external ? ( {item.label} - {item.label === "GitHub" && } ) : ( { : "0ms", }} > - {item.external ? ( + {item.label === "GitHub" ? ( + setMenuOpen(false)} + className="flex items-center gap-3 px-5 py-4 rounded-xl text-[17px] text-foreground/80 hover:text-foreground hover:bg-accent/30 transition-[background-color] duration-150 ease-[cubic-bezier(0.23,1,0.32,1)]" + > + {String(i + 1).padStart(2, "0")} + GitHub + ↗ + + ) : item.external ? (