changed naming cred params when added to login block (#4034)

This commit is contained in:
Celal Zamanoglu
2025-11-20 00:35:32 +03:00
committed by GitHub
parent d055b2fef9
commit 0dbe9f6d36

View File

@@ -27,6 +27,24 @@ type Props = {
onChange?: (value: string) => void; onChange?: (value: string) => void;
}; };
// Function to generate a unique credential parameter key
function generateDefaultCredentialParameterKey(existingKeys: string[]): string {
const baseName = "credentials";
// Check if "credentials" is available
if (!existingKeys.includes(baseName)) {
return baseName;
}
// Find the next available number
let counter = 1;
while (existingKeys.includes(`${baseName}_${counter}`)) {
counter++;
}
return `${baseName}_${counter}`;
}
function LoginBlockCredentialSelector({ nodeId, value, onChange }: Props) { function LoginBlockCredentialSelector({ nodeId, value, onChange }: Props) {
const { setIsOpen, setType } = useCredentialModalState(); const { setIsOpen, setType } = useCredentialModalState();
const nodes = useNodes<AppNode>(); const nodes = useNodes<AppNode>();
@@ -124,22 +142,31 @@ function LoginBlockCredentialSelector({ nodeId, value, onChange }: Props) {
} }
const option = options.find((option) => option.value === newValue); const option = options.find((option) => option.value === newValue);
let parameterKeyToUse = newValue;
if (option?.type === "credential") { if (option?.type === "credential") {
const existingCredential = workflowParameters.find((parameter) => { const existingCredential = workflowParameters.find((parameter) => {
return ( return (
parameter.parameterType === "credential" && parameter.parameterType === "credential" &&
"credentialId" in parameter && "credentialId" in parameter &&
parameter.credentialId === newValue && parameter.credentialId === newValue
parameter.key === newValue
); );
}); });
if (!existingCredential) { if (existingCredential) {
// Use the existing parameter's key
parameterKeyToUse = existingCredential.key;
} else {
// Generate a new parameter key based on existing keys
const existingKeys = newParameters.map((param) => param.key);
const newKey =
generateDefaultCredentialParameterKey(existingKeys);
parameterKeyToUse = newKey;
newParameters = [ newParameters = [
...newParameters, ...newParameters,
{ {
parameterType: "credential", parameterType: "credential",
credentialId: newValue, credentialId: newValue,
key: newValue, key: newKey,
}, },
]; ];
} }
@@ -148,7 +175,7 @@ function LoginBlockCredentialSelector({ nodeId, value, onChange }: Props) {
(parameter) => parameter.key !== value, (parameter) => parameter.key !== value,
); );
} }
onChange?.(newValue); onChange?.(parameterKeyToUse);
setWorkflowParameters(newParameters); setWorkflowParameters(newParameters);
}} }}
> >
@@ -171,13 +198,16 @@ function LoginBlockCredentialSelector({ nodeId, value, onChange }: Props) {
</Select> </Select>
<CredentialsModal <CredentialsModal
onCredentialCreated={(id) => { onCredentialCreated={(id) => {
onChange?.(id); const existingKeys = workflowParameters.map((param) => param.key);
const newKey = generateDefaultCredentialParameterKey(existingKeys);
onChange?.(newKey);
setWorkflowParameters([ setWorkflowParameters([
...workflowParameters, ...workflowParameters,
{ {
parameterType: "credential", parameterType: "credential",
credentialId: id, credentialId: id,
key: id, key: newKey,
}, },
]); ]);
}} }}