Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor(editor): Remove unnecessary computed on store methods (no-changelog) #12281

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/editor-ui/src/components/ParameterInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ describe('ParameterInput.vue', () => {
throw new Error('Node does not have any credentials set');
});

// @ts-expect-error Readonly property
mockNodeTypesState.getNodeType = vi.fn().mockReturnValue({
displayName: 'Test',
credentials: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1955,7 +1955,7 @@ describe('useCanvasOperations', () => {
const node = createTestNode({ id: nodeId, type: 'unknown-type' });

workflowsStore.getNodeById.mockReturnValue(node);
nodeTypesStore.getNodeType = () => null;
nodeTypesStore.getNodeType.mockReturnValue(null);

const { revalidateNodeInputConnections } = useCanvasOperations({ router });
revalidateNodeInputConnections(nodeId);
Expand Down Expand Up @@ -2104,7 +2104,7 @@ describe('useCanvasOperations', () => {
const node = createTestNode({ id: nodeId, type: 'unknown-type' });

workflowsStore.getNodeById.mockReturnValue(node);
nodeTypesStore.getNodeType = () => null;
nodeTypesStore.getNodeType.mockReturnValue(null);

const { revalidateNodeOutputConnections } = useCanvasOperations({ router });
revalidateNodeOutputConnections(nodeId);
Expand Down
160 changes: 70 additions & 90 deletions packages/editor-ui/src/stores/credentials.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,114 +84,94 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, () => {
);
});

const allUsableCredentialsForNode = computed(() => {
return (node: INodeUi): ICredentialsResponse[] => {
let credentials: ICredentialsResponse[] = [];
const nodeType = useNodeTypesStore().getNodeType(node.type, node.typeVersion);
if (nodeType?.credentials) {
nodeType.credentials.forEach((cred) => {
credentials = credentials.concat(allUsableCredentialsByType.value[cred.name]);
});
}
return credentials.sort((a, b) => {
const aDate = new Date(a.updatedAt);
const bDate = new Date(b.updatedAt);
return aDate.getTime() - bDate.getTime();
const allUsableCredentialsForNode = (node: INodeUi): ICredentialsResponse[] => {
let credentials: ICredentialsResponse[] = [];
const nodeType = useNodeTypesStore().getNodeType(node.type, node.typeVersion);
if (nodeType?.credentials) {
nodeType.credentials.forEach((cred) => {
credentials = credentials.concat(allUsableCredentialsByType.value[cred.name]);
});
};
});

const getCredentialTypeByName = computed(() => {
return (type: string): ICredentialType | undefined => state.value.credentialTypes[type];
});
}
return credentials.sort((a, b) => {
const aDate = new Date(a.updatedAt);
const bDate = new Date(b.updatedAt);
return aDate.getTime() - bDate.getTime();
});
};

const getCredentialById = computed(() => {
return (id: string): ICredentialsResponse => state.value.credentials[id];
});
const getCredentialTypeByName = (type: string): ICredentialType | undefined =>
state.value.credentialTypes[type];

const getCredentialByIdAndType = computed(() => {
return (id: string, type: string): ICredentialsResponse | undefined => {
const credential = state.value.credentials[id];
return !credential || credential.type !== type ? undefined : credential;
};
});
const getCredentialById = (id: string): ICredentialsResponse => state.value.credentials[id];

const getCredentialsByType = computed(() => {
return (credentialType: string): ICredentialsResponse[] => {
return allCredentialsByType.value[credentialType] || [];
};
});
const getCredentialByIdAndType = (id: string, type: string): ICredentialsResponse | undefined => {
const credential = state.value.credentials[id];
return !credential || credential.type !== type ? undefined : credential;
};

const getUsableCredentialByType = computed(() => {
return (credentialType: string): ICredentialsResponse[] => {
return allUsableCredentialsByType.value[credentialType] || [];
};
});
const getCredentialsByType = (credentialType: string): ICredentialsResponse[] =>
allCredentialsByType.value[credentialType] || [];

const getNodesWithAccess = computed(() => {
return (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName.value(credentialTypeName);
if (!credentialType) {
return [];
}
const nodeTypesStore = useNodeTypesStore();
const getUsableCredentialByType = (credentialType: string): ICredentialsResponse[] =>
allUsableCredentialsByType.value[credentialType] || [];

return (credentialType.supportedNodes ?? [])
.map((nodeType) => nodeTypesStore.getNodeType(nodeType))
.filter(isPresent);
};
});
const getNodesWithAccess = (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName(credentialTypeName);
if (!credentialType) {
return [];
}
const nodeTypesStore = useNodeTypesStore();

const getScopesByCredentialType = computed(() => {
return (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName.value(credentialTypeName);
if (!credentialType) {
return [];
}
return (credentialType.supportedNodes ?? [])
.map((nodeType) => nodeTypesStore.getNodeType(nodeType))
.filter(isPresent);
};

const scopeProperty = credentialType.properties.find((p) => p.name === 'scope');
const getScopesByCredentialType = (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName(credentialTypeName);
if (!credentialType) {
return [];
}

if (
!scopeProperty ||
!scopeProperty.default ||
typeof scopeProperty.default !== 'string' ||
scopeProperty.default === ''
) {
return [];
}
const scopeProperty = credentialType.properties.find((p) => p.name === 'scope');

let { default: scopeDefault } = scopeProperty;
if (
!scopeProperty ||
!scopeProperty.default ||
typeof scopeProperty.default !== 'string' ||
scopeProperty.default === ''
) {
return [];
}

// disregard expressions for display
scopeDefault = scopeDefault.replace(/^=/, '').replace(/\{\{.*\}\}/, '');
let { default: scopeDefault } = scopeProperty;

if (/ /.test(scopeDefault)) return scopeDefault.split(' ');
// disregard expressions for display
scopeDefault = scopeDefault.replace(/^=/, '').replace(/\{\{.*\}\}/, '');

if (/,/.test(scopeDefault)) return scopeDefault.split(',');
if (/ /.test(scopeDefault)) return scopeDefault.split(' ');

return [scopeDefault];
};
});
if (/,/.test(scopeDefault)) return scopeDefault.split(',');

const getCredentialOwnerName = computed(() => {
return (credential: ICredentialsResponse | IUsedCredential | undefined): string => {
const { name, email } = splitName(credential?.homeProject?.name ?? '');
return [scopeDefault];
};

return name
? email
? `${name} (${email})`
: name
: (email ?? i18n.baseText('credentialEdit.credentialSharing.info.sharee.fallback'));
};
});
const getCredentialOwnerName = (
credential: ICredentialsResponse | IUsedCredential | undefined,
): string => {
const { name, email } = splitName(credential?.homeProject?.name ?? '');

const getCredentialOwnerNameById = computed(() => {
return (credentialId: string): string => {
const credential = getCredentialById.value(credentialId);
return name
? email
? `${name} (${email})`
: name
: (email ?? i18n.baseText('credentialEdit.credentialSharing.info.sharee.fallback'));
};

return getCredentialOwnerName.value(credential);
};
});
const getCredentialOwnerNameById = (credentialId: string): string => {
const credential = getCredentialById(credentialId);
return getCredentialOwnerName(credential);
};

const httpOnlyCredentialTypes = computed(() => {
return allCredentialTypes.value.filter(
Expand Down Expand Up @@ -372,7 +352,7 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, () => {
const { credentialTypeName } = params;
let newName = DEFAULT_CREDENTIAL_NAME;
if (!TYPES_WITH_DEFAULT_NAME.includes(credentialTypeName)) {
const cred = getCredentialTypeByName.value(credentialTypeName);
const cred = getCredentialTypeByName(credentialTypeName);
newName = cred ? getAppNameFromCredType(cred.displayName) : '';
newName =
newName.length > 0 ? `${newName} ${DEFAULT_CREDENTIAL_POSTFIX}` : DEFAULT_CREDENTIAL_NAME;
Expand Down
125 changes: 57 additions & 68 deletions packages/editor-ui/src/stores/nodeTypes.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,70 +56,61 @@ export const useNodeTypesStore = defineStore(STORES.NODE_TYPES, () => {
);
});

const getNodeType = computed(() => {
return (nodeTypeName: string, version?: number): INodeTypeDescription | null => {
if (utils.isCredentialOnlyNodeType(nodeTypeName)) {
return getCredentialOnlyNodeType.value(nodeTypeName, version);
}
const getNodeType = (nodeTypeName: string, version?: number): INodeTypeDescription | null => {
if (utils.isCredentialOnlyNodeType(nodeTypeName)) {
return getCredentialOnlyNodeType(nodeTypeName, version);
}

const nodeVersions = nodeTypes.value[nodeTypeName];
const nodeVersions = nodeTypes.value[nodeTypeName];

if (!nodeVersions) return null;
if (!nodeVersions) return null;

const versionNumbers = Object.keys(nodeVersions).map(Number);
const nodeType = nodeVersions[version ?? Math.max(...versionNumbers)];
return nodeType ?? null;
};
});
const versionNumbers = Object.keys(nodeVersions).map(Number);
const nodeType = nodeVersions[version ?? Math.max(...versionNumbers)];
return nodeType ?? null;
};

const getNodeVersions = computed(() => {
return (nodeTypeName: string): number[] => {
return Object.keys(nodeTypes.value[nodeTypeName] ?? {}).map(Number);
};
});
const getNodeVersions = (nodeTypeName: string): number[] => {
return Object.keys(nodeTypes.value[nodeTypeName] ?? {}).map(Number);
};

const getCredentialOnlyNodeType = computed(() => {
return (nodeTypeName: string, version?: number): INodeTypeDescription | null => {
const credentialName = utils.getCredentialTypeName(nodeTypeName);
const httpNode = getNodeType.value(
HTTP_REQUEST_NODE_TYPE,
version ?? CREDENTIAL_ONLY_HTTP_NODE_VERSION,
);
const credential = useCredentialsStore().getCredentialTypeByName(credentialName);
return utils.getCredentialOnlyNodeType(httpNode, credential) ?? null;
};
});
const getCredentialOnlyNodeType = (
nodeTypeName: string,
version?: number,
): INodeTypeDescription | null => {
const credentialName = utils.getCredentialTypeName(nodeTypeName);
const httpNode = getNodeType(
HTTP_REQUEST_NODE_TYPE,
version ?? CREDENTIAL_ONLY_HTTP_NODE_VERSION,
);
const credential = useCredentialsStore().getCredentialTypeByName(credentialName);
return utils.getCredentialOnlyNodeType(httpNode, credential) ?? null;
};

const isConfigNode = computed(() => {
return (workflow: Workflow, node: INode, nodeTypeName: string): boolean => {
if (!workflow.nodes[node.name]) {
return false;
}
const nodeType = getNodeType.value(nodeTypeName);
if (!nodeType) {
return false;
}
const outputs = NodeHelpers.getNodeOutputs(workflow, node, nodeType);
const outputTypes = NodeHelpers.getConnectionTypes(outputs);
const isConfigNode = (workflow: Workflow, node: INode, nodeTypeName: string): boolean => {
if (!workflow.nodes[node.name]) {
return false;
}
const nodeType = getNodeType(nodeTypeName);
if (!nodeType) {
return false;
}
const outputs = NodeHelpers.getNodeOutputs(workflow, node, nodeType);
const outputTypes = NodeHelpers.getConnectionTypes(outputs);

return outputTypes
? outputTypes.filter((output) => output !== NodeConnectionType.Main).length > 0
: false;
};
});
return outputTypes
? outputTypes.filter((output) => output !== NodeConnectionType.Main).length > 0
: false;
};

const isTriggerNode = computed(() => {
return (nodeTypeName: string) => {
const nodeType = getNodeType.value(nodeTypeName);
return !!(nodeType && nodeType.group.includes('trigger'));
};
});
const isTriggerNode = (nodeTypeName: string) => {
const nodeType = getNodeType(nodeTypeName);
return !!(nodeType && nodeType.group.includes('trigger'));
};

const isCoreNodeType = computed(() => {
return (nodeType: INodeTypeDescription) => {
return nodeType.codex?.categories?.includes('Core Nodes');
};
});
const isCoreNodeType = (nodeType: INodeTypeDescription) => {
return nodeType.codex?.categories?.includes('Core Nodes');
};

const visibleNodeTypes = computed(() => {
return allLatestNodeTypes.value.filter((nodeType: INodeTypeDescription) => !nodeType.hidden);
Expand Down Expand Up @@ -198,20 +189,18 @@ export const useNodeTypesStore = defineStore(STORES.NODE_TYPES, () => {
return nodesByOutputType;
});

const isConfigurableNode = computed(() => {
return (workflow: Workflow, node: INode, nodeTypeName: string): boolean => {
const nodeType = getNodeType.value(nodeTypeName);
if (nodeType === null) {
return false;
}
const inputs = NodeHelpers.getNodeInputs(workflow, node, nodeType);
const inputTypes = NodeHelpers.getConnectionTypes(inputs);
const isConfigurableNode = (workflow: Workflow, node: INode, nodeTypeName: string): boolean => {
const nodeType = getNodeType(nodeTypeName);
if (nodeType === null) {
return false;
}
const inputs = NodeHelpers.getNodeInputs(workflow, node, nodeType);
const inputTypes = NodeHelpers.getConnectionTypes(inputs);

return inputTypes
? inputTypes.filter((input) => input !== NodeConnectionType.Main).length > 0
: false;
};
});
return inputTypes
? inputTypes.filter((input) => input !== NodeConnectionType.Main).length > 0
: false;
};

// #endregion

Expand Down
Loading
Loading