diff --git a/src/App.jsx b/src/App.jsx index 1dcddbb..7fb9983 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -4,6 +4,7 @@ import Stats from "./components/Stats.jsx"; import About from "./components/About.jsx"; import Experience from "./components/Experience.jsx"; import Projects from "./components/Projects.jsx"; +import PushTracker from "./components/PushTracker.jsx"; import GitHubGrid from "./components/GitHubGrid.jsx"; import SkillsCarousel from "./components/SkillsCarousel.jsx"; import Interests from "./components/Interests.jsx"; @@ -20,6 +21,7 @@ export default function App() { + diff --git a/src/components/PushTracker.jsx b/src/components/PushTracker.jsx new file mode 100644 index 0000000..015420d --- /dev/null +++ b/src/components/PushTracker.jsx @@ -0,0 +1,175 @@ +import { useEffect, useState } from "react"; +import { profile } from "../content.js"; + +const WEEKS = 13; +const MAX_PAGES = 5; + +function startOfLocalDay(d) { + const x = new Date(d); + x.setHours(0, 0, 0, 0); + return x; +} + +function addDays(d, n) { + const x = new Date(d); + x.setDate(x.getDate() + n); + return x; +} + +function dayKey(d) { + return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String( + d.getDate() + ).padStart(2, "0")}`; +} + +function fmtDay(d) { + return d.toLocaleDateString("en-GB", { day: "numeric", month: "short" }); +} + +function levelFor(count) { + if (count <= 0) return 0; + if (count === 1) return 1; + if (count <= 3) return 2; + if (count <= 6) return 3; + return 4; +} + +async function fetchPushCounts(cutoff) { + const counts = new Map(); + let totalPushes = 0; + let lastPush = null; + + for (let page = 1; page <= MAX_PAGES; page++) { + const res = await fetch( + `https://api.github.com/users/${profile.githubUser}/events/public?per_page=100&page=${page}`, + { headers: { Accept: "application/vnd.github+json" } } + ); + if (!res.ok) throw new Error(`GitHub API responded ${res.status}`); + const events = await res.json(); + if (!Array.isArray(events) || events.length === 0) break; + + let oldest = null; + for (const ev of events) { + const created = new Date(ev.created_at); + oldest = oldest === null ? created : (created < oldest ? created : oldest); + if (ev.type !== "PushEvent") continue; + const day = startOfLocalDay(created); + if (day < cutoff) continue; + const k = dayKey(day); + counts.set(k, (counts.get(k) || 0) + 1); + totalPushes += 1; + if (lastPush === null || created > lastPush) lastPush = created; + } + + // Events arrive newest-first: once this page reaches past the window, + // older pages can't contribute anything. + if (oldest !== null && oldest < cutoff) break; + } + + return { counts, totalPushes, lastPush }; +} + +export default function PushTracker() { + const [status, setStatus] = useState("loading"); + const [data, setData] = useState(null); + + useEffect(() => { + let cancelled = false; + (async () => { + try { + const today = startOfLocalDay(new Date()); + const thisSunday = addDays(today, -today.getDay()); + const gridStart = addDays(thisSunday, -(WEEKS - 1) * 7); + const cutoff = addDays(gridStart, -5); + const result = await fetchPushCounts(cutoff); + if (cancelled) return; + setData({ ...result, today, gridStart }); + setStatus("ready"); + } catch { + if (!cancelled) setStatus("error"); + } + })(); + return () => { + cancelled = true; + }; + }, []); + + let grid = null; + let statsLine = null; + if (status === "ready" && data) { + const { counts, totalPushes, lastPush, today, gridStart } = data; + const activeDays = counts.size; + + const columns = []; + for (let c = 0; c < WEEKS; c++) { + const week = []; + for (let r = 0; r < 7; r++) { + const day = addDays(gridStart, c * 7 + r); + const count = counts.get(dayKey(day)) || 0; + week.push({ day, count, future: day > today }); + } + columns.push(week); + } + grid = columns; + + statsLine = ( +

+ {totalPushes} push{totalPushes === 1 ? "" : "es"} · {activeDays} active day + {activeDays === 1 ? "" : "s"} + {lastPush ? ` · last push ${fmtDay(lastPush)}` : ""} +

+ ); + } + + return ( +
+
+

Activity

+

Push tracker

+

+ Every git push across my public repositories, pulled live from GitHub's + events API — the last 13 weeks. +

+
+ {status === "loading" && ( +

Fetching push activity…

+ )} + {status === "error" && ( +

Push activity is unavailable right now.

+ )} + {status === "ready" && grid && ( + <> +
+ {grid.map((week, c) => ( +
+ {week.map((cell, r) => ( + + ))} +
+ ))} +
+
+ {statsLine} + +
+ + )} +
+
+
+ ); +} diff --git a/src/index.css b/src/index.css index 29731d2..dedf0d2 100644 --- a/src/index.css +++ b/src/index.css @@ -583,6 +583,87 @@ a { background: var(--accent-tint); } +/* ---------- push tracker ---------- */ + +.push-tracker { + border-top: 1px solid var(--hairline); +} + +.pt-card { + margin-top: 48px; + background: var(--surface); + border: 1px solid var(--hairline); + border-radius: var(--radius); + padding: 28px; +} + +.pt-grid { + display: flex; + gap: 3px; +} + +.pt-col { + display: flex; + flex-direction: column; + gap: 3px; +} + +.pt-cell { + width: 12px; + height: 12px; + border-radius: 3px; + background: #efede5; +} + +.pt-cell.l1 { + background: #e0d6f2; +} + +.pt-cell.l2 { + background: #c6b2e8; +} + +.pt-cell.l3 { + background: #a184d6; +} + +.pt-cell.l4 { + background: #7050bd; +} + +.pt-cell.is-future { + opacity: 0; + pointer-events: none; +} + +.pt-note { + color: var(--muted); + font-size: 15px; + padding: 24px 0; +} + +.pt-foot { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + margin-top: 18px; + flex-wrap: wrap; +} + +.pt-stats { + color: var(--ink-soft); + font-size: 14px; +} + +.pt-legend { + display: flex; + align-items: center; + gap: 4px; + color: var(--muted); + font-size: 12px; +} + /* ---------- github ---------- */ .github {