mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor(editor): Migrate CommunityNodes.store
to use composition API (no-changelog) (#9766)
This commit is contained in:
parent
0431f5a4ee
commit
4a1ff4878f
|
@ -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) =>
|
|
||||||
a.packageName.localeCompare(b.packageName),
|
const getInstalledPackages = computed(() => {
|
||||||
);
|
return Object.values(installedPackages.value).sort((a, b) =>
|
||||||
},
|
a.packageName.localeCompare(b.packageName),
|
||||||
getInstalledPackageByName() {
|
);
|
||||||
return (name: string): PublicInstalledPackage => this.installedPackages[name];
|
});
|
||||||
},
|
|
||||||
},
|
// #endregion
|
||||||
actions: {
|
|
||||||
async fetchAvailableCommunityPackageCount(): Promise<void> {
|
// ---------------------------------------------------------------------------
|
||||||
if (this.availablePackageCount === -1) {
|
// #region Methods
|
||||||
this.availablePackageCount = await getAvailableCommunityPackageCount();
|
// ---------------------------------------------------------------------------
|
||||||
}
|
|
||||||
},
|
const fetchAvailableCommunityPackageCount = async (): Promise<void> => {
|
||||||
async fetchInstalledPackages(): 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 rootStore = useRootStore();
|
||||||
const installedPackages = await getInstalledCommunityNodes(rootStore.getRestApiContext);
|
await communityNodesApi.installNewPackage(rootStore.getRestApiContext, packageName);
|
||||||
this.setInstalledPackages(installedPackages);
|
await fetchInstalledPackages();
|
||||||
const timeout = installedPackages.length > 0 ? 0 : LOADER_DELAY;
|
} catch (error) {
|
||||||
setTimeout(() => {
|
throw error;
|
||||||
return;
|
}
|
||||||
}, timeout);
|
};
|
||||||
},
|
|
||||||
async installPackage(packageName: string): Promise<void> {
|
const uninstallPackage = async (packageName: string): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const rootStore = useRootStore();
|
const rootStore = useRootStore();
|
||||||
await installNewPackage(rootStore.getRestApiContext, packageName);
|
await communityNodesApi.uninstallPackage(rootStore.getRestApiContext, packageName);
|
||||||
await this.fetchInstalledPackages();
|
removePackageByName(packageName);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
async uninstallPackage(packageName: string): Promise<void> {
|
|
||||||
try {
|
const removePackageByName = (name: string): void => {
|
||||||
const rootStore = useRootStore();
|
const { [name]: removedPackage, ...remainingPackages } = installedPackages.value;
|
||||||
await uninstallPackage(rootStore.getRestApiContext, packageName);
|
installedPackages.value = remainingPackages;
|
||||||
this.removePackageByName(packageName);
|
};
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
const updatePackageObject = (newPackage: PublicInstalledPackage) => {
|
||||||
}
|
installedPackages.value[newPackage.packageName] = newPackage;
|
||||||
},
|
};
|
||||||
async updatePackage(packageName: string): Promise<void> {
|
|
||||||
try {
|
const updatePackage = async (packageName: string): Promise<void> => {
|
||||||
const rootStore = useRootStore();
|
try {
|
||||||
const packageToUpdate: PublicInstalledPackage = this.getInstalledPackageByName(packageName);
|
const rootStore = useRootStore();
|
||||||
const updatedPackage: PublicInstalledPackage = await updatePackage(
|
const packageToUpdate: PublicInstalledPackage = getInstalledPackageByName.value(packageName);
|
||||||
rootStore.getRestApiContext,
|
const updatedPackage: PublicInstalledPackage = await communityNodesApi.updatePackage(
|
||||||
packageToUpdate.packageName,
|
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;
|
|
||||||
},
|
|
||||||
{},
|
|
||||||
);
|
);
|
||||||
},
|
updatePackageObject(updatedPackage);
|
||||||
removePackageByName(name: string): void {
|
} catch (error) {
|
||||||
const { [name]: removedPackage, ...remainingPackages } = this.installedPackages;
|
throw error;
|
||||||
this.installedPackages = remainingPackages;
|
}
|
||||||
},
|
};
|
||||||
updatePackageObject(newPackage: PublicInstalledPackage) {
|
|
||||||
this.installedPackages[newPackage.packageName] = newPackage;
|
// #endregion
|
||||||
},
|
|
||||||
},
|
const getInstalledPackageByName = computed(() => {
|
||||||
|
return (name: string): PublicInstalledPackage => installedPackages.value[name];
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
getInstalledPackageByName,
|
||||||
|
getInstalledPackages,
|
||||||
|
availablePackageCount,
|
||||||
|
fetchAvailableCommunityPackageCount,
|
||||||
|
fetchInstalledPackages,
|
||||||
|
installPackage,
|
||||||
|
uninstallPackage,
|
||||||
|
updatePackage,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue