"use client"; import Link from "next/link"; import { useEffect, useState } from "react"; import { WeekCard, WeeklyEmailSwitch } from "@/components/WeeklyHoroscope"; import { api, errorText } from "@/lib/api"; import type { Analysis } from "@/lib/types"; import { MONTHS } from "@/lib/weekly"; const POLL_MS = 3000; const MAX_POLLS = 60; interface Week { week_start: string; week_end: string; analysis: Analysis; } interface Month { month: string; current_month: string; this_week: string; weeks: Week[]; older: string | null; newer: string | null; email: boolean; } /** "2026. november". */ function monthName(key: string): string { const [y, m] = key.split("-").map(Number); return `${y}. ${MONTHS[m - 1]}`; } /** * One month of weekly horoscopes, the newest week first. In this month the * week's horoscope is asked for first (queued if new), then read until the * worker has written it (or given up); meanwhile its card shows the waiting * animation. */ export function WeeklyPage({ month, isAdmin }: { month: string | null; isAdmin: boolean }) { const [data, setData] = useState(null); const [error, setError] = useState(null); const [gaveUp, setGaveUp] = useState(false); // Bumped when an admin has this week's text written again: polls anew. const [round, setRound] = useState(0); const query = month ? `?month=${month}` : ""; /** This week's horoscope into the list (it may not be there yet). */ const putWeek = (week: Week) => setData((d) => (d ? { ...d, weeks: [week, ...d.weeks.filter((w) => w.week_start !== week.week_start)] } : d)); useEffect(() => { let stopped = false; let timer: ReturnType | undefined; const wait = () => new Promise((resolve) => (timer = setTimeout(resolve, POLL_MS))); /** * This week's text: asked for once (queued if new, or written again if * the last attempt failed), then only read until the worker is done. */ const follow = async () => { let week: Week | null = null; for (let tries = 0; !week && tries < 5 && !stopped; tries++) { try { const got = await api("/weekly", { method: "POST" }); week = { week_start: got.week_start, week_end: got.week_end, analysis: got.analysis }; } catch { await wait(); } } if (!week || stopped) return setGaveUp(!stopped); putWeek(week); for (let polls = 0; !stopped; polls++) { const { status } = week.analysis; if (status === "done" || status === "failed") return; if (polls >= MAX_POLLS) return setGaveUp(true); await wait(); try { const id: string = week.analysis.id; const analysis: Analysis = await api(`/analyses/${id}`); if (stopped) return; week = { ...week, analysis }; putWeek(week); } catch { // try again on the next tick } } }; (async () => { try { const page = await api(`/weekly/history${query}`); if (stopped) return; setData(page); if (page.month === page.current_month) await follow(); } catch (e) { if (!stopped) setError(errorText(e)); } })(); return () => { stopped = true; clearTimeout(timer); }; }, [query, round]); if (error) { return (

{error}

); } if (!data) { return

Betöltés…

; } const restarted = (week: Week) => (a: Analysis) => { setGaveUp(false); setData((d) => d ? { ...d, weeks: d.weeks.map((w) => (w.week_start === week.week_start ? { ...w, analysis: a } : w)) } : d, ); if (week.week_start === data.this_week) setRound((r) => r + 1); }; const pager = ( ); return (
{pager} {data.weeks.length === 0 ? (

Ebben a hónapban nem nézted meg a heti horoszkópodat.

) : ( data.weeks.map((w) => ( )) )} {(data.older || data.newer) && data.weeks.length > 1 && pager}
); }