feat(refresh): pull-to-refresh broadcasts to useFetch polls

router.refresh() only revalidates server-component data. Until now,
GitHubStatus / SystemStats / CalendarCard / WeatherCard kept their useFetch
caches until the next intervalMs tick — so a user pulling to refresh /today
saw the greeting / action queue / commitments update, but weather + calendar
were stale for up to 10 minutes.

Wire a window-level CustomEvent ("cockpit:refresh"):

- src/lib/client-events.ts (new): SSOT for browser-side event names. Kept
  separate from src/lib/events.ts which owns the server-side Worker/Bridge/
  Brain JSONL schema — different domain, different file.
- PullToRefresh: dispatches the event right after router.refresh() on commit.
- useFetch: adds a window listener that bumps revision when the event fires,
  triggering an immediate refetch.

One gesture, everything refreshes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-20 20:47:35 +02:00
1 parent ea063f3439
commit 093b33f6ec
3 files changed
+31

No files matched your search

+4
View File
@@ -4,6 +4,7 @@ import { useEffect, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import { RefreshCw } from "lucide-react";
import { haptic } from "@/lib/haptics";
import { COCKPIT_REFRESH_EVENT } from "@/lib/client-events";
/**
* Mobile-only pull-to-refresh. Swipe-down past 80px at scrollTop=0 fires
@@ -59,6 +60,9 @@ export function PullToRefresh({ children }: { children: React.ReactNode }) {
setRefreshing(true);
setPull(THRESHOLD);
router.refresh();
// Wake up client-side useFetch polls in lockstep — router.refresh()
// only revalidates server-component data.
window.dispatchEvent(new CustomEvent(COCKPIT_REFRESH_EVENT));
// Hold the indicator briefly so the user sees the refresh, then snap back.
setTimeout(() => {
setRefreshing(false);
+11
View File
@@ -2,6 +2,7 @@
import { useState, useEffect } from "react";
import { getJson } from "@/lib/api/fetch";
import { COCKPIT_REFRESH_EVENT } from "@/lib/client-events";
/**
* Default abort ceiling for every useFetch call. Individual sites can pass
@@ -67,5 +68,15 @@ export function useFetch<T>(
return () => clearInterval(id);
}, [intervalMs, url]);
// Listen for the global refresh event (currently fired by PullToRefresh).
// Lets a single user gesture refresh both server-component data
// (router.refresh) and every client-side useFetch poll at once.
useEffect(() => {
if (!url) return;
const handler = () => setRevision((v) => v + 1);
window.addEventListener(COCKPIT_REFRESH_EVENT, handler);
return () => window.removeEventListener(COCKPIT_REFRESH_EVENT, handler);
}, [url]);
return { data, loading, error, refetch };
}
+16
View File
@@ -0,0 +1,16 @@
/**
* Window-level CustomEvent names used to coordinate state across detached
* client components in the browser. SSOT so producer and listener can't drift.
*
* Distinct from `src/lib/events.ts` — that file owns the Worker/Bridge/Brain
* JSONL event-log schema (a server-side concept). This file is purely for
* runtime DOM events fired through `window`.
*
* Producers: `window.dispatchEvent(new CustomEvent(NAME))`
* Listeners: `window.addEventListener(NAME, handler)` — keep handlers idempotent.
*/
/** Fired by PullToRefresh on a successful commit. useFetch listens and bumps
* its revision so client-side polls refetch in lockstep with the
* router.refresh() that drives server-component revalidation. */
export const COCKPIT_REFRESH_EVENT = "cockpit:refresh";