Files
sorsfonalak/services/frontend/src/app/heti-horoszkop/WeeklyPage.tsx
T

152 lines
4.4 KiB
TypeScript
Raw Normal View History

"use client";
import Link from "next/link";
import { useEffect, useState } from "react";
import { MONTHS, WeekCard, WeeklyEmailSwitch } from "@/components/WeeklyHoroscope";
import { api, errorText } from "@/lib/api";
import type { Analysis } from "@/lib/types";
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), and polled until the
* worker has written it; meanwhile its card shows the waiting animation.
*/
export function WeeklyPage({ month, isAdmin }: { month: string | null; isAdmin: boolean }) {
const [data, setData] = useState<Month | null>(null);
const [error, setError] = useState<string | null>(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<typeof setTimeout> | undefined;
let polls = 0;
const poll = async () => {
try {
const week = await api<Week & { email: boolean }>("/weekly", { method: "POST" });
if (stopped) return;
putWeek({ week_start: week.week_start, week_end: week.week_end, analysis: week.analysis });
if (week.analysis.status === "done") return;
} catch {
// try again on the next tick
}
if (++polls >= MAX_POLLS) return setGaveUp(true);
timer = setTimeout(poll, POLL_MS);
};
(async () => {
try {
const page = await api<Month>(`/weekly/history${query}`);
if (stopped) return;
setData(page);
if (page.month === page.current_month) await poll();
} catch (e) {
if (!stopped) setError(errorText(e));
}
})();
return () => {
stopped = true;
clearTimeout(timer);
};
}, [query, round]);
if (error) {
return (
<p className="msg msg-error" role="alert">
{error}
</p>
);
}
if (!data) {
return <p className="muted">Betöltés…</p>;
}
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 = (
<nav className="weekly-pager" aria-label="Hónapok">
{data.older ? (
<Link href={`/heti-horoszkop?honap=${data.older}`} className="btn btn-sm">
← Korábbi hónap
</Link>
) : (
<span />
)}
<strong>{monthName(data.month)}</strong>
{data.newer ? (
<Link
href={data.newer === data.current_month ? "/heti-horoszkop" : `/heti-horoszkop?honap=${data.newer}`}
className="btn btn-sm"
>
Újabb hónap →
</Link>
) : (
<span />
)}
</nav>
);
return (
<div className="field" style={{ gap: 18 }}>
{pager}
{data.weeks.length === 0 ? (
<p className="muted">Ebben a hónapban nem nézted meg a heti horoszkópodat.</p>
) : (
data.weeks.map((w) => (
<WeekCard
key={w.week_start}
start={w.week_start}
end={w.week_end}
analysis={w.analysis}
current={w.week_start === data.this_week}
gaveUp={w.week_start === data.this_week && gaveUp}
isAdmin={isAdmin}
onRestarted={restarted(w)}
/>
))
)}
{(data.older || data.newer) && data.weeks.length > 1 && pager}
<div className="card">
<WeeklyEmailSwitch initial={data.email} label="Hétfőnként szóljatok e-mailben, ha itt az új heti horoszkópom" />
</div>
</div>
);
}