Refactor AIAgentConfigWorkbench for improved readability and maintainability by removing unused memoization and enhancing button accessibility

This commit is contained in:
mlogclub
2026-06-23 21:03:27 +08:00
parent 188c6b6f6b
commit 622cfeef59
@@ -207,11 +207,6 @@ export function AIAgentConfigWorkbench({
const [directToolGroupToAdd, setDirectToolGroupToAdd] = useState("") const [directToolGroupToAdd, setDirectToolGroupToAdd] = useState("")
const [directToolToAdd, setDirectToolToAdd] = useState("") const [directToolToAdd, setDirectToolToAdd] = useState("")
const editorKey = useMemo(
() => `${workflow?.id ?? "new"}-${workflow?.updatedAt ?? ""}`,
[workflow?.id, workflow?.updatedAt]
)
useEffect(() => { useEffect(() => {
setCurrentAgentId(agentId ?? null) setCurrentAgentId(agentId ?? null)
}, [agentId]) }, [agentId])
@@ -500,15 +495,32 @@ export function AIAgentConfigWorkbench({
if (!currentAgentId) return if (!currentAgentId) return
setSavingWorkflow(true) setSavingWorkflow(true)
try { try {
await saveAIAgentWorkflow({ const saved = await saveAIAgentWorkflow({
agentId: currentAgentId, agentId: currentAgentId,
name: "", name: "",
description: "", description: "",
definition, definition,
}) })
setWorkflow(saved)
const version = await publishAIAgentWorkflow(currentAgentId, definition) const version = await publishAIAgentWorkflow(currentAgentId, definition)
toast.success(`Published version ${version.version}`) toast.success(`Published version ${version.version}`)
await loadData() setAgent((current) =>
current ? { ...current, workflowVersionId: version.id } : current
)
setWorkflow((current) =>
current ? { ...current, publishedVersionId: version.id } : saved
)
if (saved.id > 0) {
const versionPage = await fetchAIWorkflowVersions({
workflowId: saved.id,
limit: 20,
})
setWorkflowVersions(versionPage.results ?? [])
} else {
setWorkflowVersions((current) =>
current.some((item) => item.id === version.id) ? current : [version, ...current]
)
}
onAgentSaved?.() onAgentSaved?.()
} catch (error) { } catch (error) {
toast.error(error instanceof Error ? error.message : "Failed to publish workflow") toast.error(error instanceof Error ? error.message : "Failed to publish workflow")
@@ -553,26 +565,49 @@ export function AIAgentConfigWorkbench({
{validation.valid ? "校验通过" : `${validation.errors.length} 个问题`} {validation.valid ? "校验通过" : `${validation.errors.length} 个问题`}
</Badge> </Badge>
) : null} ) : null}
<Button variant="outline" disabled={savingWorkflow || loading || !currentAgentId} onClick={validateWorkflowDraft}> <Button
type="button"
variant="outline"
disabled={savingWorkflow || loading || !currentAgentId}
onClick={validateWorkflowDraft}
>
<CheckCircle2Icon className="size-4" /> <CheckCircle2Icon className="size-4" />
</Button> </Button>
<Button variant="outline" disabled={savingWorkflow || loading || !currentAgentId} onClick={saveWorkflowDraft}> <Button
type="button"
variant="outline"
disabled={savingWorkflow || loading || !currentAgentId}
onClick={saveWorkflowDraft}
>
<SaveIcon className="size-4" /> <SaveIcon className="size-4" />
稿 稿
</Button> </Button>
<Button disabled={savingWorkflow || loading || !currentAgentId} onClick={publishWorkflow}> <Button
type="button"
disabled={savingWorkflow || loading || !currentAgentId}
onClick={publishWorkflow}
>
<SendIcon className="size-4" /> <SendIcon className="size-4" />
</Button> </Button>
</> </>
) : ( ) : (
<> <>
<Button variant="outline" disabled={savingAgent || loading} onClick={saveAgentSettings}> <Button
type="button"
variant="outline"
disabled={savingAgent || loading}
onClick={saveAgentSettings}
>
<SaveIcon className="size-4" /> <SaveIcon className="size-4" />
</Button> </Button>
<Button disabled={savingWorkflow || loading || !currentAgentId} onClick={publishWorkflow}> <Button
type="button"
disabled={savingWorkflow || loading || !currentAgentId}
onClick={publishWorkflow}
>
<SendIcon className="size-4" /> <SendIcon className="size-4" />
</Button> </Button>
@@ -695,10 +730,17 @@ export function AIAgentConfigWorkbench({
<div key={option.value} className="flex items-center gap-2"> <div key={option.value} className="flex items-center gap-2">
<Badge variant="secondary" className="min-w-8 justify-center">{index + 1}</Badge> <Badge variant="secondary" className="min-w-8 justify-center">{index + 1}</Badge>
<div className="flex-1 text-sm">{option.label}</div> <div className="flex-1 text-sm">{option.label}</div>
<Button variant="outline" size="icon-sm" disabled={index === 0} onClick={() => moveKnowledge(index, -1)}> <Button
type="button"
variant="outline"
size="icon-sm"
disabled={index === 0}
onClick={() => moveKnowledge(index, -1)}
>
<ArrowUpIcon /> <ArrowUpIcon />
</Button> </Button>
<Button <Button
type="button"
variant="outline" variant="outline"
size="icon-sm" size="icon-sm"
disabled={index === selectedKnowledgeOptions.length - 1} disabled={index === selectedKnowledgeOptions.length - 1}
@@ -707,6 +749,7 @@ export function AIAgentConfigWorkbench({
<ArrowDownIcon /> <ArrowDownIcon />
</Button> </Button>
<Button <Button
type="button"
variant="outline" variant="outline"
size="icon-sm" size="icon-sm"
onClick={() => setSelectedKnowledgeIds((current) => current.filter((id) => id !== Number(option.value)))} onClick={() => setSelectedKnowledgeIds((current) => current.filter((id) => id !== Number(option.value)))}
@@ -761,7 +804,12 @@ export function AIAgentConfigWorkbench({
addDirectTool(value) addDirectTool(value)
}} }}
/> />
<Button variant="outline" disabled={!directToolToAdd} onClick={() => addDirectTool(directToolToAdd)}> <Button
type="button"
variant="outline"
disabled={!directToolToAdd}
onClick={() => addDirectTool(directToolToAdd)}
>
</Button> </Button>
</div> </div>
@@ -791,7 +839,6 @@ export function AIAgentConfigWorkbench({
{activeSection === "workflow" ? ( {activeSection === "workflow" ? (
<WorkflowEditor <WorkflowEditor
key={editorKey}
definition={definition} definition={definition}
nodeSpecs={nodeSpecs} nodeSpecs={nodeSpecs}
onDefinitionChange={setDefinition} onDefinitionChange={setDefinition}
@@ -849,9 +896,9 @@ export function AIAgentConfigWorkbench({
<div className="space-y-3"> <div className="space-y-3">
<div> <div>
<h3 className="text-sm font-medium"></h3> <h3 className="text-sm font-medium"></h3>
<p className="mt-1 text-xs text-muted-foreground"> {/* <p className="mt-1 text-xs text-muted-foreground">
仅展示已发布的不可变流程版本,当前页面暂不支持切换或回滚版本。 仅展示已发布的不可变流程版本,当前页面暂不支持切换或回滚版本。
</p> </p> */}
</div> </div>
<div className="overflow-hidden rounded-md border"> <div className="overflow-hidden rounded-md border">
{workflowVersions.length > 0 ? ( {workflowVersions.length > 0 ? (