- library list (kind/status/search, cursor paging) and detail: paths and roots, external identity mapping with lock, field-level metadata sources and locks, slot cards with rejection reasons, content units (provisional flag), downloads with verification; remap/lock/refresh/merge actions - subscriptions: list/create/update, slot rules editor (hard/soft/upgrade/completion), policy revision draft -> preview -> publish (grant warning) -> rollback - preferences: tag CRUD, disable/enable, suppression records and lift, history timeline - inbox: grouped by entity and kind, evidence/suggestion/impact/preview, resolve options, SSE refresh - identity: pool assign/dismiss/retry, external identity revisions and lock, title alias management - all responses validated against contracts zod schemas Co-Authored-By: Claude Opus 5.5 <[email protected]>
66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import {
|
|
cursorPage,
|
|
MediaEntity,
|
|
Subscription,
|
|
type SubscriptionCreate,
|
|
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 };
|
|
|
|
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),
|
|
});
|
|
}
|