From f59b591c93ecd7cbd279668abe6494ef2b88c831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milorad=20FIlipovi=C4=87?= Date: Fri, 17 Mar 2023 09:01:39 +0100 Subject: [PATCH] feat(editor): Recommend and pre-select auth type with overrides (#5684) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(editor): Recommend auth type with overrides and pre-select them when creating new credentials * ⚡ Only auto-selecting credentials on cloud --- .../src/plugins/i18n/locales/en.json | 1 + .../editor-ui/src/utils/nodeTypesUtils.ts | 36 +++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/editor-ui/src/plugins/i18n/locales/en.json b/packages/editor-ui/src/plugins/i18n/locales/en.json index 1e7d1f6d9..6ed0d7274 100644 --- a/packages/editor-ui/src/plugins/i18n/locales/en.json +++ b/packages/editor-ui/src/plugins/i18n/locales/en.json @@ -283,6 +283,7 @@ "credentialEdit.credentialConfig.missingCredentialType": "This credential's type isn't available. This usually happens when a previously installed community or custom node was uninstalled.", "credentialEdit.credentialConfig.authTypeSelectorLabel": "Connect using", "credentialEdit.credentialConfig.authTypeSelectorTooltip": "The authentication method to use for the connection", + "credentialEdit.credentialConfig.recommendedAuthTypeSuffix": "(recommended)", "credentialEdit.credentialEdit.confirmMessage.beforeClose1.cancelButtonText": "Keep Editing", "credentialEdit.credentialEdit.confirmMessage.beforeClose1.confirmButtonText": "Close", "credentialEdit.credentialEdit.confirmMessage.beforeClose1.headline": "Close without saving?", diff --git a/packages/editor-ui/src/utils/nodeTypesUtils.ts b/packages/editor-ui/src/utils/nodeTypesUtils.ts index 4d74523c9..b13a2b258 100644 --- a/packages/editor-ui/src/utils/nodeTypesUtils.ts +++ b/packages/editor-ui/src/utils/nodeTypesUtils.ts @@ -36,6 +36,9 @@ import { INodePropertyCollection, } from 'n8n-workflow'; import { isResourceLocatorValue, isJsonKeyObject } from '@/utils'; +import { useCredentialsStore } from '@/stores/credentials'; +import { i18n as locale } from '@/plugins/i18n'; +import { useSettingsStore } from '@/stores/settings'; /* Constants and utility functions mainly used to get information about @@ -381,6 +384,9 @@ export const getNodeAuthOptions = ( if (!nodeType) { return []; } + const recommendedSuffix = locale.baseText( + 'credentialEdit.credentialConfig.recommendedAuthTypeSuffix', + ); let options: NodeAuthenticationOption[] = []; const authProp = getMainAuthField(nodeType); // Some nodes have multiple auth fields with same name but different display options so need @@ -392,18 +398,34 @@ export const getNodeAuthOptions = ( authProps.forEach((field) => { if (field.options) { options = options.concat( - field.options.map((option) => ({ - name: option.name, - value: option.value, - // Also add in the display options so we can hide/show the option if necessary - displayOptions: field.displayOptions, - })) || [], + field.options.map((option) => { + // Check if credential type associated with this auth option has overwritten properties + let hasOverrides = false; + if (useSettingsStore().isCloudDeployment) { + const cred = getNodeCredentialForSelectedAuthType(nodeType, option.value); + if (cred) { + hasOverrides = + useCredentialsStore().getCredentialTypeByName(cred.name).__overwrittenProperties !== + undefined; + } + } + return { + name: + // Add recommended suffix if credentials have overrides and option is not already recommended + hasOverrides && !option.name.endsWith(recommendedSuffix) + ? `${option.name} ${recommendedSuffix}` + : option.name, + value: option.value, + // Also add in the display options so we can hide/show the option if necessary + displayOptions: field.displayOptions, + }; + }) || [], ); } }); // sort so recommended options are first options.forEach((item, i) => { - if (item.name.includes('(recommended)')) { + if (item.name.includes(recommendedSuffix)) { options.splice(i, 1); options.unshift(item); }