diff --git a/packages/editor-ui/src/stores/communityNodes.store.ts b/packages/editor-ui/src/stores/communityNodes.store.ts index f746a903ae..5cfe7ab4c6 100644 --- a/packages/editor-ui/src/stores/communityNodes.store.ts +++ b/packages/editor-ui/src/stores/communityNodes.store.ts @@ -1,95 +1,120 @@ -import { - getInstalledCommunityNodes, - installNewPackage, - uninstallPackage, - updatePackage, -} from '@/api/communityNodes'; +import * as communityNodesApi from '@/api/communityNodes'; import { getAvailableCommunityPackageCount } from '@/api/settings'; import { defineStore } from 'pinia'; import { useRootStore } from './n8nRoot.store'; import type { PublicInstalledPackage } from 'n8n-workflow'; -import type { CommunityNodesState, CommunityPackageMap } from '@/Interface'; +import type { CommunityPackageMap } from '@/Interface'; import { STORES } from '@/constants'; +import { computed, ref } from 'vue'; const LOADER_DELAY = 300; -export const useCommunityNodesStore = defineStore(STORES.COMMUNITY_NODES, { - state: (): CommunityNodesState => ({ - // -1 means that package count has not been fetched yet - availablePackageCount: -1, - installedPackages: {}, - }), - getters: { - getInstalledPackages(): PublicInstalledPackage[] { - return Object.values(this.installedPackages).sort((a, b) => - a.packageName.localeCompare(b.packageName), - ); - }, - getInstalledPackageByName() { - return (name: string): PublicInstalledPackage => this.installedPackages[name]; - }, - }, - actions: { - async fetchAvailableCommunityPackageCount(): Promise { - if (this.availablePackageCount === -1) { - this.availablePackageCount = await getAvailableCommunityPackageCount(); - } - }, - async fetchInstalledPackages(): Promise { +export const useCommunityNodesStore = defineStore(STORES.COMMUNITY_NODES, () => { + const availablePackageCount = ref(-1); + + const installedPackages = ref({}); + + // --------------------------------------------------------------------------- + // #region Computed + // --------------------------------------------------------------------------- + + const getInstalledPackages = computed(() => { + return Object.values(installedPackages.value).sort((a, b) => + a.packageName.localeCompare(b.packageName), + ); + }); + + // #endregion + + // --------------------------------------------------------------------------- + // #region Methods + // --------------------------------------------------------------------------- + + const fetchAvailableCommunityPackageCount = async (): Promise => { + if (availablePackageCount.value === -1) { + availablePackageCount.value = await getAvailableCommunityPackageCount(); + } + }; + + const setInstalledPackages = (packages: PublicInstalledPackage[]) => { + installedPackages.value = packages.reduce( + (packageMap: CommunityPackageMap, pack: PublicInstalledPackage) => { + packageMap[pack.packageName] = pack; + return packageMap; + }, + {}, + ); + }; + + const fetchInstalledPackages = async (): Promise => { + const rootStore = useRootStore(); + const installedPackages = await communityNodesApi.getInstalledCommunityNodes( + rootStore.getRestApiContext, + ); + setInstalledPackages(installedPackages); + const timeout = installedPackages.length > 0 ? 0 : LOADER_DELAY; + setTimeout(() => { + return; + }, timeout); + }; + + const installPackage = async (packageName: string): Promise => { + try { const rootStore = useRootStore(); - const installedPackages = await getInstalledCommunityNodes(rootStore.getRestApiContext); - this.setInstalledPackages(installedPackages); - const timeout = installedPackages.length > 0 ? 0 : LOADER_DELAY; - setTimeout(() => { - return; - }, timeout); - }, - async installPackage(packageName: string): Promise { - try { - const rootStore = useRootStore(); - await installNewPackage(rootStore.getRestApiContext, packageName); - await this.fetchInstalledPackages(); - } catch (error) { - throw error; - } - }, - async uninstallPackage(packageName: string): Promise { - try { - const rootStore = useRootStore(); - await uninstallPackage(rootStore.getRestApiContext, packageName); - this.removePackageByName(packageName); - } catch (error) { - throw error; - } - }, - async updatePackage(packageName: string): Promise { - try { - const rootStore = useRootStore(); - const packageToUpdate: PublicInstalledPackage = this.getInstalledPackageByName(packageName); - const updatedPackage: PublicInstalledPackage = await updatePackage( - rootStore.getRestApiContext, - packageToUpdate.packageName, - ); - this.updatePackageObject(updatedPackage); - } catch (error) { - throw error; - } - }, - setInstalledPackages(packages: PublicInstalledPackage[]) { - this.installedPackages = packages.reduce( - (packageMap: CommunityPackageMap, pack: PublicInstalledPackage) => { - packageMap[pack.packageName] = pack; - return packageMap; - }, - {}, + await communityNodesApi.installNewPackage(rootStore.getRestApiContext, packageName); + await fetchInstalledPackages(); + } catch (error) { + throw error; + } + }; + + const uninstallPackage = async (packageName: string): Promise => { + try { + const rootStore = useRootStore(); + await communityNodesApi.uninstallPackage(rootStore.getRestApiContext, packageName); + removePackageByName(packageName); + } catch (error) { + throw error; + } + }; + + const removePackageByName = (name: string): void => { + const { [name]: removedPackage, ...remainingPackages } = installedPackages.value; + installedPackages.value = remainingPackages; + }; + + const updatePackageObject = (newPackage: PublicInstalledPackage) => { + installedPackages.value[newPackage.packageName] = newPackage; + }; + + const updatePackage = async (packageName: string): Promise => { + try { + const rootStore = useRootStore(); + const packageToUpdate: PublicInstalledPackage = getInstalledPackageByName.value(packageName); + const updatedPackage: PublicInstalledPackage = await communityNodesApi.updatePackage( + rootStore.getRestApiContext, + packageToUpdate.packageName, ); - }, - removePackageByName(name: string): void { - const { [name]: removedPackage, ...remainingPackages } = this.installedPackages; - this.installedPackages = remainingPackages; - }, - updatePackageObject(newPackage: PublicInstalledPackage) { - this.installedPackages[newPackage.packageName] = newPackage; - }, - }, + updatePackageObject(updatedPackage); + } catch (error) { + throw error; + } + }; + + // #endregion + + const getInstalledPackageByName = computed(() => { + return (name: string): PublicInstalledPackage => installedPackages.value[name]; + }); + + return { + getInstalledPackageByName, + getInstalledPackages, + availablePackageCount, + fetchAvailableCommunityPackageCount, + fetchInstalledPackages, + installPackage, + uninstallPackage, + updatePackage, + }; });