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

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2024-12-18 12:35:11 +01:00
parent 6ba91b5e1e
commit f10bbcf65c
No known key found for this signature in database
6 changed files with 183 additions and 233 deletions

View file

@ -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: [

View file

@ -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);
@ -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);

View file

@ -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]);
});
};
});
}
return credentials.sort((a, b) => {
const aDate = new Date(a.updatedAt);
const bDate = new Date(b.updatedAt);
return aDate.getTime() - bDate.getTime();
});
};
const getCredentialTypeByName = computed(() => {
return (type: string): ICredentialType | undefined => state.value.credentialTypes[type];
});
const getCredentialTypeByName = (type: string): ICredentialType | undefined =>
state.value.credentialTypes[type];
const getCredentialById = computed(() => {
return (id: string): ICredentialsResponse => state.value.credentials[id];
});
const getCredentialById = (id: string): ICredentialsResponse => state.value.credentials[id];
const getCredentialByIdAndType = computed(() => {
return (id: string, type: string): ICredentialsResponse | undefined => {
const credential = state.value.credentials[id];
return !credential || credential.type !== type ? undefined : credential;
};
});
const getCredentialByIdAndType = (id: string, type: string): ICredentialsResponse | undefined => {
const credential = state.value.credentials[id];
return !credential || credential.type !== type ? undefined : credential;
};
const getCredentialsByType = computed(() => {
return (credentialType: string): ICredentialsResponse[] => {
return allCredentialsByType.value[credentialType] || [];
};
});
const getCredentialsByType = (credentialType: string): ICredentialsResponse[] =>
allCredentialsByType.value[credentialType] || [];
const getUsableCredentialByType = computed(() => {
return (credentialType: string): ICredentialsResponse[] => {
return allUsableCredentialsByType.value[credentialType] || [];
};
});
const getUsableCredentialByType = (credentialType: string): ICredentialsResponse[] =>
allUsableCredentialsByType.value[credentialType] || [];
const getNodesWithAccess = computed(() => {
return (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName.value(credentialTypeName);
if (!credentialType) {
return [];
}
const nodeTypesStore = useNodeTypesStore();
const getNodesWithAccess = (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName(credentialTypeName);
if (!credentialType) {
return [];
}
const nodeTypesStore = useNodeTypesStore();
return (credentialType.supportedNodes ?? [])
.map((nodeType) => nodeTypesStore.getNodeType(nodeType))
.filter(isPresent);
};
});
return (credentialType.supportedNodes ?? [])
.map((nodeType) => nodeTypesStore.getNodeType(nodeType))
.filter(isPresent);
};
const getScopesByCredentialType = computed(() => {
return (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName.value(credentialTypeName);
if (!credentialType) {
return [];
}
const getScopesByCredentialType = (credentialTypeName: string) => {
const credentialType = getCredentialTypeByName(credentialTypeName);
if (!credentialType) {
return [];
}
const scopeProperty = credentialType.properties.find((p) => p.name === 'scope');
const scopeProperty = credentialType.properties.find((p) => p.name === 'scope');
if (
!scopeProperty ||
!scopeProperty.default ||
typeof scopeProperty.default !== 'string' ||
scopeProperty.default === ''
) {
return [];
}
if (
!scopeProperty ||
!scopeProperty.default ||
typeof scopeProperty.default !== 'string' ||
scopeProperty.default === ''
) {
return [];
}
let { default: scopeDefault } = scopeProperty;
let { default: scopeDefault } = scopeProperty;
// disregard expressions for display
scopeDefault = scopeDefault.replace(/^=/, '').replace(/\{\{.*\}\}/, '');
// disregard expressions for display
scopeDefault = scopeDefault.replace(/^=/, '').replace(/\{\{.*\}\}/, '');
if (/ /.test(scopeDefault)) return scopeDefault.split(' ');
if (/ /.test(scopeDefault)) return scopeDefault.split(' ');
if (/,/.test(scopeDefault)) return scopeDefault.split(',');
if (/,/.test(scopeDefault)) return scopeDefault.split(',');
return [scopeDefault];
};
});
return [scopeDefault];
};
const getCredentialOwnerName = computed(() => {
return (credential: ICredentialsResponse | IUsedCredential | undefined): string => {
const { name, email } = splitName(credential?.homeProject?.name ?? '');
const getCredentialOwnerName = (
credential: ICredentialsResponse | IUsedCredential | undefined,
): string => {
const { name, email } = splitName(credential?.homeProject?.name ?? '');
return name
? email
? `${name} (${email})`
: name
: (email ?? i18n.baseText('credentialEdit.credentialSharing.info.sharee.fallback'));
};
});
return name
? email
? `${name} (${email})`
: name
: (email ?? i18n.baseText('credentialEdit.credentialSharing.info.sharee.fallback'));
};
const getCredentialOwnerNameById = computed(() => {
return (credentialId: string): string => {
const credential = getCredentialById.value(credentialId);
return getCredentialOwnerName.value(credential);
};
});
const getCredentialOwnerNameById = (credentialId: string): string => {
const credential = getCredentialById(credentialId);
return getCredentialOwnerName(credential);
};
const httpOnlyCredentialTypes = computed(() => {
return allCredentialTypes.value.filter(
@ -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;

View file

@ -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);
@ -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

View file

@ -66,75 +66,60 @@ export const useTemplatesStore = defineStore(STORES.TEMPLATES, () => {
);
});
const getTemplatesById = computed(() => {
return (id: string): null | ITemplatesWorkflow => workflows.value[id];
});
const getTemplatesById = (id: string): null | ITemplatesWorkflow => workflows.value[id];
const getFullTemplateById = computed(() => {
return (id: string): null | ITemplatesWorkflowFull => {
const template = workflows.value[id];
return template && 'full' in template && template.full ? template : null;
};
});
const getFullTemplateById = (id: string): null | ITemplatesWorkflowFull => {
const template = workflows.value[id];
return template && 'full' in template && template.full ? template : null;
};
const getCollectionById = computed(() => collections.value);
const getCategoryById = computed(() => {
return (id: string): null | ITemplatesCategory => categories.value[id as unknown as number];
});
const getCategoryById = (id: string): null | ITemplatesCategory =>
categories.value[id as unknown as number];
const getSearchedCollections = computed(() => {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = collectionSearches.value[searchKey];
if (!search) {
return null;
}
const getSearchedCollections = (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = collectionSearches.value[searchKey];
if (!search) {
return null;
}
return search.collectionIds.map((collectionId: string) => collections.value[collectionId]);
};
});
return search.collectionIds.map((collectionId: string) => collections.value[collectionId]);
};
const getSearchedWorkflows = computed(() => {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
if (!search) {
return null;
}
const getSearchedWorkflows = (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
if (!search) {
return null;
}
return search.workflowIds.map((workflowId: string) => workflows.value[workflowId]);
};
});
return search.workflowIds.map((workflowId: string) => workflows.value[workflowId]);
};
const getSearchedWorkflowsTotal = computed(() => {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
const getSearchedWorkflowsTotal = (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
return search ? search.totalWorkflows : 0;
};
});
return search ? search.totalWorkflows : 0;
};
const isSearchLoadingMore = computed(() => {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
const isSearchLoadingMore = (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
return Boolean(search && search.loadingMore);
};
});
return Boolean(search && search.loadingMore);
};
const isSearchFinished = computed(() => {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
const isSearchFinished = (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = workflowSearches.value[searchKey];
return Boolean(
search && !search.loadingMore && search.totalWorkflows === search.workflowIds.length,
);
};
});
return Boolean(
search && !search.loadingMore && search.totalWorkflows === search.workflowIds.length,
);
};
const hasCustomTemplatesHost = computed(() => {
return settingsStore.templatesHost !== TEMPLATES_URLS.DEFAULT_API_HOST;
@ -317,7 +302,7 @@ export const useTemplatesStore = defineStore(STORES.TEMPLATES, () => {
};
const getCollections = async (query: ITemplatesQuery): Promise<ITemplatesCollection[]> => {
const cachedResults = getSearchedCollections.value(query);
const cachedResults = getSearchedCollections(query);
if (cachedResults) {
return cachedResults;
}
@ -337,7 +322,7 @@ export const useTemplatesStore = defineStore(STORES.TEMPLATES, () => {
};
const getWorkflows = async (query: ITemplatesQuery): Promise<ITemplatesWorkflow[]> => {
const cachedResults = getSearchedWorkflows.value(query);
const cachedResults = getSearchedWorkflows(query);
if (cachedResults) {
categories.value = workflowSearches.value[getSearchKey(query)].categories ?? [];
return cachedResults;
@ -353,14 +338,14 @@ export const useTemplatesStore = defineStore(STORES.TEMPLATES, () => {
addWorkflows(payload.workflows);
addWorkflowsSearch({ ...payload, query });
return getSearchedWorkflows.value(query) || [];
return getSearchedWorkflows(query) || [];
};
const getMoreWorkflows = async (query: ITemplatesQuery): Promise<ITemplatesWorkflow[]> => {
if (isSearchLoadingMore.value(query) && !isSearchFinished.value(query)) {
if (isSearchLoadingMore(query) && !isSearchFinished(query)) {
return [];
}
const cachedResults = getSearchedWorkflows.value(query) || [];
const cachedResults = getSearchedWorkflows(query) || [];
const apiEndpoint: string = settingsStore.templatesHost;
setWorkflowSearchLoading(query);
@ -375,7 +360,7 @@ export const useTemplatesStore = defineStore(STORES.TEMPLATES, () => {
addWorkflows(payload.workflows);
addWorkflowsSearch({ ...payload, query });
return getSearchedWorkflows.value(query) || [];
return getSearchedWorkflows(query) || [];
} catch (e) {
setWorkflowSearchLoaded(query);
throw e;

View file

@ -7,23 +7,20 @@ import { useWorkflowsStore } from '@/stores/workflows.store';
import { i18n } from '@/plugins/i18n';
import type { ProjectSharingData } from '@/types/projects.types';
import { splitName } from '@/utils/projects.utils';
import { computed } from 'vue';
export const useWorkflowsEEStore = defineStore(STORES.WORKFLOWS_EE, () => {
const rootStore = useRootStore();
const settingsStore = useSettingsStore();
const workflowStore = useWorkflowsStore();
const getWorkflowOwnerName = computed(() => {
return (
workflowId: string,
fallback = i18n.baseText('workflows.shareModal.info.sharee.fallback'),
): string => {
const workflow = workflowStore.getWorkflowById(workflowId);
const { name, email } = splitName(workflow?.homeProject?.name ?? '');
return name ? (email ? `${name} (${email})` : name) : (email ?? fallback);
};
});
const getWorkflowOwnerName = (
workflowId: string,
fallback = i18n.baseText('workflows.shareModal.info.sharee.fallback'),
): string => {
const workflow = workflowStore.getWorkflowById(workflowId);
const { name, email } = splitName(workflow?.homeProject?.name ?? '');
return name ? (email ? `${name} (${email})` : name) : (email ?? fallback);
};
const setWorkflowSharedWith = (payload: {
workflowId: string;