fix(ui): prevent duplicate workflow on create (#438)
This commit is contained in:
@@ -51,7 +51,6 @@ type WorkflowActionsArgs = {
|
||||
workflowTemplates: WorkflowTemplate[];
|
||||
setWorkflowItems: React.Dispatch<React.SetStateAction<Workflow[]>>;
|
||||
setSavedWorkflowItems: React.Dispatch<React.SetStateAction<Workflow[]>>;
|
||||
router: ReturnType<typeof useRouter>;
|
||||
};
|
||||
|
||||
function buildWorkflowSteps(workflow: Workflow, definitions: StepDefinition[]): WorkflowStep[] {
|
||||
@@ -157,7 +156,6 @@ function useWorkflowActions({
|
||||
workflowTemplates,
|
||||
setWorkflowItems,
|
||||
setSavedWorkflowItems,
|
||||
router,
|
||||
}: WorkflowActionsArgs) {
|
||||
const [isAddWorkflowDialogOpen, setIsAddWorkflowDialogOpen] = useState(false);
|
||||
const [newWorkflowName, setNewWorkflowName] = useState("");
|
||||
@@ -209,7 +207,8 @@ function useWorkflowActions({
|
||||
const handleWorkflowCreated = (tempId: string, created: Workflow) => {
|
||||
setWorkflowItems((prev) => prev.map((item) => (item.id === tempId ? created : item)));
|
||||
setSavedWorkflowItems((prev) => [{ ...created }, ...prev]);
|
||||
router.refresh();
|
||||
// Note: No router.refresh() needed. Local state already has the correct workflow,
|
||||
// and SSR will fetch fresh data on next navigation.
|
||||
};
|
||||
|
||||
const handleSaveWorkflow = async (workflowId: string) => {
|
||||
@@ -444,7 +443,6 @@ function useWorkspaceWorkflowsPage(
|
||||
workflowTemplates,
|
||||
setWorkflowItems,
|
||||
setSavedWorkflowItems,
|
||||
router,
|
||||
});
|
||||
const {
|
||||
isAddWorkflowDialogOpen,
|
||||
|
||||
@@ -55,8 +55,13 @@ export function useWorkflowSettings(initialWorkflows: Workflow[]) {
|
||||
|
||||
const newFromStore = (prev: Workflow[]) => {
|
||||
const localIds = new Set(prev.map((w) => w.id));
|
||||
// Don't add workflows from store for workspaces where we have temp (pending create) workflows.
|
||||
// This prevents race conditions where WS event arrives before the create API callback.
|
||||
const tempWorkspaceIds = new Set(
|
||||
prev.filter((w) => w.id.startsWith("temp-")).map((w) => w.workspace_id),
|
||||
);
|
||||
return storeWorkflows
|
||||
.filter((sw) => !localIds.has(sw.id))
|
||||
.filter((sw) => !localIds.has(sw.id) && !tempWorkspaceIds.has(sw.workspaceId))
|
||||
.map((sw) => storeItemToWorkflow(sw));
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user