refactor(editor): Migrate CommunityNodes.store to use composition API (no-changelog) (#9766)

This commit is contained in:
Ricardo Espinoza 2024-06-17 04:53:25 -04:00 committed by GitHub
parent 0431f5a4ee
commit 4a1ff4878f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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