n8n/packages/editor-ui/src/stores/versions.ts
Iván Ovejero 5ca2148c7e
refactor(editor): Apply Prettier (no-changelog) (#4920)
*  Adjust `format` script

* 🔥 Remove exemption for `editor-ui`

* 🎨 Prettify

* 👕 Fix lint
2022-12-14 10:04:10 +01:00

50 lines
1.6 KiB
TypeScript

import { getNextVersions } from '@/api/versions';
import { STORES } from '@/constants';
import { IVersion, IVersionNotificationSettings, IVersionsState } from '@/Interface';
import { defineStore } from 'pinia';
import { useRootStore } from './n8nRootStore';
export const useVersionsStore = defineStore(STORES.VERSIONS, {
state: (): IVersionsState => ({
versionNotificationSettings: {
enabled: false,
endpoint: '',
infoUrl: '',
},
nextVersions: [],
currentVersion: undefined,
}),
getters: {
hasVersionUpdates(): boolean {
return this.nextVersions.length > 0;
},
areNotificationsEnabled(): boolean {
return this.versionNotificationSettings.enabled;
},
infoUrl(): string {
return this.versionNotificationSettings.infoUrl;
},
},
actions: {
setVersions({ versions, currentVersion }: { versions: IVersion[]; currentVersion: string }) {
this.nextVersions = versions.filter((version) => version.name !== currentVersion);
this.currentVersion = versions.find((version) => version.name === currentVersion);
},
setVersionNotificationSettings(settings: IVersionNotificationSettings) {
this.versionNotificationSettings = settings;
},
async fetchVersions() {
try {
const { enabled, endpoint } = this.versionNotificationSettings;
if (enabled && endpoint) {
const rootStore = useRootStore();
const currentVersion = rootStore.versionCli;
const instanceId = rootStore.instanceId;
const versions = await getNextVersions(endpoint, currentVersion, instanceId);
this.setVersions({ versions, currentVersion });
}
} catch (e) {}
},
},
});