Repository navigation
feat(github-integration): implement GitHub star functionality and UI … #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" } }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| "use client" | ||
|
|
||
| import type { AnchorHTMLAttributes } from "react" | ||
|
|
||
| export function GithubStarLink({ children, onClick, ...props }: AnchorHTMLAttributes<HTMLAnchorElement>) { | ||
| return ( | ||
| <a | ||
| {...props} | ||
| href="https://github.com/yashdev9274/superCli" | ||
| onClick={(event) => { | ||
| navigator.sendBeacon("/api/github/star") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Clicking “Explore on GitHub” or “GitHub” now stars the repository for a signed-in visitor with a usable GitHub token. These labels promise navigation, not permission to change the visitor’s account. Keep navigation separate from starring, or require an explicit “Star on GitHub” action. How this was verified: The navigation link sends a same-origin request, and the route uses the signed-in user’s GitHub token to star the repository. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The client cannot read the response from |
||
| onClick?.(event) | ||
| }} | ||
| > | ||
| {children} | ||
| </a> | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" ? ( | ||
| <GithubStarLink key={item.label} className={`${baseClasses} transition-colors ease-[cubic-bezier(0.23,1,0.32,1)]`}> | ||
| GitHub <GithubStars /> | ||
| </GithubStarLink> | ||
| ) : item.external ? ( | ||
| <a | ||
| key={item.label} | ||
| href={item.href} | ||
| className={`${baseClasses} transition-colors ease-[cubic-bezier(0.23,1,0.32,1)]`} | ||
| > | ||
| {item.label} | ||
| {item.label === "GitHub" && <GithubStars />} | ||
| </a> | ||
| ) : ( | ||
| <Link | ||
|
|
@@ -442,7 +446,16 @@ const Navbar = () => { | |
| : "0ms", | ||
| }} | ||
| > | ||
| {item.external ? ( | ||
| {item.label === "GitHub" ? ( | ||
| <GithubStarLink | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve new-tab navigation for the mobile GitHub link. The previous external-link branch supplied Pass both attributes to this
|
||
| onClick={() => 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)]" | ||
|
Comment on lines
+450
to
+452
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The mobile GitHub replacement drops the previous link’s Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| > | ||
| <span className="text-primary/60 text-sm w-5">{String(i + 1).padStart(2, "0")}</span> | ||
| GitHub | ||
| <span className="ml-auto text-muted-foreground/40 text-sm">↗</span> | ||
| </GithubStarLink> | ||
| ) : item.external ? ( | ||
| <a | ||
| href={item.href} | ||
| target="_blank" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require an explicit star action before changing the user's GitHub account.
A signed-in user who selects “Explore on GitHub” or “GitHub” triggers this beacon. With a valid token, the endpoint stars the repository. Neither label discloses that account change. Previously, these controls only opened the repository.
Keep exploration links navigation-only. Send the star request from a separate, clearly labeled star control or after explicit confirmation. This preserves the star feature without treating repository navigation as consent.