feat: auto-refresh homepage data every 2 minutes on current week
All checks were successful
Build and Deploy / Build & Push (push) Successful in 52s

Uses router.refresh() to re-run server data fetching (ride counts, open
status) without a full page reload. Only runs when viewing the current
week — no need to poll for past/future weeks.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Josh Wright
2026-04-05 11:00:14 -04:00
parent e7dac31d22
commit 7ee28c7ca3

View File

@@ -1,6 +1,7 @@
"use client";
import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import { WeekCalendar } from "./WeekCalendar";
import { MobileCardList } from "./MobileCardList";
import { WeekNav } from "./WeekNav";
@@ -9,6 +10,8 @@ import { EmptyState } from "./EmptyState";
import { PARKS, groupByRegion } from "@/lib/parks";
import type { DayData } from "@/lib/db";
const REFRESH_INTERVAL_MS = 2 * 60 * 1000; // 2 minutes
const COASTER_MODE_KEY = "coasterMode";
interface HomePageClientProps {
@@ -36,6 +39,7 @@ export function HomePageClient({
hasCoasterData,
scrapedCount,
}: HomePageClientProps) {
const router = useRouter();
const [coastersOnly, setCoastersOnly] = useState(false);
// Hydrate from localStorage after mount to avoid SSR mismatch.
@@ -43,6 +47,13 @@ export function HomePageClient({
setCoastersOnly(localStorage.getItem(COASTER_MODE_KEY) === "true");
}, []);
// Periodically re-fetch server data (ride counts, open status) without a full page reload.
useEffect(() => {
if (!isCurrentWeek) return;
const id = setInterval(() => router.refresh(), REFRESH_INTERVAL_MS);
return () => clearInterval(id);
}, [isCurrentWeek, router]);
// Remember the current week so the park page back button returns here.
useEffect(() => {
localStorage.setItem("lastWeek", weekStart);