想要在 React 作品集中展示您的 GitHub 活動?在本教程中,我將向您展示如何建立一個 React 元件,該元件使用 GitHub 的 GraphQL API 顯示您的 GitHub 貢獻總量,並配有高效的快取。讓我們建造一些很酷的東西! ?
我們將建立一個 React 元件:
在我們開始之前,您需要:
首先,在專案根目錄中建立一個 .env 檔案並新增您的 GitHub 令牌:
NEXT_PUBLIC_GITHUB_TOKEN=your_github_token_here
建立一個名為 githubApi.js 的新檔案:
export async function fetchGithubCommits(username) { const GITHUB_TOKEN = process.env.NEXT_PUBLIC_GITHUB_TOKEN; const CACHE_KEY = `github-commits-${username}`; const CACHE_TTL = 3600; // 1 hour in seconds if (!GITHUB_TOKEN) { console.error("No GitHub token found!"); throw new Error("GitHub token is required"); } const cachedData = getCachedData(CACHE_KEY); if (cachedData) { return cachedData.value; } try { const currentYear = new Date().getFullYear(); const startYear = 2020; let totalCommits = 0; for (let year = startYear; year <= currentYear; year++) { const query = ` query($username: String!, $from: DateTime!, $to: DateTime!) { user(login: $username) { contributionsCollection(from: $from, to: $to) { totalCommitContributions restrictedContributionsCount } } } `; const response = await fetch("https://api.github.com/graphql", { method: "POST", headers: { Authorization: `Bearer ${GITHUB_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ query, variables: { username, from: `${year}-01-01T00:00:00Z`, to: `${year}-12-31T23:59:59Z`, }, }), }); const data = await response.json(); if (data.errors) { throw new Error(data.errors[0].message); } const yearCommits = (data.data?.user?.contributionsCollection?.totalCommitContributions || 0) + (data.data?.user?.contributionsCollection?.restrictedContributionsCount || 0); totalCommits += yearCommits; } setCachedData(CACHE_KEY, totalCommits, CACHE_TTL); return totalCommits; } catch (error) { console.error("Error fetching GitHub commits:", error); throw error; } } function setCachedData(key, value, ttl) { const item = { value, timestamp: Date.now(), ttl: ttl * 1000, }; localStorage.setItem(key, JSON.stringify(item)); } function getCachedData(key) { try { const item = localStorage.getItem(key); if (!item) return null; const parsedItem = JSON.parse(item); const now = Date.now(); if (now - parsedItem.timestamp > parsedItem.ttl) { localStorage.removeItem(key); return null; } return parsedItem; } catch { return null; } } export function invalidateCommitsCache(username) { localStorage.removeItem(`github-commits-${username}`); }
建立一個名為 GitHubStats.js 的新檔案:
import React, { useState, useEffect } from 'react'; import { fetchGithubCommits } from './githubApi'; const GitHubStats = ({ username }) => { const [commits, setCommits] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchStats = async () => { try { setLoading(true); setError(null); const totalCommits = await fetchGithubCommits(username); setCommits(totalCommits); } catch (err) { setError(err.message); } finally { setLoading(false); } }; fetchStats(); }, [username]); if (loading) { return <div>Loading GitHub stats...</div>; } if (error) { return <div>Error: {error}</div>; } return ( <div className="github-stats"> <h2>GitHub Contributions</h2> <p>Total commits since 2020: {commits.toLocaleString()}</p> </div> ); }; export default GitHubStats;
讓我們加入一些基本樣式。建立 GitHubStats.css:
.github-stats { padding: 20px; border-radius: 8px; background-color: #f6f8fa; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); margin: 20px 0; } .github-stats h2 { margin: 0 0 15px 0; color: #24292e; } .github-stats p { font-size: 1.2em; color: #586069; }
現在您可以在應用程式中使用該元件:
import GitHubStats from './GitHubStats'; function App() { return ( <div className="App"> <h1>My Developer Portfolio</h1> <GitHubStats username="your-github-username" /> </div> ); }
更新 GitHubStats.js 以包含手動刷新選項:
import React, { useState, useEffect } from 'react'; import { fetchGithubCommits, invalidateCommitsCache } from './githubApi'; const GitHubStats = ({ username }) => { // ... previous state declarations ... const handleRefresh = async () => { invalidateCommitsCache(username); await fetchStats(); }; return ( <div className="github-stats"> <h2>GitHub Contributions</h2> <p>Total commits since 2020: {commits.toLocaleString()}</p> <button onClick={handleRefresh} disabled={loading}> {loading ? 'Refreshing...' : 'Refresh Stats'} </button> </div> ); };
我們可以修改組件以顯示每年的貢獻:
NEXT_PUBLIC_GITHUB_TOKEN=your_github_token_here
現在,您的 React 應用程式中已經有了一個功能齊全的 GitHub stats 元件!此實施為您的建置提供了堅實的基礎。一些增強的想法:
請記得確保您的 GitHub 令牌安全,切勿將其提交到您的儲存庫。快樂編碼! ?
以上是如何將 GitHub 貢獻統計新增到您的 React 應用程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!