- 后端新增 featured_order 字段及精选排序 API(/agents/selector、/agents/{id}/featured、/agents/featured/order),移除 user_agent_pin 表
- 前端新对话页 (ChatPage) 与输入框 (InputBox) 接入 agent-selector,选中后跳转 AgentChatPage 并通过 sessionStorage 透传首条消息
- 新增管理员智能体管理弹窗 (agent-manage-dialog) 支持精选添加/移除/上下移动
113 lines
3.0 KiB
TypeScript
113 lines
3.0 KiB
TypeScript
import {
|
|
keepPreviousData,
|
|
useMutation,
|
|
useQuery,
|
|
useQueryClient,
|
|
} from "@tanstack/react-query";
|
|
|
|
import {
|
|
createAgent,
|
|
deleteAgent,
|
|
getAgent,
|
|
getAgentSelector,
|
|
listAgents,
|
|
reorderFeaturedAgents,
|
|
setAgentFeatured,
|
|
updateAgent,
|
|
} from "./api";
|
|
import type { CreateAgentRequest, UpdateAgentRequest } from "./types";
|
|
|
|
export function useAgents(params: { search?: string; tagIds?: string[] } = {}) {
|
|
const search = params.search ?? "";
|
|
const tagIds = params.tagIds ?? [];
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["agents", search, [...tagIds].sort()],
|
|
queryFn: () => listAgents({ search, tagIds }),
|
|
placeholderData: keepPreviousData,
|
|
});
|
|
return { agents: data ?? [], isLoading, error };
|
|
}
|
|
|
|
export function useAgent(id: string | null | undefined) {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["agents", id],
|
|
queryFn: () => getAgent(id!),
|
|
enabled: !!id,
|
|
});
|
|
return { agent: data ?? null, isLoading, error };
|
|
}
|
|
|
|
export function useCreateAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (request: CreateAgentRequest) => createAgent(request),
|
|
onSuccess: () => {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
request,
|
|
}: {
|
|
id: string;
|
|
request: UpdateAgentRequest;
|
|
}) => updateAgent(id, request),
|
|
onSuccess: (_data, { id }) => {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
void queryClient.invalidateQueries({ queryKey: ["agents", id] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => deleteAgent(id),
|
|
onSuccess: () => {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAgentSelector() {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["agents", "selector"],
|
|
queryFn: () => getAgentSelector(),
|
|
staleTime: 60_000,
|
|
});
|
|
return {
|
|
agents: data?.agents ?? [],
|
|
isLoading,
|
|
error,
|
|
};
|
|
}
|
|
|
|
function invalidateAgentQueries(queryClient: ReturnType<typeof useQueryClient>) {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
}
|
|
|
|
export function useSetAgentFeatured() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, order }: { id: string; order: number | null }) =>
|
|
setAgentFeatured(id, order),
|
|
onSuccess: (_data, { id }) => {
|
|
invalidateAgentQueries(queryClient);
|
|
void queryClient.invalidateQueries({ queryKey: ["agents", id] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useReorderFeaturedAgents() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (agentIds: string[]) => reorderFeaturedAgents(agentIds),
|
|
onSuccess: () => invalidateAgentQueries(queryClient),
|
|
});
|
|
}
|