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

This commit is contained in:
Ricardo Espinoza 2024-06-19 08:34:58 -07:00 committed by GitHub
parent adbd0d17ab
commit f6b8b8d956
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,68 +1,80 @@
import { getNextVersions } from '@/api/versions'; import * as versionsApi from '@/api/versions';
import { STORES, VERSIONS_MODAL_KEY } from '@/constants'; import { STORES, VERSIONS_MODAL_KEY } from '@/constants';
import type { IVersion, IVersionNotificationSettings, IVersionsState } from '@/Interface'; import type { IVersion, IVersionNotificationSettings } from '@/Interface';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { useRootStore } from './root.store'; import { useRootStore } from './root.store';
import { useToast } from '@/composables/useToast'; import { useToast } from '@/composables/useToast';
import { useUIStore } from '@/stores/ui.store'; import { useUIStore } from '@/stores/ui.store';
import { computed, ref } from 'vue';
export const useVersionsStore = defineStore(STORES.VERSIONS, { type SetVersionParams = { versions: IVersion[]; currentVersion: string };
state: (): IVersionsState => ({
versionNotificationSettings: { export const useVersionsStore = defineStore(STORES.VERSIONS, () => {
enabled: false, const versionNotificationSettings = ref({ enabled: false, endpoint: '', infoUrl: '' });
endpoint: '', const nextVersions = ref<IVersion[]>([]);
infoUrl: '', const currentVersion = ref<IVersion | undefined>();
},
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) {}
},
async checkForNewVersions() {
const enabled = this.areNotificationsEnabled;
if (!enabled) {
return;
}
const { showToast } = useToast(); const { showToast } = useToast();
const uiStore = useUIStore(); const uiStore = useUIStore();
await this.fetchVersions(); // ---------------------------------------------------------------------------
// #region Computed
// ---------------------------------------------------------------------------
const currentVersion = this.currentVersion; const hasVersionUpdates = computed(() => {
const nextVersions = this.nextVersions; return nextVersions.value.length > 0;
});
if (currentVersion && currentVersion.hasSecurityIssue && nextVersions.length) { const areNotificationsEnabled = computed(() => {
const fixVersion = currentVersion.securityIssueFixVersion; return versionNotificationSettings.value.enabled;
});
const infoUrl = computed(() => {
return versionNotificationSettings.value.infoUrl;
});
// #endregion
// ---------------------------------------------------------------------------
// #region Methods
// ---------------------------------------------------------------------------
const fetchVersions = async () => {
try {
const { enabled, endpoint } = versionNotificationSettings.value;
if (enabled && endpoint) {
const rootStore = useRootStore();
const currentVersion = rootStore.versionCli;
const instanceId = rootStore.instanceId;
const versions = await versionsApi.getNextVersions(endpoint, currentVersion, instanceId);
setVersions({ versions, currentVersion });
}
} catch (e) {}
};
const setVersions = (params: SetVersionParams) => {
nextVersions.value = params.versions.filter((v) => v.name !== params.currentVersion);
currentVersion.value = params.versions.find((v) => v.name === params.currentVersion);
};
const setVersionNotificationSettings = (settings: IVersionNotificationSettings) => {
versionNotificationSettings.value = settings;
};
const checkForNewVersions = async () => {
const enabled = areNotificationsEnabled.value;
if (!enabled) {
return;
}
await fetchVersions();
if (
currentVersion.value &&
currentVersion.value.hasSecurityIssue &&
nextVersions.value.length
) {
const fixVersion = currentVersion.value.securityIssueFixVersion;
let message = 'Please update to latest version.'; let message = 'Please update to latest version.';
if (fixVersion) { if (fixVersion) {
message = `Please update to version ${fixVersion} or higher.`; message = `Please update to version ${fixVersion} or higher.`;
@ -81,6 +93,19 @@ export const useVersionsStore = defineStore(STORES.VERSIONS, {
duration: 0, duration: 0,
}); });
} }
}, };
},
// #endregion
return {
currentVersion,
nextVersions,
hasVersionUpdates,
areNotificationsEnabled,
infoUrl,
fetchVersions,
setVersions,
setVersionNotificationSettings,
checkForNewVersions,
};
}); });