超级编导的数据中心里已经有每条分发作品的抖音链接,不用再一条条粘。按 「分发活动创建时间」选一段区间 → 预览作品/博主/播放数据 → 一键建成作品 模式的监控任务,建完默认立刻采一轮,主页信息第一轮就补全。 - 活动列表不支持日期过滤(传了也被静默忽略),只能全量拉下来本地按 created_at 筛;边界按东八区算 —— 容器跑 UTC,按 localtime 会整体错开 8 小时。 - data_center 必须带 video_status=-1,不带默认 0,接口返回空列表。 - share_url 是双斜杠形态,建任务时只取裸作品 id;video_link 的 id 提取 也顺带认双斜杠了。 - 作品模式不拉创作者资料、没有账号快照,组头的主页链接一直出不来。 作品上新增 creator_id(author.sec_uid),优先快照、再用作品兜底。 - token 存设置页(secret),不进仓库。
622 lines
21 KiB
TypeScript
622 lines
21 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
import { monitorApi, settingsApi } from '@/lib/api'
|
|
import { usePlatformStore } from '@/store/platformStore'
|
|
import type { SuperDirTaskPayload, TaskCreatePayload } from '@/types/monitor'
|
|
|
|
/**
|
|
* The platform every monitor hook is scoped to.
|
|
*
|
|
* Read from the store rather than threaded through props, so switching the
|
|
* global platform switcher re-scopes every view at once and no component can
|
|
* forget to pass it along.
|
|
*/
|
|
function usePlatformParam(): string {
|
|
return usePlatformStore((state) => state.platform)
|
|
}
|
|
|
|
/** Polling cadence for the dashboard. Runs take minutes, so a few seconds of
|
|
* lag is fine and keeps the number of requests low. */
|
|
const POLL_MS = 5000
|
|
|
|
export function useMonitorOverview() {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorOverview', platform],
|
|
queryFn: async () => (await monitorApi.getOverview(platform)).data,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
export function useMonitorTasks() {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorTasks', platform],
|
|
queryFn: async () => (await monitorApi.getTasks(platform)).data.tasks,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
export function useCreateTask() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (payload: TaskCreatePayload) => monitorApi.createTask(payload),
|
|
onSuccess: () => {
|
|
toast.success('监控任务已创建')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorTasks'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorOverview'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`创建失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useUpdateTask() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ id, payload }: { id: number; payload: Partial<TaskCreatePayload> }) =>
|
|
monitorApi.updateTask(id, payload),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['monitorTasks'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`更新失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useDeleteTask() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: number) => monitorApi.deleteTask(id),
|
|
onSuccess: () => {
|
|
toast.success('监控任务已删除')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorTasks'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorOverview'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`删除失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useRunTaskNow() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: number) => monitorApi.runTaskNow(id),
|
|
onSuccess: () => {
|
|
// The crawl itself runs in the background; the run list picks it up on poll.
|
|
toast.success('已排队执行,可在下方运行历史查看进度')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorRuns'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`启动失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useTaskRuns(taskId: number | null) {
|
|
return useQuery({
|
|
queryKey: ['monitorRuns', taskId],
|
|
queryFn: async () => (await monitorApi.getRuns(taskId as number)).data.runs,
|
|
enabled: taskId !== null,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 作品**和博主**一起取。
|
|
*
|
|
* 博主不能从作品推出来 —— 「一条作品都没有的博主」在作品表里根本不存在,而它恰恰是
|
|
* 最该显示的一类。所以服务端单独给一份 `creators`,两者必须来自同一次请求,否则
|
|
* 组头和组里的作品会对不上(比如刚删掉一个任务)。
|
|
*/
|
|
export function useMonitorNotes(
|
|
taskId: number | null,
|
|
onlyNew: boolean,
|
|
tagIds: number[] = [],
|
|
) {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorNotes', platform, taskId, onlyNew, tagIds],
|
|
queryFn: async () =>
|
|
(await monitorApi.getNotes(taskId ?? undefined, onlyNew, 200, platform, tagIds)).data,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
// --- 标签 ------------------------------------------------------------------
|
|
|
|
export function useMonitorTags() {
|
|
return useQuery({
|
|
queryKey: ['monitorTags'],
|
|
queryFn: async () => (await monitorApi.listTags()).data.tags,
|
|
// 词表是人手工维护的,没有别的写入方,不用频繁刷。
|
|
staleTime: 60_000,
|
|
})
|
|
}
|
|
|
|
/** 标签变了要让作品列表重取 —— 标签是画在作品行上的。 */
|
|
function useTagMutation<TArgs>(
|
|
mutationFn: (args: TArgs) => Promise<unknown>,
|
|
successMessage: string,
|
|
) {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn,
|
|
onSuccess: () => {
|
|
toast.success(successMessage)
|
|
queryClient.invalidateQueries({ queryKey: ['monitorTags'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorNotes'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`操作失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useCreateTag() {
|
|
return useTagMutation(
|
|
({ name, color, keywords }: { name: string; color: string; keywords: string }) =>
|
|
monitorApi.createTag(name, color, keywords),
|
|
'标签已创建',
|
|
)
|
|
}
|
|
|
|
export function useUpdateTag() {
|
|
return useTagMutation(
|
|
({ id, name, color, keywords }: { id: number; name: string; color: string; keywords: string }) =>
|
|
monitorApi.updateTag(id, name, color, keywords),
|
|
'标签已保存',
|
|
)
|
|
}
|
|
|
|
export function useDeleteTag() {
|
|
return useTagMutation((id: number) => monitorApi.deleteTag(id), '标签已删除')
|
|
}
|
|
|
|
export function useSetNoteTags() {
|
|
const queryClient = useQueryClient()
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: ({ noteId, tagIds }: { noteId: string; tagIds: number[] }) =>
|
|
monitorApi.setNoteTags(noteId, tagIds, platform),
|
|
onSuccess: () => {
|
|
// 不打 toast:贴标签是个连着点好几次的动作,每次都弹一条会把屏幕刷满。
|
|
queryClient.invalidateQueries({ queryKey: ['monitorNotes'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorTags'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`标签保存失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useNoteSeries(noteId: string | null, taskId: number | null) {
|
|
return useQuery({
|
|
queryKey: ['monitorNoteSeries', noteId, taskId],
|
|
queryFn: async () => (await monitorApi.getNoteSeries(noteId as string, taskId ?? undefined)).data.series,
|
|
enabled: noteId !== null,
|
|
staleTime: 30_000,
|
|
})
|
|
}
|
|
|
|
export function useMonitorComments(taskId: number | null, noteId?: string | null) {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorComments', platform, taskId, noteId ?? null],
|
|
queryFn: async () =>
|
|
(await monitorApi.getComments(taskId ?? undefined, noteId ?? undefined, 200, platform)).data
|
|
.comments,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
export function useMonitorCommentsGrouped(taskId: number | null, noteId?: string | null) {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorCommentsGrouped', platform, taskId, noteId ?? null],
|
|
queryFn: async () =>
|
|
(await monitorApi.getCommentsGrouped(taskId ?? undefined, noteId ?? undefined, 200, platform))
|
|
.data.groups,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
export function useCommentNoteOptions(taskId: number | null) {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorCommentNotes', platform, taskId],
|
|
queryFn: async () =>
|
|
(await monitorApi.getCommentNotes(taskId ?? undefined, platform)).data.notes,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
export function useMonitorEvents(taskId: number | null) {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorEvents', platform, taskId],
|
|
queryFn: async () =>
|
|
(await monitorApi.getEvents(taskId ?? undefined, 200, platform)).data.events,
|
|
refetchInterval: POLL_MS,
|
|
})
|
|
}
|
|
|
|
export function useMarkEventsRead() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (taskId?: number) => monitorApi.markEventsRead(taskId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['monitorEvents'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorTasks'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorOverview'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useCookieStatus() {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorCookie', platform],
|
|
queryFn: async () => (await monitorApi.getCookieStatus(platform)).data,
|
|
refetchInterval: 15000,
|
|
})
|
|
}
|
|
|
|
export function useSetCookie() {
|
|
const queryClient = useQueryClient()
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: (cookie: string) => monitorApi.setCookie(cookie, platform),
|
|
onSuccess: () => {
|
|
toast.success('Cookie 已保存')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorCookie'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`保存失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useClearCookie() {
|
|
const queryClient = useQueryClient()
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: () => monitorApi.clearCookie(platform),
|
|
onSuccess: () => {
|
|
toast.success('Cookie 已清除')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorCookie'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// --- QR login (over CDP) ---------------------------------------------------
|
|
|
|
/** Polls only while a code is on screen; an idle session has nothing to watch. */
|
|
export function useQrLoginStatus(polling: boolean) {
|
|
return useQuery({
|
|
queryKey: ['monitorQrLogin'],
|
|
queryFn: async () => (await monitorApi.getQrLogin()).data,
|
|
refetchInterval: polling ? 2000 : false,
|
|
})
|
|
}
|
|
|
|
export function useStartQrLogin() {
|
|
const queryClient = useQueryClient()
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: () => monitorApi.startQrLogin(platform),
|
|
onSuccess: (response) => {
|
|
queryClient.setQueryData(['monitorQrLogin'], response.data)
|
|
},
|
|
onError: (error: Error) => {
|
|
// The backend's reason -- "Chrome is not reachable on ...", "no QR on the
|
|
// page" -- is the entire value here, and axios would flatten it to
|
|
// "Request failed with status code 502".
|
|
const detail = (error as { response?: { data?: { detail?: string } } })?.response
|
|
?.data?.detail
|
|
toast.error(`获取二维码失败:${detail ?? error.message}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useCancelQrLogin() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: () => monitorApi.cancelQrLogin(),
|
|
onSuccess: (response) => {
|
|
queryClient.setQueryData(['monitorQrLogin'], response.data)
|
|
},
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Whether the browser is actually signed in.
|
|
*
|
|
* Tracked separately from the QR session above: that session lives in the
|
|
* server's memory and a restart erases it, while the profile it wrote to does
|
|
* not. Polled slowly when idle and briskly while a code is on screen.
|
|
*/
|
|
export function useLoginState(polling: boolean) {
|
|
return useQuery({
|
|
queryKey: ['monitorLoginState'],
|
|
queryFn: async () => (await monitorApi.getLoginState()).data,
|
|
refetchInterval: polling ? 4000 : 30000,
|
|
refetchOnWindowFocus: false,
|
|
})
|
|
}
|
|
|
|
/** Re-ask after reloading the page, for when the state looks stale. */
|
|
export function useRecheckLogin() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: () => monitorApi.getLoginState(true),
|
|
onSuccess: (response) => {
|
|
queryClient.setQueryData(['monitorLoginState'], response.data)
|
|
},
|
|
onError: (error: Error) => toast.error(`检测失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- Settings -------------------------------------------------------------
|
|
|
|
export function useSettings() {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['settings', platform],
|
|
queryFn: async () => (await settingsApi.getAll(platform)).data,
|
|
staleTime: 30_000,
|
|
})
|
|
}
|
|
|
|
export function useUpdateSettings() {
|
|
const queryClient = useQueryClient()
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: (values: Record<string, unknown>) => settingsApi.update(values, platform),
|
|
onSuccess: (response) => {
|
|
toast.success(`已保存 ${response.data.changed.length} 项设置`)
|
|
queryClient.invalidateQueries({ queryKey: ['settings'] })
|
|
// Cookie / webhook panels read their own keys.
|
|
queryClient.invalidateQueries({ queryKey: ['monitorCookie'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorWebhook'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`保存失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- Report ---------------------------------------------------------------
|
|
|
|
export function useReport(taskIds: number[], startDate: string, endDate: string) {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorReport', platform, taskIds, startDate, endDate],
|
|
queryFn: async () =>
|
|
(await monitorApi.getReport(taskIds, startDate, endDate, platform)).data,
|
|
// Reports are read-only aggregates; no need to poll them every few seconds.
|
|
staleTime: 30_000,
|
|
})
|
|
}
|
|
|
|
// --- WeCom webhook --------------------------------------------------------
|
|
|
|
export function useWebhookStatus() {
|
|
return useQuery({
|
|
queryKey: ['monitorWebhook'],
|
|
queryFn: async () => (await monitorApi.getWebhook()).data,
|
|
refetchInterval: 30_000,
|
|
})
|
|
}
|
|
|
|
export function useSetWebhook() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (url: string) => monitorApi.setWebhook(url),
|
|
onSuccess: () => {
|
|
toast.success('Webhook 已保存')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorWebhook'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`保存失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useClearWebhook() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: () => monitorApi.clearWebhook(),
|
|
onSuccess: () => {
|
|
toast.success('Webhook 已删除')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorWebhook'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useTestWebhook() {
|
|
return useMutation({
|
|
mutationFn: (url?: string) => monitorApi.testWebhook(url),
|
|
onSuccess: () => toast.success('测试消息已发送,请查看企业微信群'),
|
|
onError: (error: Error) => toast.error(`发送失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
/** 手动推一次「昨天的视频情况」图。定时那条由后端调度器负责。 */
|
|
export function useSendDailyDigest() {
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: async () => (await monitorApi.sendDailyDigest(platform)).data,
|
|
onSuccess: (result) =>
|
|
toast.success(`已发送昨天的视频情况图(${result.rows} 条),请查看企业微信群`),
|
|
onError: (error: Error) => toast.error(`发送失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- 视频链接 → 博主主页 ---------------------------------------------------
|
|
|
|
export function useResolveVideoLinks() {
|
|
return useMutation({
|
|
mutationFn: async (text: string) => (await monitorApi.resolveVideoLinks(text)).data,
|
|
// 单条失败在结果里各自有 error,这里只管整批都没走到的情况(网络、鉴权)。
|
|
onError: (error: Error) => toast.error(`解析失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- 超级编导导入 ----------------------------------------------------------
|
|
|
|
/**
|
|
* 按创建时间区间查超级编导上的作品链接。
|
|
*
|
|
* 是 mutation 不是 query:点「查询」才发请求,日期还没选好、打开弹窗的那一刻不该
|
|
* 就去翻一遍全部活动 —— 那个接口没有日期过滤,每次都挺贵。
|
|
*/
|
|
export function useSuperDirPreview() {
|
|
return useMutation({
|
|
mutationFn: async ({ start, end }: { start: string; end: string }) =>
|
|
(await monitorApi.previewSuperDir(start, end)).data,
|
|
onError: (error: Error) => toast.error(`查询失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useCreateSuperDirTask() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: async (payload: SuperDirTaskPayload) =>
|
|
(await monitorApi.createSuperDirTask(payload)).data,
|
|
onSuccess: (result) => {
|
|
toast.success(`已建监控任务,共 ${result.targets} 条作品`)
|
|
queryClient.invalidateQueries({ queryKey: ['monitorTasks'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorOverview'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`建任务失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- 博主备注 --------------------------------------------------------------
|
|
|
|
export function useSetCreatorAlias() {
|
|
const queryClient = useQueryClient()
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: ({ creatorHash, alias }: { creatorHash: string; alias: string }) =>
|
|
monitorApi.setCreatorAlias(creatorHash, alias, platform),
|
|
onSuccess: () => {
|
|
toast.success('备注已保存')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorNotes'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorCommentsGrouped'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`备注保存失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- 作品备注 --------------------------------------------------------------
|
|
|
|
export function useSetNoteAlias() {
|
|
const queryClient = useQueryClient()
|
|
const platform = usePlatformParam()
|
|
return useMutation({
|
|
mutationFn: ({ noteId, alias }: { noteId: string; alias: string }) =>
|
|
monitorApi.setNoteAlias(noteId, alias, platform),
|
|
onSuccess: () => {
|
|
toast.success('备注已保存')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorNotes'] })
|
|
queryClient.invalidateQueries({ queryKey: ['monitorCommentsGrouped'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`备注保存失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- 上游更新检查 -----------------------------------------------------------------------------------------------------------------
|
|
|
|
export function useUpstreamStatus() {
|
|
return useQuery({
|
|
queryKey: ['monitorUpstream'],
|
|
queryFn: async () => (await monitorApi.getUpstream()).data,
|
|
// 检查本身是每天一次的量级,页面开着时慢点刷就够了。
|
|
refetchInterval: 60_000,
|
|
})
|
|
}
|
|
|
|
export function useCheckUpstream() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: () => monitorApi.checkUpstream(),
|
|
onSuccess: (response) => {
|
|
const data = response.data
|
|
if (!data.ok) {
|
|
toast.error(`检查失败:${data.error ?? '未知错误'}`)
|
|
} else if ((data.behind ?? 0) > 0) {
|
|
toast.success(`上游有 ${data.behind} 个新提交`)
|
|
} else {
|
|
toast.success('已是最新,上游没有新提交')
|
|
}
|
|
queryClient.invalidateQueries({ queryKey: ['monitorUpstream'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`检查失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
// --- 身份池(抖音多登录态) ------------------------------------------------
|
|
|
|
export function useMonitorIdentities() {
|
|
const platform = usePlatformParam()
|
|
return useQuery({
|
|
queryKey: ['monitorIdentities', platform],
|
|
queryFn: async () => (await monitorApi.listIdentities(platform)).data.identities,
|
|
// 状态会随着采集轮次变化,慢点刷就够了。
|
|
refetchInterval: 15_000,
|
|
})
|
|
}
|
|
|
|
export function useCreateIdentity() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (payload: {
|
|
label: string
|
|
cookie: string
|
|
user_agent?: string
|
|
platform?: string
|
|
}) => monitorApi.createIdentity(payload),
|
|
onSuccess: () => {
|
|
toast.success('身份已添加')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
|
},
|
|
onError: (error: Error) => {
|
|
// 后端会把「拿不到 UA」这类原因写在 detail 里,axios 默认会把它压成状态码。
|
|
const detail = (error as { response?: { data?: { detail?: string } } })?.response
|
|
?.data?.detail
|
|
toast.error(`添加失败:${detail ?? error.message}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useUpdateIdentity() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
payload,
|
|
}: {
|
|
id: number
|
|
payload: Partial<{ label: string; cookie: string; user_agent: string; enabled: boolean }>
|
|
}) => monitorApi.updateIdentity(id, payload),
|
|
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] }),
|
|
onError: (error: Error) => toast.error(`更新失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useDeleteIdentity() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: number) => monitorApi.deleteIdentity(id),
|
|
onSuccess: () => {
|
|
toast.success('身份已删除')
|
|
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`删除失败:${error.message}`),
|
|
})
|
|
}
|
|
|
|
export function useCheckIdentity() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: number) => monitorApi.checkIdentity(id),
|
|
onSuccess: (response) => {
|
|
const data = response.data
|
|
if (data.ok) {
|
|
toast.success(data.verified ? '验证通过' : '格式检查通过(没有可探测的博主)')
|
|
} else {
|
|
toast.error(`不可用:${data.error || '未知原因'}`)
|
|
}
|
|
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
|
},
|
|
onError: (error: Error) => toast.error(`验证失败:${error.message}`),
|
|
})
|
|
}
|