n8n/packages/editor-ui/src/stores/communityNodes.store.ts
Alex Grozav bbe493896c
fix: Remove Vue.component usage and refactor plugins into Vue Plugins (no-changelog) (#6445)
* fix: remove Vue.component usage and refactor plugins into Vue Plugins system (no-changelog)

* fix linting issues

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2023-06-16 10:30:57 +03:00

96 lines
3.1 KiB
TypeScript

import {
getInstalledCommunityNodes,
installNewPackage,
uninstallPackage,
updatePackage,
} 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 { STORES } from '@/constants';
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> {
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;
},
{},
);
},
removePackageByName(name: string): void {
const { [name]: removedPackage, ...remainingPackages } = this.installedPackages;
this.installedPackages = remainingPackages;
},
updatePackageObject(newPackage: PublicInstalledPackage) {
this.installedPackages[newPackage.packageName] = newPackage;
},
},
});