Context parameters in UI (#902)

This commit is contained in:
Kerem Yilmaz
2024-10-03 12:19:45 -07:00
committed by GitHub
parent 9e50456995
commit 83d0879034
18 changed files with 342 additions and 270 deletions

View File

@@ -0,0 +1,51 @@
import { useNodes, useReactFlow } from "@xyflow/react";
import { AppNode } from "../editor/nodes";
import {
getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys,
getUpdatedParametersAfterLabelUpdateForSourceParameterKey,
} from "../editor/workflowEditorUtils";
import { useState } from "react";
import { useWorkflowParametersState } from "../editor/useWorkflowParametersState";
type Props = {
id: string;
initialValue: string;
};
function useNodeLabelChangeHandler({ id, initialValue }: Props) {
const [label, setLabel] = useState(initialValue);
const nodes = useNodes<AppNode>();
const { setNodes } = useReactFlow();
const [workflowParameters, setWorkflowParameters] =
useWorkflowParametersState();
function handleLabelChange(value: string) {
const existingLabels = nodes.map((n) => n.data.label);
const labelWithoutWhitespace = value.replace(/\s+/g, "_");
const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels,
);
setLabel(newLabel);
setNodes(
getUpdatedNodesAfterLabelUpdateForParameterKeys(
id,
newLabel,
nodes as Array<AppNode>,
),
);
setWorkflowParameters(
getUpdatedParametersAfterLabelUpdateForSourceParameterKey(
id,
newLabel,
nodes,
workflowParameters,
),
);
}
return [label, handleLabelChange] as const;
}
export { useNodeLabelChangeHandler };