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,86 +1,111 @@
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: [], const { showToast } = useToast();
currentVersion: undefined, const uiStore = useUIStore();
}),
getters: { // ---------------------------------------------------------------------------
hasVersionUpdates(): boolean { // #region Computed
return this.nextVersions.length > 0; // ---------------------------------------------------------------------------
},
areNotificationsEnabled(): boolean { const hasVersionUpdates = computed(() => {
return this.versionNotificationSettings.enabled; return nextVersions.value.length > 0;
}, });
infoUrl(): string {
return this.versionNotificationSettings.infoUrl; const areNotificationsEnabled = computed(() => {
}, return versionNotificationSettings.value.enabled;
}, });
actions: {
setVersions({ versions, currentVersion }: { versions: IVersion[]; currentVersion: string }) { const infoUrl = computed(() => {
this.nextVersions = versions.filter((version) => version.name !== currentVersion); return versionNotificationSettings.value.infoUrl;
this.currentVersion = versions.find((version) => version.name === currentVersion); });
},
setVersionNotificationSettings(settings: IVersionNotificationSettings) { // #endregion
this.versionNotificationSettings = settings;
}, // ---------------------------------------------------------------------------
async fetchVersions() { // #region Methods
try { // ---------------------------------------------------------------------------
const { enabled, endpoint } = this.versionNotificationSettings;
if (enabled && endpoint) { const fetchVersions = async () => {
const rootStore = useRootStore(); try {
const currentVersion = rootStore.versionCli; const { enabled, endpoint } = versionNotificationSettings.value;
const instanceId = rootStore.instanceId; if (enabled && endpoint) {
const versions = await getNextVersions(endpoint, currentVersion, instanceId); const rootStore = useRootStore();
this.setVersions({ versions, currentVersion }); const currentVersion = rootStore.versionCli;
} const instanceId = rootStore.instanceId;
} catch (e) {} const versions = await versionsApi.getNextVersions(endpoint, currentVersion, instanceId);
}, setVersions({ versions, currentVersion });
async checkForNewVersions() { }
const enabled = this.areNotificationsEnabled; } catch (e) {}
if (!enabled) { };
return;
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.';
if (fixVersion) {
message = `Please update to version ${fixVersion} or higher.`;
} }
const { showToast } = useToast(); message = `${message} <a class="primary-color">More info</a>`;
const uiStore = useUIStore(); showToast({
title: 'Critical update available',
message,
onClick: () => {
uiStore.openModal(VERSIONS_MODAL_KEY);
},
closeOnClick: true,
customClass: 'clickable',
type: 'warning',
duration: 0,
});
}
};
await this.fetchVersions(); // #endregion
const currentVersion = this.currentVersion; return {
const nextVersions = this.nextVersions; currentVersion,
nextVersions,
if (currentVersion && currentVersion.hasSecurityIssue && nextVersions.length) { hasVersionUpdates,
const fixVersion = currentVersion.securityIssueFixVersion; areNotificationsEnabled,
let message = 'Please update to latest version.'; infoUrl,
if (fixVersion) { fetchVersions,
message = `Please update to version ${fixVersion} or higher.`; setVersions,
} setVersionNotificationSettings,
checkForNewVersions,
message = `${message} <a class="primary-color">More info</a>`; };
showToast({
title: 'Critical update available',
message,
onClick: () => {
uiStore.openModal(VERSIONS_MODAL_KEY);
},
closeOnClick: true,
customClass: 'clickable',
type: 'warning',
duration: 0,
});
}
},
},
}); });