diff --git a/src/app/[username]/page.jsx b/src/app/[username]/page.jsx index 4138109..50b23ac 100644 --- a/src/app/[username]/page.jsx +++ b/src/app/[username]/page.jsx @@ -1,6 +1,8 @@ 'use client'; +import { generateUserTagline } from "../../../ai/generateTagline"; import { useSearchParams, useRouter } from "next/navigation"; import { useEffect, useState } from "react"; +import {contributions} from '../api/ai/route' function UserPage({ params }) { const { username } = params; @@ -34,12 +36,14 @@ function UserPage({ params }) { router.push(`/${username}${queryString}`); }; + const tagline=generateUserTagline(username,contributions); + return (
-

Welcome, {username}!

+

{tagline}!

diff --git a/src/app/api/ai/route.js b/src/app/api/ai/route.js new file mode 100644 index 0000000..e6cc7da --- /dev/null +++ b/src/app/api/ai/route.js @@ -0,0 +1,75 @@ +import { NextResponse } from "next/server"; +const GITHUB_API_URL = "https://api.github.com/graphql"; + +export async function POST(req) { + const { username } = await req.json(); + + if (!username) { + return new NextResponse("Username required", { status: 400 }); + } + + const query = ` + query ($login: String!) { + user(login: $login) { + login + contributionsCollection { + totalCommitContributions + contributionCalendar { + totalContributions + } + pullRequestContributionsByRepository { + repository { + name + } + totalCount + } + } + repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) { + nodes { + primaryLanguage { + name + } + } + } + } + } + `; + + try { + const response = await fetch(GITHUB_API_URL, { + method: "POST", + headers: { + Accept: "application/json", + Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + query, + variables: { login: username }, + }), + }); + + if (!response.ok) { + return new NextResponse(await response.text(), { + status: response.status, + }); + } + + const data = await response.json(); + console.log(data); + if (data.data.user) { + const contributions = data.data.user.contributionsCollection; + return NextResponse.json({ + exists: true, + totalContributions: contributions.totalCommitContributions, + activeCodingDays: contributions.contributionCalendar.totalContributions + }); + } else { + return NextResponse.json({ exists: false }); + } + } catch (error) { + console.error("Error checking GitHub username:", error); + return new NextResponse("Internal Server Error", { status: 500 }); + } +} +export default contributions; diff --git a/src/app/api/user/route.js b/src/app/api/user/route.js index e37e2f8..147a78c 100644 --- a/src/app/api/user/route.js +++ b/src/app/api/user/route.js @@ -1,5 +1,4 @@ import { NextResponse } from "next/server"; -import { generateUserTagline } from "../../../../ai/generateTagline"; const GITHUB_API_URL = "https://api.github.com/graphql"; @@ -14,25 +13,6 @@ export async function POST(req) { query ($login: String!) { user(login: $login) { login - contributionsCollection { - totalCommitContributions - contributionCalendar { - totalContributions - } - pullRequestContributionsByRepository { - repository { - name - } - totalCount - } - } - repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) { - nodes { - primaryLanguage { - name - } - } - } } } `; @@ -60,14 +40,7 @@ export async function POST(req) { const data = await response.json(); console.log(data); if (data.data.user) { - const contributions = data.data.user.contributionsCollection; - const tagline = await generateUserTagline(username, contributions); - return NextResponse.json({ - exists: true, - totalContributions: contributions.totalCommitContributions, - activeCodingDays: contributions.contributionCalendar.totalContributions, - tagline, - }); + return NextResponse.json({ exists: true }); } else { return NextResponse.json({ exists: false }); }