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'); throw new Error('Node does not have any credentials set');
}); });
// @ts-expect-error Readonly property
mockNodeTypesState.getNodeType = vi.fn().mockReturnValue({ mockNodeTypesState.getNodeType = vi.fn().mockReturnValue({
displayName: 'Test', displayName: 'Test',
credentials: [ credentials: [

View file

@ -1955,7 +1955,7 @@ describe('useCanvasOperations', () => {
const node = createTestNode({ id: nodeId, type: 'unknown-type' }); const node = createTestNode({ id: nodeId, type: 'unknown-type' });
workflowsStore.getNodeById.mockReturnValue(node); workflowsStore.getNodeById.mockReturnValue(node);
nodeTypesStore.getNodeType = () => null; nodeTypesStore.getNodeType.mockReturnValue(null);
const { revalidateNodeInputConnections } = useCanvasOperations({ router }); const { revalidateNodeInputConnections } = useCanvasOperations({ router });
revalidateNodeInputConnections(nodeId); revalidateNodeInputConnections(nodeId);
@ -2104,7 +2104,7 @@ describe('useCanvasOperations', () => {
const node = createTestNode({ id: nodeId, type: 'unknown-type' }); const node = createTestNode({ id: nodeId, type: 'unknown-type' });
workflowsStore.getNodeById.mockReturnValue(node); workflowsStore.getNodeById.mockReturnValue(node);
nodeTypesStore.getNodeType = () => null; nodeTypesStore.getNodeType.mockReturnValue(null);
const { revalidateNodeOutputConnections } = useCanvasOperations({ router }); const { revalidateNodeOutputConnections } = useCanvasOperations({ router });
revalidateNodeOutputConnections(nodeId); revalidateNodeOutputConnections(nodeId);

View file

@ -84,114 +84,94 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, () => {
); );
}); });
const allUsableCredentialsForNode = computed(() => { const allUsableCredentialsForNode = (node: INodeUi): ICredentialsResponse[] => {
return (node: INodeUi): ICredentialsResponse[] => { let credentials: ICredentialsResponse[] = [];
let credentials: ICredentialsResponse[] = []; const nodeType = useNodeTypesStore().getNodeType(node.type, node.typeVersion);
const nodeType = useNodeTypesStore().getNodeType(node.type, node.typeVersion); if (nodeType?.credentials) {
if (nodeType?.credentials) { nodeType.credentials.forEach((cred) => {
nodeType.credentials.forEach((cred) => { credentials = credentials.concat(allUsableCredentialsByType.value[cred.name]);
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();
}); });
}; }
}); 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(() => { const getCredentialTypeByName = (type: string): ICredentialType | undefined =>
return (type: string): ICredentialType | undefined => state.value.credentialTypes[type]; state.value.credentialTypes[type];
});
const getCredentialById = computed(() => { const getCredentialById = (id: string): ICredentialsResponse => state.value.credentials[id];
return (id: string): ICredentialsResponse => state.value.credentials[id];
});
const getCredentialByIdAndType = computed(() => { const getCredentialByIdAndType = (id: string, type: string): ICredentialsResponse | undefined => {
return (id: string, type: string): ICredentialsResponse | undefined => { const credential = state.value.credentials[id];
const credential = state.value.credentials[id]; return !credential || credential.type !== type ? undefined : credential;
return !credential || credential.type !== type ? undefined : credential; };
};
});
const getCredentialsByType = computed(() => { const getCredentialsByType = (credentialType: string): ICredentialsResponse[] =>
return (credentialType: string): ICredentialsResponse[] => { allCredentialsByType.value[credentialType] || [];
return allCredentialsByType.value[credentialType] || [];
};
});
const getUsableCredentialByType = computed(() => { const getUsableCredentialByType = (credentialType: string): ICredentialsResponse[] =>
return (credentialType: string): ICredentialsResponse[] => { allUsableCredentialsByType.value[credentialType] || [];
return allUsableCredentialsByType.value[credentialType] || [];
};
});
const getNodesWithAccess = computed(() => { const getNodesWithAccess = (credentialTypeName: string) => {
return (credentialTypeName: string) => { const credentialType = getCredentialTypeByName(credentialTypeName);
const credentialType = getCredentialTypeByName.value(credentialTypeName); if (!credentialType) {
if (!credentialType) { return [];
return []; }
} const nodeTypesStore = useNodeTypesStore();
const nodeTypesStore = useNodeTypesStore();
return (credentialType.supportedNodes ?? []) return (credentialType.supportedNodes ?? [])
.map((nodeType) => nodeTypesStore.getNodeType(nodeType)) .map((nodeType) => nodeTypesStore.getNodeType(nodeType))
.filter(isPresent); .filter(isPresent);
}; };
});
const getScopesByCredentialType = computed(() => { const getScopesByCredentialType = (credentialTypeName: string) => {
return (credentialTypeName: string) => { const credentialType = getCredentialTypeByName(credentialTypeName);
const credentialType = getCredentialTypeByName.value(credentialTypeName); if (!credentialType) {
if (!credentialType) { return [];
return []; }
}
const scopeProperty = credentialType.properties.find((p) => p.name === 'scope'); const scopeProperty = credentialType.properties.find((p) => p.name === 'scope');
if ( if (
!scopeProperty || !scopeProperty ||
!scopeProperty.default || !scopeProperty.default ||
typeof scopeProperty.default !== 'string' || typeof scopeProperty.default !== 'string' ||
scopeProperty.default === '' scopeProperty.default === ''
) { ) {
return []; return [];
} }
let { default: scopeDefault } = scopeProperty; let { default: scopeDefault } = scopeProperty;
// disregard expressions for display // disregard expressions for display
scopeDefault = scopeDefault.replace(/^=/, '').replace(/\{\{.*\}\}/, ''); 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(() => { const getCredentialOwnerName = (
return (credential: ICredentialsResponse | IUsedCredential | undefined): string => { credential: ICredentialsResponse | IUsedCredential | undefined,
const { name, email } = splitName(credential?.homeProject?.name ?? ''); ): string => {
const { name, email } = splitName(credential?.homeProject?.name ?? '');
return name return name
? email ? email
? `${name} (${email})` ? `${name} (${email})`
: name : name
: (email ?? i18n.baseText('credentialEdit.credentialSharing.info.sharee.fallback')); : (email ?? i18n.baseText('credentialEdit.credentialSharing.info.sharee.fallback'));
}; };
});
const getCredentialOwnerNameById = computed(() => { const getCredentialOwnerNameById = (credentialId: string): string => {
return (credentialId: string): string => { const credential = getCredentialById(credentialId);
const credential = getCredentialById.value(credentialId); return getCredentialOwnerName(credential);
};
return getCredentialOwnerName.value(credential);
};
});
const httpOnlyCredentialTypes = computed(() => { const httpOnlyCredentialTypes = computed(() => {
return allCredentialTypes.value.filter( return allCredentialTypes.value.filter(
@ -372,7 +352,7 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, () => {
const { credentialTypeName } = params; const { credentialTypeName } = params;
let newName = DEFAULT_CREDENTIAL_NAME; let newName = DEFAULT_CREDENTIAL_NAME;
if (!TYPES_WITH_DEFAULT_NAME.includes(credentialTypeName)) { if (!TYPES_WITH_DEFAULT_NAME.includes(credentialTypeName)) {
const cred = getCredentialTypeByName.value(credentialTypeName); const cred = getCredentialTypeByName(credentialTypeName);
newName = cred ? getAppNameFromCredType(cred.displayName) : ''; newName = cred ? getAppNameFromCredType(cred.displayName) : '';
newName = newName =
newName.length > 0 ? `${newName} ${DEFAULT_CREDENTIAL_POSTFIX}` : DEFAULT_CREDENTIAL_NAME; 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(() => { const getNodeType = (nodeTypeName: string, version?: number): INodeTypeDescription | null => {
return (nodeTypeName: string, version?: number): INodeTypeDescription | null => { if (utils.isCredentialOnlyNodeType(nodeTypeName)) {
if (utils.isCredentialOnlyNodeType(nodeTypeName)) { return getCredentialOnlyNodeType(nodeTypeName, version);
return getCredentialOnlyNodeType.value(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 versionNumbers = Object.keys(nodeVersions).map(Number);
const nodeType = nodeVersions[version ?? Math.max(...versionNumbers)]; const nodeType = nodeVersions[version ?? Math.max(...versionNumbers)];
return nodeType ?? null; return nodeType ?? null;
}; };
});
const getNodeVersions = computed(() => { const getNodeVersions = (nodeTypeName: string): number[] => {
return (nodeTypeName: string): number[] => { return Object.keys(nodeTypes.value[nodeTypeName] ?? {}).map(Number);
return Object.keys(nodeTypes.value[nodeTypeName] ?? {}).map(Number); };
};
});
const getCredentialOnlyNodeType = computed(() => { const getCredentialOnlyNodeType = (
return (nodeTypeName: string, version?: number): INodeTypeDescription | null => { nodeTypeName: string,
const credentialName = utils.getCredentialTypeName(nodeTypeName); version?: number,
const httpNode = getNodeType.value( ): INodeTypeDescription | null => {
HTTP_REQUEST_NODE_TYPE, const credentialName = utils.getCredentialTypeName(nodeTypeName);
version ?? CREDENTIAL_ONLY_HTTP_NODE_VERSION, const httpNode = getNodeType(
); HTTP_REQUEST_NODE_TYPE,
const credential = useCredentialsStore().getCredentialTypeByName(credentialName); version ?? CREDENTIAL_ONLY_HTTP_NODE_VERSION,
return utils.getCredentialOnlyNodeType(httpNode, credential) ?? null; );
}; const credential = useCredentialsStore().getCredentialTypeByName(credentialName);
}); return utils.getCredentialOnlyNodeType(httpNode, credential) ?? null;
};
const isConfigNode = computed(() => { const isConfigNode = (workflow: Workflow, node: INode, nodeTypeName: string): boolean => {
return (workflow: Workflow, node: INode, nodeTypeName: string): boolean => { if (!workflow.nodes[node.name]) {
if (!workflow.nodes[node.name]) { return false;
return false; }
} const nodeType = getNodeType(nodeTypeName);
const nodeType = getNodeType.value(nodeTypeName); if (!nodeType) {
if (!nodeType) { return false;
return false; }
} const outputs = NodeHelpers.getNodeOutputs(workflow, node, nodeType);
const outputs = NodeHelpers.getNodeOutputs(workflow, node, nodeType); const outputTypes = NodeHelpers.getConnectionTypes(outputs);
const outputTypes = NodeHelpers.getConnectionTypes(outputs);
return outputTypes return outputTypes
? outputTypes.filter((output) => output !== NodeConnectionType.Main).length > 0 ? outputTypes.filter((output) => output !== NodeConnectionType.Main).length > 0
: false; : false;
}; };
});
const isTriggerNode = computed(() => { const isTriggerNode = (nodeTypeName: string) => {
return (nodeTypeName: string) => { const nodeType = getNodeType(nodeTypeName);
const nodeType = getNodeType.value(nodeTypeName); return !!(nodeType && nodeType.group.includes('trigger'));
return !!(nodeType && nodeType.group.includes('trigger')); };
};
});
const isCoreNodeType = computed(() => { const isCoreNodeType = (nodeType: INodeTypeDescription) => {
return (nodeType: INodeTypeDescription) => { return nodeType.codex?.categories?.includes('Core Nodes');
return nodeType.codex?.categories?.includes('Core Nodes'); };
};
});
const visibleNodeTypes = computed(() => { const visibleNodeTypes = computed(() => {
return allLatestNodeTypes.value.filter((nodeType: INodeTypeDescription) => !nodeType.hidden); return allLatestNodeTypes.value.filter((nodeType: INodeTypeDescription) => !nodeType.hidden);
@ -198,20 +189,18 @@ export const useNodeTypesStore = defineStore(STORES.NODE_TYPES, () => {
return nodesByOutputType; return nodesByOutputType;
}); });
const isConfigurableNode = computed(() => { const isConfigurableNode = (workflow: Workflow, node: INode, nodeTypeName: string): boolean => {
return (workflow: Workflow, node: INode, nodeTypeName: string): boolean => { const nodeType = getNodeType(nodeTypeName);
const nodeType = getNodeType.value(nodeTypeName); if (nodeType === null) {
if (nodeType === null) { return false;
return false; }
} const inputs = NodeHelpers.getNodeInputs(workflow, node, nodeType);
const inputs = NodeHelpers.getNodeInputs(workflow, node, nodeType); const inputTypes = NodeHelpers.getConnectionTypes(inputs);
const inputTypes = NodeHelpers.getConnectionTypes(inputs);
return inputTypes return inputTypes
? inputTypes.filter((input) => input !== NodeConnectionType.Main).length > 0 ? inputTypes.filter((input) => input !== NodeConnectionType.Main).length > 0
: false; : false;
}; };
});
// #endregion // #endregion

View file

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

View file

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