Refresh airing TV series when the calendar is opened
- POST /entries/sync-series starts a background refresh of airing TV series (not Ended/Canceled, auto sync on) not refreshed in the last 24h; manual refreshes reset that clock. GET kept as an alias, plus GET /entries/sync-series/status for progress - Reuses the rate-limited stale refresh job with a series-only scope; only one refresh job runs at a time - Calendar starts the sync on visit, shows progress, and reloads episodes and the library when it finishes - Remove the old SyncSeriesData, whose date check no longer worked
This commit is contained in:
1 parent
a88423163e
commit
ac0030d611
8 files changed
+119
-214
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { api } from "@/lib/api";
|
||||
import { normalizeEntry, normalizeCredit } from "@/lib/normalizers";
|
||||
import type { StaleRefreshJob } from "@/api/maintenance";
|
||||
import type {
|
||||
Entry,
|
||||
Credit,
|
||||
@@ -90,7 +91,14 @@ export const entriesApi = {
|
||||
addArtwork: (id: number, data: { type: ArtworkType; file_path: string }) =>
|
||||
api.post<Media>(`/entries/${id}/artwork`, data).then((r) => r.data),
|
||||
|
||||
syncSeries: () => api.get("/entries/sync-series").then((r) => r.data),
|
||||
/** Starts the background refresh of airing TV series older than 24h. */
|
||||
syncSeries: () =>
|
||||
api.post<StaleRefreshJob>("/entries/sync-series").then((r) => r.data),
|
||||
|
||||
syncSeriesStatus: () =>
|
||||
api
|
||||
.get<StaleRefreshJob>("/entries/sync-series/status")
|
||||
.then((r) => r.data),
|
||||
|
||||
previewTmdb: (tmdbId: number, type: "movie" | "tv") =>
|
||||
api
|
||||
|
||||
@@ -13,6 +13,8 @@ export interface OrphanPeopleResult {
|
||||
|
||||
export interface StaleRefreshJob {
|
||||
running: boolean;
|
||||
/** "series" = calendar sync of airing TV series, "all" = maintenance refresh */
|
||||
scope?: "series" | "all";
|
||||
hours?: number;
|
||||
total?: number;
|
||||
done?: number;
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { entriesApi } from "@/api/entries";
|
||||
import { entryKeys } from "@/hooks/useEntries";
|
||||
|
||||
/**
|
||||
* Refreshes airing TV series when the calendar is opened. The backend only
|
||||
* picks series not refreshed in the last 24h (a manual refresh resets that),
|
||||
* so most visits start nothing. While a sync runs its status is polled, and
|
||||
* calendar episodes + the library are reloaded when it finishes.
|
||||
*/
|
||||
export function useSeriesSync() {
|
||||
const queryClient = useQueryClient();
|
||||
const start = useMutation({ mutationFn: entriesApi.syncSeries });
|
||||
const startedRef = useRef(false);
|
||||
|
||||
// Kick off once per visit (StrictMode's second call just sees it running)
|
||||
useEffect(() => {
|
||||
if (startedRef.current) return;
|
||||
startedRef.current = true;
|
||||
start.mutate();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
const startedJob = start.data;
|
||||
const status = useQuery({
|
||||
queryKey: ["entries", "sync-series", "status"],
|
||||
queryFn: entriesApi.syncSeriesStatus,
|
||||
enabled: !!startedJob?.running,
|
||||
refetchInterval: (query) =>
|
||||
query.state.data?.running === false ? false : 2500,
|
||||
});
|
||||
|
||||
const job = status.data ?? startedJob;
|
||||
const running = !!job?.running;
|
||||
|
||||
// Reload calendar data once the sync that was running finishes
|
||||
const wasRunning = useRef(false);
|
||||
useEffect(() => {
|
||||
if (wasRunning.current && !running) {
|
||||
queryClient.invalidateQueries({ queryKey: ["episodes"] });
|
||||
queryClient.invalidateQueries({ queryKey: entryKeys.all, exact: true });
|
||||
}
|
||||
wasRunning.current = running;
|
||||
}, [running, queryClient]);
|
||||
|
||||
return { job, running };
|
||||
}
|
||||
@@ -10,7 +10,13 @@ import {
|
||||
subMonths,
|
||||
isSameDay,
|
||||
} from "date-fns";
|
||||
import { ChevronLeft, ChevronRight, LayoutGrid, List } from "lucide-react";
|
||||
import {
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
LayoutGrid,
|
||||
List,
|
||||
Loader2,
|
||||
} from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
@@ -19,6 +25,7 @@ import {
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { useEpisodesByMonth } from "@/hooks/useEpisodes";
|
||||
import { useSeriesSync } from "@/hooks/useSeriesSync";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
@@ -46,6 +53,8 @@ export function CalendarPage() {
|
||||
const endDateStr = format(monthEnd, "yyyy-MM-dd");
|
||||
|
||||
const { data: episodes = [] } = useEpisodesByMonth(startDateStr, endDateStr);
|
||||
// Refresh airing TV series (at most every 24h each) so air dates are current
|
||||
const { job: syncJob, running: syncRunning } = useSeriesSync();
|
||||
|
||||
const days = useMemo(
|
||||
() => eachDayOfInterval({ start: monthStart, end: monthEnd }),
|
||||
@@ -69,7 +78,26 @@ export function CalendarPage() {
|
||||
return (
|
||||
<div className="flex flex-col h-[calc(100vh-3rem)]">
|
||||
<div className="flex items-center justify-between mb-4 shrink-0">
|
||||
<h1 className="text-2xl font-bold">Calendar</h1>
|
||||
<div className="flex min-w-0 items-center gap-3">
|
||||
<h1 className="text-2xl font-bold">Calendar</h1>
|
||||
{syncRunning && syncJob?.total ? (
|
||||
<span
|
||||
className="inline-flex min-w-0 items-center gap-1.5 rounded-full border border-border/60 bg-muted/40 px-2.5 py-0.5 text-xs text-muted-foreground"
|
||||
title="Refreshing TV series not updated in the last 24 hours"
|
||||
>
|
||||
<Loader2 className="h-3 w-3 shrink-0 animate-spin" />
|
||||
<span className="tabular-nums">
|
||||
{syncJob.scope === "all" ? "Refreshing library" : "Updating series"}{" "}
|
||||
{syncJob.done ?? 0}/{syncJob.total}
|
||||
</span>
|
||||
{syncJob.current?.title && (
|
||||
<span className="hidden max-w-40 truncate sm:inline">
|
||||
· {syncJob.current.title}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
|
||||
Reference in new issue
Block a user