feat: refactor workflow branch selection logic and introduce context provider

This commit is contained in:
mlogclub
2026-06-28 10:35:19 +08:00
parent 64c10ba6dd
commit af9c28033a
5 changed files with 113 additions and 67 deletions
@@ -0,0 +1,41 @@
"use client"
import { createContext, useContext, useMemo, type ReactNode } from "react"
export type SelectedWorkflowBranch = {
nodeId: string
branchId: string
}
type WorkflowBranchSelectionContextValue = {
selectedBranch: SelectedWorkflowBranch | null
onSelectBranch: (branch: SelectedWorkflowBranch | null) => void
}
const WorkflowBranchSelectionContext = createContext<WorkflowBranchSelectionContextValue>({
selectedBranch: null,
onSelectBranch: () => {},
})
export function WorkflowBranchSelectionProvider({
selectedBranch,
onSelectBranch,
children,
}: WorkflowBranchSelectionContextValue & {
children: ReactNode
}) {
const value = useMemo(
() => ({ selectedBranch, onSelectBranch }),
[onSelectBranch, selectedBranch]
)
return (
<WorkflowBranchSelectionContext.Provider value={value}>
{children}
</WorkflowBranchSelectionContext.Provider>
)
}
export function useWorkflowBranchSelection() {
return useContext(WorkflowBranchSelectionContext)
}