Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: apply variables to fields automatically, remove password truncation from variable fields #5031

Merged
merged 8 commits into from
Dec 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function GlobalVariableModal({
}): JSX.Element {
const [key, setKey] = useState(initialData?.name ?? "");
const [value, setValue] = useState(initialData?.value ?? "");
const [type, setType] = useState(initialData?.type ?? "Generic");
const [type, setType] = useState(initialData?.type ?? "Credential");
const [fields, setFields] = useState<string[]>(
initialData?.default_fields ?? [],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const CustomInputPopover = ({
editNode && disabled && "h-fit w-fit",
disabled &&
"disabled:text-muted disabled:opacity-100 placeholder:disabled:text-muted-foreground",
password && "max-w-64 text-clip pr-14",
password && "text-clip pr-14",
)}
placeholder={
selectedOptions?.length > 0 || selectedOption ? "" : placeholder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
useDeleteGlobalVariables,
useGetGlobalVariables,
} from "@/controllers/API/queries/variables";
import { useGlobalVariablesStore } from "@/stores/globalVariablesStore/globalVariables";
import { useEffect } from "react";
import DeleteConfirmationModal from "../../../../../modals/deleteConfirmationModal";
import useAlertStore from "../../../../../stores/alertStore";
Expand All @@ -14,6 +15,7 @@ import { InputGlobalComponentType, InputProps } from "../../types";
import InputComponent from "../inputComponent";

export default function InputGlobalComponent({
display_name,
disabled,
handleOnNewValue,
value,
Expand All @@ -28,9 +30,12 @@ export default function InputGlobalComponent({

const { data: globalVariables } = useGetGlobalVariables();
const { mutate: mutateDeleteGlobalVariable } = useDeleteGlobalVariables();
const unavailableFields = useGlobalVariablesStore(
(state) => state.unavailableFields,
);

useEffect(() => {
if (globalVariables)
if (globalVariables) {
if (
load_from_db &&
!globalVariables.find((variable) => variable.name === value)
Expand All @@ -40,7 +45,19 @@ export default function InputGlobalComponent({
{ skipSnapshot: true },
);
}
}, [globalVariables]);
if (
!load_from_db &&
value === "" &&
unavailableFields &&
Object.keys(unavailableFields).includes(display_name ?? "")
) {
handleOnNewValue(
{ value: unavailableFields[display_name ?? ""], load_from_db: true },
{ skipSnapshot: true },
);
}
}
}, [globalVariables, unavailableFields]);

async function handleDelete(key: string) {
if (!globalVariables) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import TextAreaComponent from "../textAreaComponent";
export function StrRenderComponent({
templateData,
name,
display_name,
placeholder,
...baseInputProps
}: InputProps<string, StrRenderComponentType>) {
const { handleOnNewValue, id, disabled, editNode, value, isToolMode } =
baseInputProps;
const { handleOnNewValue, id, isToolMode } = baseInputProps;

if (!templateData.options) {
return templateData.multiline ? (
Expand All @@ -34,7 +34,8 @@ export function StrRenderComponent({
password={templateData.password}
load_from_db={templateData.load_from_db}
placeholder={placeholder}
id={"input-" + name}
display_name={display_name}
id={`input-${name}`}
isToolMode={isToolMode}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function ParameterRenderComponent({
{...baseInputProps}
templateData={templateData}
name={name}
display_name={templateData.display_name ?? ""}
editNode={editNode}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type KeyPairListComponentType = {
export type StrRenderComponentType = {
templateData: Partial<InputFieldType>;
name: string;
display_name: string;
};

export type InputListComponentType = {
Expand All @@ -84,6 +85,7 @@ export type TextAreaComponentType = {
export type InputGlobalComponentType = {
load_from_db: boolean | undefined;
password: boolean | undefined;
display_name: string;
};
export type MultiselectComponentType = {
options: string[];
Expand Down
14 changes: 1 addition & 13 deletions src/frontend/src/hooks/flows/use-add-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { usePostAddFlow } from "@/controllers/API/queries/flows/use-post-add-flo
import useAlertStore from "@/stores/alertStore";
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import { useFolderStore } from "@/stores/foldersStore";
import { useGlobalVariablesStore } from "@/stores/globalVariablesStore/globalVariables";
import { useTypesStore } from "@/stores/typesStore";
import { FlowType } from "@/types/flow";
import {
Expand All @@ -19,12 +18,6 @@ import { useParams } from "react-router-dom";
import useDeleteFlow from "./use-delete-flow";

const useAddFlow = () => {
const unavaliableFields = useGlobalVariablesStore(
(state) => state.unavailableFields,
);
const globalVariablesEntries = useGlobalVariablesStore(
(state) => state.globalVariablesEntries,
);
const flows = useFlowsManagerStore((state) => state.flows);
const setFlows = useFlowsManagerStore((state) => state.setFlows);
const { deleteFlow } = useDeleteFlow();
Expand All @@ -48,12 +41,7 @@ const useAddFlow = () => {
? await processDataFromFlow(flow)
: { nodes: [], edges: [], viewport: { zoom: 1, x: 0, y: 0 } };
flowData?.nodes.forEach((node) => {
updateGroupRecursion(
node,
flowData?.edges,
unavaliableFields,
globalVariablesEntries,
);
updateGroupRecursion(node, flowData?.edges);
});
// Create a new flow with a default name if no flow is provided.
if (params?.override && flow) {
Expand Down
8 changes: 1 addition & 7 deletions src/frontend/src/stores/flowStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import { getInputsAndOutputs } from "../utils/storeUtils";
import useAlertStore from "./alertStore";
import { useDarkStore } from "./darkStore";
import useFlowsManagerStore from "./flowsManagerStore";
import { useGlobalVariablesStore } from "./globalVariablesStore/globalVariables";
import { useTypesStore } from "./typesStore";

// this is our useStore hook that we can use in our components to get parts of the store and call actions
Expand Down Expand Up @@ -396,12 +395,7 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
id: newId,
},
};
updateGroupRecursion(
newNode,
selection.edges,
useGlobalVariablesStore.getState().unavailableFields,
useGlobalVariablesStore.getState().globalVariablesEntries,
);
updateGroupRecursion(newNode, selection.edges);

// Add the new node to the list of nodes in state
newNodes = newNodes
Expand Down
58 changes: 2 additions & 56 deletions src/frontend/src/utils/reactflowUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1625,30 +1625,11 @@ export function isOutputType(type: string): boolean {
return OUTPUT_TYPES.has(type);
}

export function updateGroupRecursion(
groupNode: NodeType,
edges: Edge[],
unavailableFields:
| {
[name: string]: string;
}
| undefined,
globalVariablesEntries: string[] | undefined,
) {
updateGlobalVariables(
groupNode.data.node,
unavailableFields,
globalVariablesEntries,
);
export function updateGroupRecursion(groupNode: NodeType, edges: Edge[]) {
if (groupNode.data.node?.flow) {
groupNode.data.node.flow.data!.nodes.forEach((node) => {
if (node.data.node?.flow) {
updateGroupRecursion(
node,
node.data.node.flow.data!.edges,
unavailableFields,
globalVariablesEntries,
);
updateGroupRecursion(node, node.data.node.flow.data!.edges);
}
});
let newFlow = groupNode.data.node!.flow;
Expand All @@ -1660,41 +1641,6 @@ export function updateGroupRecursion(
}
}

export function updateGlobalVariables(
node: APIClassType | undefined,
unavailableFields:
| {
[name: string]: string;
}
| undefined,
globalVariablesEntries: string[] | undefined,
) {
if (node && node.template) {
Object.keys(node.template).forEach((field) => {
if (
globalVariablesEntries &&
node!.template[field].load_from_db &&
!globalVariablesEntries.includes(node!.template[field].value)
) {
node!.template[field].value = "";
node!.template[field].load_from_db = false;
}
if (
!node!.template[field].load_from_db &&
node!.template[field].value === "" &&
unavailableFields &&
Object.keys(unavailableFields).includes(
node!.template[field].display_name ?? "",
)
) {
node!.template[field].value =
unavailableFields[node!.template[field].display_name ?? ""];
node!.template[field].load_from_db = true;
}
});
}
}

export function getGroupOutputNodeId(
flow: FlowType,
p_name: string,
Expand Down
Loading