175 lines
6.5 KiB
TypeScript
175 lines
6.5 KiB
TypeScript
"use client";
|
||
|
||
import { Bot, Loader2 } from "lucide-react";
|
||
import { useEffect, useMemo, useRef, useState } from "react";
|
||
import { toast } from "sonner";
|
||
import { Select } from "tdesign-react";
|
||
|
||
import { Button } from "@/components/ui/button";
|
||
import {
|
||
Dialog,
|
||
DialogContent,
|
||
DialogDescription,
|
||
DialogFooter,
|
||
DialogHeader,
|
||
DialogTitle,
|
||
} from "@/components/ui/dialog";
|
||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||
import { useAgents } from "@/core/agents";
|
||
import type { Agent } from "@/core/agents";
|
||
import {
|
||
useAgentModuleConfig,
|
||
useUpdateAgentModuleConfig,
|
||
} from "@/core/system-settings/hooks";
|
||
import { useTags } from "@/core/tags";
|
||
import { desensitize } from "@/lib/desensitize";
|
||
|
||
/**
|
||
* 模块(zzJC / zzXD)类型配置弹窗(需求 0616 #3/#4,admin)。
|
||
*
|
||
* 单选一个「类型」(agent 标签,TDesign 下拉框)→ 下方实时预览该类型下的智能体(名称+描述)→
|
||
* 确认后写入后端(``/api/system-settings/agent-modules/{key}``,单选即存单元素列表),
|
||
* 该模块左栏据此只展示选中类型的智能体。
|
||
*/
|
||
export function AgentModuleSettingsDialog({
|
||
open,
|
||
onOpenChange,
|
||
moduleKey,
|
||
moduleTitle,
|
||
}: {
|
||
open: boolean;
|
||
onOpenChange: (open: boolean) => void;
|
||
moduleKey: string;
|
||
moduleTitle: string;
|
||
}) {
|
||
const { data: tags = [] } = useTags("agent");
|
||
const { agents } = useAgents();
|
||
const { data: config } = useAgentModuleConfig(moduleKey);
|
||
const { mutate: save, isPending } = useUpdateAgentModuleConfig(moduleKey);
|
||
|
||
const [selectedTagId, setSelectedTagId] = useState<string | undefined>();
|
||
// Radix Dialog(modal) 把 body 设为 pointer-events:none,TDesign 浮层默认挂到 body 上
|
||
// 会点不动 —— 用 attach 把下拉浮层挂到弹窗内部容器(pointer-events 正常)。
|
||
const selectWrapRef = useRef<HTMLDivElement>(null);
|
||
|
||
// 每次打开同步当前已保存配置(取首个,单选语义)。
|
||
useEffect(() => {
|
||
if (open) setSelectedTagId(config?.tag_ids?.[0]);
|
||
}, [open, config?.tag_ids]);
|
||
|
||
const tagOptions = useMemo(
|
||
() => tags.map((t) => ({ label: t.name, value: t.id })),
|
||
[tags],
|
||
);
|
||
|
||
// 选中类型下的智能体(含名称 + 描述)。
|
||
const previewAgents = useMemo<Agent[]>(() => {
|
||
if (!selectedTagId) return [];
|
||
return agents.filter((agent) =>
|
||
(agent.tags ?? []).some((t) => t.id === selectedTagId),
|
||
);
|
||
}, [agents, selectedTagId]);
|
||
|
||
const handleConfirm = () => {
|
||
save(selectedTagId ? [selectedTagId] : [], {
|
||
onSuccess: () => {
|
||
toast.success(`已更新「${moduleTitle}」展示的类型`);
|
||
onOpenChange(false);
|
||
},
|
||
onError: (err) =>
|
||
toast.error(err instanceof Error ? err.message : "保存失败,请重试"),
|
||
});
|
||
};
|
||
|
||
return (
|
||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||
<DialogContent className="flex h-[78vh] max-h-[88vh] min-h-[620px] flex-col gap-0 p-0 sm:max-w-[760px]">
|
||
<DialogHeader className="border-b border-border/60 px-6 py-4">
|
||
<DialogTitle>{moduleTitle} · 类型配置</DialogTitle>
|
||
<DialogDescription>
|
||
选择该模块左栏展示的智能体类型(标签);下方预览该类型下的智能体。
|
||
</DialogDescription>
|
||
</DialogHeader>
|
||
|
||
<div className="flex min-h-0 flex-1 flex-col gap-5 overflow-hidden px-6 py-5">
|
||
<div className="shrink-0">
|
||
<div className="text-muted-foreground/80 mb-2 text-sm font-semibold">
|
||
选择类型
|
||
</div>
|
||
{tags.length === 0 ? (
|
||
<p className="text-muted-foreground py-1 text-sm">
|
||
暂无智能体标签,请先到「标签管理」创建。
|
||
</p>
|
||
) : (
|
||
<div ref={selectWrapRef} className="relative">
|
||
<Select
|
||
className="w-full"
|
||
value={selectedTagId}
|
||
options={tagOptions}
|
||
filterable
|
||
clearable
|
||
placeholder="搜索并选择一个类型"
|
||
popupProps={{
|
||
attach: () => selectWrapRef.current ?? document.body,
|
||
}}
|
||
onChange={(v) => setSelectedTagId((v as string) || undefined)}
|
||
/>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
<div className="flex min-h-0 flex-1 flex-col">
|
||
<div className="text-muted-foreground/80 mb-2 shrink-0 text-sm font-semibold">
|
||
该类型下的智能体({previewAgents.length})
|
||
</div>
|
||
{!selectedTagId ? (
|
||
<p className="text-muted-foreground py-1 text-sm">
|
||
未选择类型时,左栏展示全部可见智能体。
|
||
</p>
|
||
) : previewAgents.length === 0 ? (
|
||
<p className="text-muted-foreground py-1 text-sm">
|
||
该类型下暂无智能体。
|
||
</p>
|
||
) : (
|
||
<ScrollArea className="min-h-0 flex-1 pr-3">
|
||
<div className="flex flex-col gap-2">
|
||
{previewAgents.map((agent) => (
|
||
<div
|
||
key={agent.id}
|
||
className="flex items-start gap-3 rounded-xl border border-border/60 bg-card p-3"
|
||
>
|
||
<div className="agent-avatar bg-primary/10 text-primary mt-0.5 flex size-8 shrink-0 items-center justify-center rounded-lg">
|
||
<Bot className="size-4" />
|
||
</div>
|
||
<div className="min-w-0 flex-1">
|
||
<div className="truncate text-sm font-semibold text-foreground">
|
||
{desensitize(agent.name)}
|
||
</div>
|
||
<p className="text-muted-foreground mt-0.5 line-clamp-2 text-xs leading-relaxed">
|
||
{agent.description?.trim()
|
||
? desensitize(agent.description)
|
||
: "暂无描述"}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
</ScrollArea>
|
||
)}
|
||
</div>
|
||
</div>
|
||
|
||
<DialogFooter className="border-t border-border/60 px-6 py-4">
|
||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||
取消
|
||
</Button>
|
||
<Button onClick={handleConfirm} disabled={isPending}>
|
||
{isPending && <Loader2 className="size-4 animate-spin" />}
|
||
确认
|
||
</Button>
|
||
</DialogFooter>
|
||
</DialogContent>
|
||
</Dialog>
|
||
);
|
||
}
|