- subscription sheet: 立即搜索, active-search times, 追更进度 (per-season total with source/lock, per-episode state chips, in-flight/deferred counts) and set/lock season episode total - labels for subscription pending and candidate decision deferred Co-Authored-By: Claude Opus 5.5 <[email protected]>
103 lines
3.7 KiB
TypeScript
103 lines
3.7 KiB
TypeScript
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import {
|
|
cursorPage,
|
|
type EpisodeTotalSet,
|
|
MediaEntity,
|
|
Subscription,
|
|
type SubscriptionCreate,
|
|
type SubscriptionMediaKind,
|
|
SubscriptionProgress,
|
|
type SubscriptionStatus,
|
|
type SubscriptionUpdate,
|
|
} from "@umt/contracts";
|
|
import { api } from "~/lib/api.ts";
|
|
import { getParsed, parseOrThrow } from "./library.ts";
|
|
|
|
// 订阅:单次收录 / 持续追踪,仅本季 / 接续后续季(REQ §6.5)
|
|
|
|
export const subscriptionKeys = {
|
|
all: ["subscriptions"] as const,
|
|
list: (f: SubscriptionFilters) => ["subscriptions", "list", f] as const,
|
|
};
|
|
|
|
export type SubscriptionFilters = {
|
|
status?: SubscriptionStatus;
|
|
mediaEntityId?: string;
|
|
mediaKind?: SubscriptionMediaKind;
|
|
/** 标题 / 原名 / 别名 / 未建档检索词 */
|
|
q?: string;
|
|
};
|
|
|
|
const Page = cursorPage(Subscription);
|
|
|
|
export function useSubscriptions(filters: SubscriptionFilters) {
|
|
return useInfiniteQuery({
|
|
queryKey: subscriptionKeys.list(filters),
|
|
initialPageParam: undefined as string | undefined,
|
|
queryFn: ({ pageParam, signal }) =>
|
|
getParsed(Page, "/subscriptions", { ...filters, cursor: pageParam, limit: 50 }, signal),
|
|
getNextPageParam: (last) => last.nextCursor ?? undefined,
|
|
placeholderData: keepPreviousData,
|
|
});
|
|
}
|
|
|
|
export function useCreateSubscription() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (body: SubscriptionCreate) =>
|
|
api.post<unknown>("/subscriptions", body).then((d) => parseOrThrow(Subscription, d, "/subscriptions")),
|
|
onSuccess: () => {
|
|
void qc.invalidateQueries({ queryKey: subscriptionKeys.all });
|
|
void qc.invalidateQueries({ queryKey: ["slots"] });
|
|
void qc.invalidateQueries({ queryKey: ["library"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateSubscription() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, ...body }: SubscriptionUpdate & { id: string }) =>
|
|
api.patch<unknown>(`/subscriptions/${id}`, body).then((d) => parseOrThrow(Subscription, d, "/subscriptions")),
|
|
onSuccess: () => void qc.invalidateQueries({ queryKey: subscriptionKeys.all }),
|
|
});
|
|
}
|
|
|
|
/** 订阅行上显示作品标题(旧 ID 自动跳转);按 ID 缓存 */
|
|
export function useEntityTitle(id: string | null | undefined) {
|
|
return useQuery({
|
|
queryKey: ["library", "entity-row", id ?? ""],
|
|
enabled: !!id,
|
|
staleTime: 5 * 60_000,
|
|
queryFn: ({ signal }) => getParsed(MediaEntity, `/media-entities/${id}`, undefined, signal),
|
|
});
|
|
}
|
|
|
|
/** 订阅进度:季总集数账本 + 每集状态(已覆盖/在途/缺失/疑似缺集)+ Slot 在途与延后(W5-C) */
|
|
export function useSubscriptionProgress(id: string | null | undefined, enabled = true) {
|
|
return useQuery({
|
|
queryKey: ["subscriptions", "progress", id ?? ""],
|
|
enabled: !!id && enabled,
|
|
queryFn: ({ signal }) => getParsed(SubscriptionProgress, `/subscriptions/${id}/progress`, undefined, signal),
|
|
});
|
|
}
|
|
|
|
/** 手动主动搜索:按标题/别名在已开启站点搜索,结果走同一识别评估链 */
|
|
export function useSearchSubscription() {
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.post<{ runId: string; deduplicated: boolean }>(`/subscriptions/${id}/search`),
|
|
});
|
|
}
|
|
|
|
/** 手动设置季总集数(不锁定时只升;锁定后元数据刷新不再改写) */
|
|
export function useSetEpisodeTotal() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, ...body }: EpisodeTotalSet & { id: string }) =>
|
|
api
|
|
.post<unknown>(`/subscriptions/${id}/episode-total`, body)
|
|
.then((d) => parseOrThrow(SubscriptionProgress, d, "/subscriptions/episode-total")),
|
|
onSuccess: (_d, v) => void qc.invalidateQueries({ queryKey: ["subscriptions", "progress", v.id] }),
|
|
});
|
|
}
|