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 {
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<void> {
if (this.availablePackageCount === -1) {
this.availablePackageCount = await getAvailableCommunityPackageCount();
}
},
async fetchInstalledPackages(): Promise<void> {
export const useCommunityNodesStore = defineStore(STORES.COMMUNITY_NODES, () => {
const availablePackageCount = ref(-1);
const installedPackages = ref<CommunityPackageMap>({});
// ---------------------------------------------------------------------------
// #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<void> => {
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<void> => {
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<void> => {
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<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[pack.packageName] = pack;
return packageMap;
},
{},
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,
);
},
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,
};
});