feat: add GitHub projects page

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-02-13 16:59:56 +08:00
parent 2402c94760
commit fde17c2308
4 changed files with 158 additions and 0 deletions

View File

@@ -33,3 +33,9 @@ NEXT_PUBLIC_TWITTER_CARD_TYPE="summary_large_image"
# Analytics (public ID only) # Analytics (public ID only)
NEXT_PUBLIC_ANALYTICS_ID="" NEXT_PUBLIC_ANALYTICS_ID=""
# Server-side only (NOT exposed to browser)
# Used to fetch GitHub repositories for the /projects page.
# Copy these into your local `.env.local` and fill in real values.
GITHUB_USERNAME="your-github-username"
GITHUB_TOKEN="your-github-token"

77
app/projects/page.tsx Normal file
View File

@@ -0,0 +1,77 @@
import Link from 'next/link';
import { fetchPublicRepos } from '@/lib/github';
import { SidebarLayout } from '@/components/sidebar-layout';
export const revalidate = 3600;
export const metadata = {
title: 'GitHub 專案',
};
export default async function ProjectsPage() {
const repos = await fetchPublicRepos();
return (
<section className="space-y-4">
<SidebarLayout>
<header className="space-y-1">
<h1 className="type-title font-semibold text-slate-900 dark:text-slate-50">
GitHub
</h1>
<p className="type-small text-slate-500 dark:text-slate-400">
GitHub
</p>
</header>
{repos.length === 0 ? (
<p className="mt-4 type-small text-slate-500 dark:text-slate-400">
GitHub GitHub
</p>
) : (
<ul className="mt-4 grid gap-4 sm:grid-cols-2">
{repos.map((repo) => (
<li
key={repo.id}
className="flex h-full flex-col rounded-lg border border-slate-200 bg-white p-4 shadow-sm transition hover:-translate-y-0.5 hover:shadow-md dark:border-slate-800 dark:bg-slate-900"
>
<div className="flex items-start justify-between gap-2">
<Link
href={repo.htmlUrl}
prefetch={false}
target="_blank"
rel="noreferrer"
className="type-base font-semibold text-slate-900 transition-colors hover:text-accent dark:text-slate-50"
>
{repo.name}
</Link>
{repo.stargazersCount > 0 && (
<span className="inline-flex items-center gap-1 rounded-full bg-amber-50 px-2 py-0.5 text-xs font-medium text-amber-700 dark:bg-amber-900/40 dark:text-amber-300">
{repo.stargazersCount}
</span>
)}
</div>
{repo.description && (
<p className="mt-2 flex-1 type-small text-slate-600 dark:text-slate-300">
{repo.description}
</p>
)}
<div className="mt-3 flex items-center justify-between text-xs text-slate-500 dark:text-slate-400">
<span>{repo.language ?? '其他'}</span>
<span suppressHydrationWarning>
{' '}
{repo.updatedAt
? new Date(repo.updatedAt).toLocaleDateString('zh-TW')
: '未知'}
</span>
</div>
</li>
))}
</ul>
)}
</SidebarLayout>
</section>
);
}

View File

@@ -57,6 +57,7 @@ export function SiteHeader() {
const navItems: NavLinkItem[] = [ const navItems: NavLinkItem[] = [
{ key: 'home', href: '/', label: '首頁', iconKey: 'home' }, { key: 'home', href: '/', label: '首頁', iconKey: 'home' },
{ key: 'projects', href: '/projects', label: '作品', iconKey: 'pen' },
{ {
key: 'about', key: 'about',
href: aboutChildren[0]?.href, href: aboutChildren[0]?.href,

74
lib/github.ts Normal file
View File

@@ -0,0 +1,74 @@
export type RepoSummary = {
id: number;
name: string;
fullName: string;
htmlUrl: string;
description: string | null;
language: string | null;
stargazersCount: number;
updatedAt: string;
};
const GITHUB_API_BASE = 'https://api.github.com';
function getGithubHeaders() {
const headers: Record<string, string> = {
Accept: 'application/vnd.github+json',
'User-Agent': 'blog-nextjs-app',
};
const token = process.env.GITHUB_TOKEN;
if (token && token.trim() !== '') {
headers.Authorization = `Bearer ${token}`;
}
return headers;
}
/**
* Fetch all public repositories for the configured GitHub user.
* Returns an empty array on error instead of throwing, so the UI
* can render a graceful fallback.
*/
export async function fetchPublicRepos(usernameOverride?: string): Promise<RepoSummary[]> {
const username = usernameOverride || process.env.GITHUB_USERNAME;
if (!username) {
console.error('GITHUB_USERNAME is not set; cannot fetch GitHub repositories.');
return [];
}
const url = `${GITHUB_API_BASE}/users/${encodeURIComponent(
username
)}/repos?type=public&sort=updated`;
try {
const res = await fetch(url, {
headers: getGithubHeaders(),
// Use Next.js App Router caching / ISR
next: { revalidate: 3600 },
});
if (!res.ok) {
console.error('Failed to fetch GitHub repositories:', res.status, res.statusText);
return [];
}
const data = (await res.json()) as any[];
return data.map((repo) => ({
id: repo.id,
name: repo.name,
fullName: repo.full_name,
htmlUrl: repo.html_url,
description: repo.description,
language: repo.language,
stargazersCount: repo.stargazers_count,
updatedAt: repo.updated_at,
}));
} catch (error) {
console.error('Error while fetching GitHub repositories:', error);
return [];
}
}