feat: refactor workflow configuration components for improved structure and usability

This commit is contained in:
mlogclub
2026-06-27 22:40:27 +08:00
parent 1472c487ac
commit 0d568ebf87
4 changed files with 87 additions and 74 deletions
@@ -591,7 +591,11 @@ export function AIAgentConfigWorkbench({
</div> </div>
</div> </div>
<main className="min-h-0 flex-1 overflow-y-auto bg-background"> <main
className={`min-h-0 flex-1 bg-background ${
activeSection === "workflow" ? "overflow-hidden" : "overflow-y-auto"
}`}
>
{loading ? ( {loading ? (
<div className="flex h-full items-center justify-center text-sm text-muted-foreground"> <div className="flex h-full items-center justify-center text-sm text-muted-foreground">
... ...
@@ -31,6 +31,7 @@ export function NodeConfigPanel({
nodeSpec, nodeSpec,
nodes, nodes,
availableVariables, availableVariables,
showHeader = true,
onChange, onChange,
onDelete, onDelete,
}: { }: {
@@ -39,6 +40,7 @@ export function NodeConfigPanel({
nodes: AIWorkflowDefinition["nodes"] nodes: AIWorkflowDefinition["nodes"]
availableVariables?: WorkflowVariableRef[] availableVariables?: WorkflowVariableRef[]
branchSummaries?: WorkflowBranchSummary[] branchSummaries?: WorkflowBranchSummary[]
showHeader?: boolean
onChange: (nodeId: string, data: AIWorkflowDefinition["nodes"][number]["data"]) => void onChange: (nodeId: string, data: AIWorkflowDefinition["nodes"][number]["data"]) => void
onDelete?: (nodeId: string) => void onDelete?: (nodeId: string) => void
}) { }) {
@@ -104,26 +106,30 @@ export function NodeConfigPanel({
return ( return (
<div className="flex h-full flex-col"> <div className="flex h-full flex-col">
<div className="border-b px-4 py-3"> {showHeader ? (
<div className="flex items-start justify-between gap-2"> <div className="border-b px-4 py-3">
<div className="min-w-0"> <div className="flex items-start justify-between gap-2">
<div className="truncate text-sm font-medium">{node.data?.title || nodeSpec?.title || node.type}</div> <div className="min-w-0">
<div className="mt-1 truncate text-xs text-muted-foreground">{node.id}</div> <div className="truncate text-sm font-medium">
{node.data?.title || nodeSpec?.title || node.type}
</div>
<div className="mt-1 truncate text-xs text-muted-foreground">{node.id}</div>
</div>
{canDelete ? (
<Button
type="button"
variant="ghost"
size="icon"
className="size-8 shrink-0 text-muted-foreground hover:text-destructive"
onClick={() => onDelete?.(node.id)}
aria-label="删除节点"
>
<Trash2Icon className="size-4" />
</Button>
) : null}
</div> </div>
{canDelete ? (
<Button
type="button"
variant="ghost"
size="icon"
className="size-8 shrink-0 text-muted-foreground hover:text-destructive"
onClick={() => onDelete?.(node.id)}
aria-label="删除节点"
>
<Trash2Icon className="size-4" />
</Button>
) : null}
</div> </div>
</div> ) : null}
<div className="min-h-0 flex-1 space-y-5 overflow-y-auto p-4"> <div className="min-h-0 flex-1 space-y-5 overflow-y-auto p-4">
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor={`node-title-${node.id}`}></Label> <Label htmlFor={`node-title-${node.id}`}></Label>
@@ -1,27 +1,28 @@
"use client" "use client"
import { XIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import type { AIWorkflowDefinition, AIWorkflowNodeSpec } from "@/lib/api/admin" import type { AIWorkflowDefinition, AIWorkflowNodeSpec } from "@/lib/api/admin"
import { cn } from "@/lib/utils"
import { NodeConfigPanel } from "./node-config-panel" import { NodeConfigPanel } from "./node-config-panel"
import { import {
getAvailableVariables, getAvailableVariables,
getNodeTitle,
type WorkflowNodeData, type WorkflowNodeData,
} from "./workflow-utils" } from "./workflow-utils"
export function WorkflowConfigSidebar({ export function WorkflowConfigPanel({
definition, definition,
nodeSpecs, nodeSpecs,
selectedNodeId, selectedNodeId,
onSelectNode, onClose,
onChangeNodeData, onChangeNodeData,
onDeleteNode, onDeleteNode,
}: { }: {
definition: AIWorkflowDefinition definition: AIWorkflowDefinition
nodeSpecs: AIWorkflowNodeSpec[] nodeSpecs: AIWorkflowNodeSpec[]
selectedNodeId: string selectedNodeId: string
onSelectNode: (nodeId: string) => void onClose: () => void
onChangeNodeData: (nodeId: string, data: WorkflowNodeData) => void onChangeNodeData: (nodeId: string, data: WorkflowNodeData) => void
onDeleteNode: (nodeId: string) => void onDeleteNode: (nodeId: string) => void
}) { }) {
@@ -33,39 +34,43 @@ export function WorkflowConfigSidebar({
? getAvailableVariables(definition, selectedNode.id, nodeSpecs) ? getAvailableVariables(definition, selectedNode.id, nodeSpecs)
: [] : []
if (!selectedNode) {
return null
}
return ( return (
<aside className="relative z-50 flex w-80 shrink-0 flex-col border-l bg-background"> <div className="pointer-events-none absolute inset-y-3 right-3 z-50 flex w-[360px] max-w-[calc(100%-1.5rem)]">
<div className="border-b px-3 py-2"> <section className="pointer-events-auto flex min-h-0 w-full flex-col overflow-hidden rounded-lg border bg-background shadow-2xl">
<div className="text-sm font-medium"></div> <div className="flex shrink-0 items-start justify-between gap-3 border-b px-4 py-3">
</div> <div className="min-w-0">
<div className="max-h-48 overflow-y-auto border-b p-2"> <div className="text-sm font-medium"></div>
<div className="space-y-1"> <div className="mt-0.5 truncate text-xs text-muted-foreground">
{definition.nodes.map((node) => ( {selectedNode.data?.title || selectedNodeSpec?.title || selectedNode.type}
<button </div>
key={node.id} </div>
type="button" <Button
className={cn( type="button"
"flex w-full items-center justify-between gap-2 rounded-md px-2 py-1.5 text-left text-sm hover:bg-muted", variant="ghost"
selectedNodeId === node.id && "bg-muted" size="icon"
)} className="size-8 shrink-0 text-muted-foreground"
onClick={() => onSelectNode(node.id)} aria-label="关闭属性面板"
> onClick={onClose}
<span className="min-w-0 truncate">{getNodeTitle(node, nodeSpecs)}</span> >
<span className="shrink-0 text-xs text-muted-foreground">{node.type}</span> <XIcon className="size-4" />
</button> </Button>
))}
</div> </div>
</div> <div className="min-h-0 flex-1">
<div className="min-h-0 flex-1"> <NodeConfigPanel
<NodeConfigPanel node={selectedNode}
node={selectedNode} nodeSpec={selectedNodeSpec}
nodeSpec={selectedNodeSpec} nodes={definition.nodes}
nodes={definition.nodes} availableVariables={availableVariables}
availableVariables={availableVariables} showHeader={false}
onChange={onChangeNodeData} onChange={onChangeNodeData}
onDelete={onDeleteNode} onDelete={onDeleteNode}
/> />
</div> </div>
</aside> </section>
</div>
) )
} }
@@ -16,7 +16,7 @@ import {
import type { AIWorkflowDefinition, AIWorkflowNodeSpec } from "@/lib/api/admin" import type { AIWorkflowDefinition, AIWorkflowNodeSpec } from "@/lib/api/admin"
import { useFlowgramEditorProps } from "./flowgram-editor-provider" import { useFlowgramEditorProps } from "./flowgram-editor-provider"
import { WorkflowConfigSidebar } from "./workflow-config-sidebar" import { WorkflowConfigPanel } from "./workflow-config-sidebar"
import { WorkflowEditorToolbar } from "./workflow-editor-toolbar" import { WorkflowEditorToolbar } from "./workflow-editor-toolbar"
import { WorkflowNodePalette } from "./workflow-node-palette" import { WorkflowNodePalette } from "./workflow-node-palette"
import { import {
@@ -63,7 +63,7 @@ export function WorkflowEditor({
toolbarExtra?: ReactNode toolbarExtra?: ReactNode
}) { }) {
const [localDefinition, setLocalDefinition] = useState(definition) const [localDefinition, setLocalDefinition] = useState(definition)
const [selectedNodeId, setSelectedNodeId] = useState(definition.nodes[0]?.id ?? "") const [selectedNodeId, setSelectedNodeId] = useState("")
const validation = useMemo( const validation = useMemo(
() => validateWorkflowDefinition(localDefinition, nodeSpecs), () => validateWorkflowDefinition(localDefinition, nodeSpecs),
@@ -157,9 +157,7 @@ function WorkflowEditorInner({
useEffect(() => { useEffect(() => {
const disposable = selectService.onSelectionChanged(() => { const disposable = selectService.onSelectionChanged(() => {
const selectedNode = selectService.selectedNodes[0] const selectedNode = selectService.selectedNodes[0]
if (selectedNode) { onSelectNode(selectedNode?.id ?? "")
onSelectNode(selectedNode.id)
}
}) })
return () => disposable.dispose() return () => disposable.dispose()
}, [onSelectNode, selectService]) }, [onSelectNode, selectService])
@@ -180,14 +178,6 @@ function WorkflowEditorInner({
emitCurrentDefinition() emitCurrentDefinition()
} }
const selectNode = (nodeId: string) => {
const node = workflowDocument.getAllNodes().find((item) => item.id === nodeId)
if (node) {
selectService.selectNodeAndFocus(node)
}
onSelectNode(nodeId)
}
const updateNodeData = (nodeId: string, data: WorkflowNodeData) => { const updateNodeData = (nodeId: string, data: WorkflowNodeData) => {
const next = updateWorkflowNodeData(definition, nodeId, data) const next = updateWorkflowNodeData(definition, nodeId, data)
context.operation.fromJSON(next as WorkflowJSON) context.operation.fromJSON(next as WorkflowJSON)
@@ -202,11 +192,19 @@ function WorkflowEditorInner({
onDefinitionChange(context.document.toJSON() as AIWorkflowDefinition) onDefinitionChange(context.document.toJSON() as AIWorkflowDefinition)
} }
const closeConfigPanel = () => {
selectService.clear()
onSelectNode("")
}
return ( return (
<div className="relative isolate flex min-h-[640px] flex-1 overflow-hidden border bg-background"> <div
data-workflow-editor-root
className="relative isolate flex h-full min-h-0 w-full flex-1 overflow-hidden border bg-background"
>
<WorkflowNodePalette nodeSpecs={nodeSpecs} onAddNode={addNode} /> <WorkflowNodePalette nodeSpecs={nodeSpecs} onAddNode={addNode} />
<div className="flex min-w-0 flex-1 flex-col"> <div className="flex min-h-0 min-w-0 flex-1 flex-col">
<WorkflowEditorToolbar <WorkflowEditorToolbar
validation={validation} validation={validation}
nodeCount={definition.nodes.length} nodeCount={definition.nodes.length}
@@ -225,16 +223,16 @@ function WorkflowEditorInner({
onPublish={onPublish} onPublish={onPublish}
publishDisabled={publishDisabled} publishDisabled={publishDisabled}
/> />
<div className="min-h-0 flex-1"> <div className="relative min-h-0 flex-1 overflow-hidden">
<EditorRenderer className="h-full w-full" /> <EditorRenderer className="h-full w-full" />
</div> </div>
</div> </div>
<WorkflowConfigSidebar <WorkflowConfigPanel
definition={definition} definition={definition}
nodeSpecs={nodeSpecs} nodeSpecs={nodeSpecs}
selectedNodeId={selectedNodeId} selectedNodeId={selectedNodeId}
onSelectNode={selectNode} onClose={closeConfigPanel}
onChangeNodeData={updateNodeData} onChangeNodeData={updateNodeData}
onDeleteNode={removeNode} onDeleteNode={removeNode}
/> />