Skip to content

site: extract shared liveData/fetchJson helpers for the live-data components - #438

Merged
max-sixty merged 1 commit into
mainfrom
tend-activity
May 12, 2026
Merged

max-sixty merged 1 commit into
mainfrom
tend-activity

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

Pure refactor — no behavior change. Stats.astro, Activity.astro, and CurrentlyTending.astro each carried their own copy of the same runtime-fetch boilerplate (fetch with Accept header, resp.ok guard, try/catch → bail, hidden = false on success). A prior review on #433 flagged the duplication. This lifts it into site/src/lib/api.ts:

  • fetchJson<T>(path): Promise<T | null> — the fetch wrapper (null on non-OK / network error). Was getJson inside CurrentlyTending.
  • liveData<T>(path, sectionId, render) — looks up getElementById(sectionId), calls fetchJson(path), hands the parsed body (or null on failure) to render(data, el), and reveals the element only if render returns true. render owns the DOM and the "is this worth showing?" decision, and may issue further fetches — CurrentlyTending passes an async render that does its /activity fallback this way.

WORKER_URL is now module-private to api.ts (only fetchJson uses it). Renamed CurrentlyTending's local render helper to paint so it doesn't overlap with liveData's render parameter. README line updated.

Net −12 lines; bundles shrink slightly (a shared api.*.js chunk replaces the per-component copies).

Test plan

  • npm run build clean; pre-commit run --all-files green; tsc --noEmit passes (checked via reviewer)
  • Verified in a browser against the production Worker: all three sections render — saw both CurrentlyTending's live state (tending owner/repo · review (+2 more)) and idle fallback (last tended N min ago), the stat strip, and the 10-row activity feed — no console errors
  • Behavior preserved: CurrentlyTending still falls back to /activity when /currently-tending fails (not only when it's empty); each section still hides in exactly the cases it did before

…ponents

Stats, Activity, and CurrentlyTending each carried their own copy of the
fetch / resp.ok-guard / try-catch / hidden=false boilerplate. Lift it into
site/src/lib/api.ts: fetchJson<T> (the fetch wrapper, was getJson in
CurrentlyTending) and liveData<T>(path, sectionId, render), which fetches,
hands the body to render, and reveals the section only if render returns
true. WORKER_URL is now module-private. No behavior change.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 8b1c60e into main May 12, 2026
6 checks passed
@max-sixty
max-sixty deleted the tend-activity branch May 12, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants