Skip to content

Commit

Permalink
devop: refactor updates state into pinia store
Browse files Browse the repository at this point in the history
  • Loading branch information
olgakup committed Feb 11, 2025
1 parent 232435a commit 9cd3295
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 96 deletions.
102 changes: 14 additions & 88 deletions packages/extension/src/ui/action/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="app__menu-row">
<logo-min class="app__menu-logo" />
<updated-icon
v-if="loadedUpdates && showUpdatesBtn"
v-if="updatesIsLoaded && showUpdatesBtn"
@click="openUpdatesDialog(UpdatesOpenLocation.logo)"
class="app__menu-updated"
/>
Expand All @@ -28,9 +28,12 @@
<a class="app__menu-dropdown-link" @click="settingsAction">
<settings-icon /> <span>Settings</span>
</a>
<div v-if="loadedUpdates" class="app__menu-dropdown-divider"></div>
<div
v-if="updatesIsLoaded"
class="app__menu-dropdown-divider"
></div>
<a
v-if="loadedUpdates"
v-if="updatesIsLoaded"
class="app__menu-dropdown-link"
@click="openUpdatesDialog(UpdatesOpenLocation.settings)"
>
Expand Down Expand Up @@ -114,8 +117,7 @@
@close:popup="updateShow = !updateShow"
/>
<modal-updates
v-if="loadedUpdates && showUpdatesDialog"
:versions="releases?.versions"
v-if="updatesIsLoaded && showUpdatesDialog"
:current-version="currentVersion"
:current-network="currentNetwork.name"
@close:popup="closeUpdatesDialog"
Expand Down Expand Up @@ -187,16 +189,14 @@ import {
} from '@/libs/metrics/types';
import { NetworksCategory } from '@action/types/network-category';
import { newNetworks } from '@/providers/common/libs/new-features';
import UpdatesState from '@/libs/updates-state';
import UpdatedIcon from '@/ui/action/icons/updates/updated.vue';
import HeartIcon from '@/ui/action/icons/updates/heart.vue';
import { getLatestEnkryptUpdates } from '@action/utils/browser';
import { Updates } from '@/ui/action/types/updates';
import { useUpdatesStore } from './store/updatesStore';
import { storeToRefs } from 'pinia';
const domainState = new DomainState();
const networksState = new NetworksState();
const rateState = new RateState();
const updatesState = new UpdatesState();
const appMenuRef = ref(null);
const showDepositWindow = ref(false);
const accountHeaderData = ref<AccountsHeaderData>({
Expand Down Expand Up @@ -233,87 +233,13 @@ const enabledTestnetworks = ref<string[]>([]);
/** -------------------
* Updates
-------------------*/
const releases = ref<Updates | null>(null);
const loadedUpdates = ref<boolean>(false);
const showUpdatesBtn = ref<boolean>(false);
const updatesStore = useUpdatesStore();
const { updatesIsLoaded, showUpdatesBtn } = storeToRefs(updatesStore);
const showUpdatesDialog = ref<boolean>(false);
const stateCurrentReleaseTimestamp = ref<number>(0);
/**
* Initializes the update state by performing the following actions:
* 1. Retrieves the current release from the state.
* 2. Updates the current release timestamp.
* 3. If the current release is empty or different from the current version in the app state,
* sets the current release and updates the release timestamp.
* 4. Fetches the latest Enkrypt updates and sets the releases state.
* 5. Displays the updates button if there are new releases.
* 6. Sets the loadedUpdates state to true if successful, otherwise false.
*
* @async
* @function initUpdateState
* @returns {Promise<void>} A promise that resolves when the update state is initialized.
* @throws Will log an error message if the initialization fails.
*/
const initUpdateState = async () => {
try {
const currentReleaseInState = await updatesState.getCurrentRelease();
stateCurrentReleaseTimestamp.value =
await updatesState.getCurrentReleaseTimestamp();
if (
currentReleaseInState === '' ||
currentReleaseInState !== currentVersion
) {
await updatesState.setCurrentRelease(currentVersion);
const newReleaseTimestamp = Date.now();
await updatesState.setCurrentReleaseTimestamp(newReleaseTimestamp);
stateCurrentReleaseTimestamp.value = newReleaseTimestamp;
}
releases.value = await getLatestEnkryptUpdates();
if (releases.value) {
await getShowUpdatesBtn();
}
loadedUpdates.value = true;
} catch (error) {
console.error('Failed to init update state:', error);
loadedUpdates.value = false;
}
};
/**
* Asynchronously determines whether to show the updates button based on the last version viewed and the current version.
*
* The function performs the following steps:
* 1. Retrieves the last version viewed from the updates state.
* 2. Checks if the last version viewed is empty or if the current version is greater than the last version viewed.
* 3. If the above condition is true, calculates an expiration timestamp (2 weeks from the current release timestamp).
* 4. Sets the `showUpdatesBtn` value to true if the current release timestamp is less than the expiration timestamp.
* 5. Otherwise, sets the `showUpdatesBtn` value to false.
*
* If an error occurs during the process, it logs an error message to the console.
*
* @returns {Promise<void>} A promise that resolves when the function completes.
*/
const getShowUpdatesBtn = async () => {
try {
const lastVersionViewed = await updatesState.getLastVersionViewed();
if (
lastVersionViewed === '' ||
(currentVersion && semverGT(currentVersion, lastVersionViewed))
) {
const expireTimestamp = stateCurrentReleaseTimestamp.value + 12096e5; //2 weeks;
showUpdatesBtn.value =
stateCurrentReleaseTimestamp.value < expireTimestamp;
} else {
showUpdatesBtn.value = false;
}
} catch (error) {
console.error('Failed to get show updates button:', error);
}
};
const openUpdatesDialog = (_location: UpdatesOpenLocation) => {
showUpdatesDialog.value = true;
updatesState.setLastVersionViewed(currentVersion);
updatesStore.setLastVersionViewed(currentVersion);
showUpdatesBtn.value = false;
if (isOpenMore.value) {
closeMoreMenu();
Expand Down Expand Up @@ -369,7 +295,7 @@ const openBuyPage = () => {
case NetworkNames.SyscoinNEVM:
case NetworkNames.Rollux:
return `${(currentNetwork.value as EvmNetwork).options.buyLink}&address=${currentNetwork.value.displayAddress(
accountHeaderData.value.selectedAccount!.address
accountHeaderData.value.selectedAccount!.address,
)}`;
case NetworkNames.SyscoinNEVMTest:
case NetworkNames.RolluxTest:
Expand Down Expand Up @@ -440,7 +366,7 @@ onMounted(async () => {
});
}, 2000);
}
initUpdateState();
updatesStore.init();
} else {
openOnboard();
}
Expand Down
4 changes: 3 additions & 1 deletion packages/extension/src/ui/action/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import App from './App.vue';
import router from './router';
import * as filters from './utils/filters';
import Vue3Lottie from 'vue3-lottie';
import { createPinia } from 'pinia';

global.WeakMap = WeakMap;

Expand All @@ -12,8 +13,9 @@ if (import.meta.env.DEV) {


const app = createApp(App);
const pinia = createPinia();

app.use(router).use(Vue3Lottie, { name: 'vue3lottie' });
app.use(router).use(Vue3Lottie, { name: 'vue3lottie' }).use(pinia);

app.config.globalProperties.$filters = filters;

Expand Down
123 changes: 123 additions & 0 deletions packages/extension/src/ui/action/store/updatesStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { defineStore } from 'pinia'
import { ref } from 'vue';
import UpdatesState from '@/libs/updates-state';
import { getLatestEnkryptUpdates } from '@action/utils/browser';
import { Updates } from '@/ui/action/types/updates';
import { gt as semverGT } from 'semver';

export const useUpdatesStore = defineStore('useUpdatesStore', () => {

/**
* Curretnt version of the app
*/
const currentVersion = __PACKAGE_VERSION__;
/**
* @updatesState {UpdatesState} - An instance of the UpdatesState in I.
*/
const updatesState = new UpdatesState();
/**
* @releases {Updates | null} - The latest Enkrypt updatesinn json object.
*/
const releases = ref<Updates | null>(null);

const stateCurrentReleaseTimestamp = ref<number>(0);
/**
* @updateIsLoaded {boolean} - A boolean value that indicates whether the updates are loaded.
*/
const updatesIsLoaded = ref<boolean>(false);
/**
* @showUpdatesBtn {boolean} - A boolean value that indicates whether to show the updates button
*/
const showUpdatesBtn = ref<boolean>(false);


/**
* Asynchronously determines whether to show the updates button based on the last version viewed and the current version.
*
* The function performs the following steps:
* 1. Retrieves the last version viewed from the updates state.
* 2. Checks if the last version viewed is empty or if the current version is greater than the last version viewed.
* 3. If the above condition is true, calculates an expiration timestamp (2 weeks from the current release timestamp).
* 4. Sets the `showUpdatesBtn` value to true if the current release timestamp is less than the expiration timestamp.
* 5. Otherwise, sets the `showUpdatesBtn` value to false.
*
* If an error occurs during the process, it logs an error message to the console.
*
* @returns {Promise<void>} A promise that resolves when the function completes.
*/
const getShowUpdatesBtn = async () => {
try {
const lastVersionViewed = await updatesState.getLastVersionViewed();
if (
lastVersionViewed === '' ||
(currentVersion && semverGT(currentVersion, lastVersionViewed))
) {
const expireTimestamp = stateCurrentReleaseTimestamp.value + 12096e5; //2 weeks;
showUpdatesBtn.value =
stateCurrentReleaseTimestamp.value < expireTimestamp;
} else {
showUpdatesBtn.value = false;
}
} catch (error) {
console.error('Failed to get show updates button:', error);
}
};
/**
* Initializes the update state by performing the following actions:
* 1. Retrieves the current release from the state.
* 2. Updates the current release timestamp.
* 3. If the current release is empty or different from the current version in the app state,
* sets the current release and updates the release timestamp.
* 4. Fetches the latest Enkrypt updates and sets the releases state.
* 5. Displays the updates button if there are new releases.
* 6. Sets the updatesIsLoaded state to true if successful, otherwise false.
*
* @async
* @function initUpdateState
* @returns {Promise<void>} A promise that resolves when the update state is initialized.
* @throws Will log an error message if the initialization fails.
*/
const init = async () => {
try {

const currentReleaseInState = await updatesState.getCurrentRelease();
stateCurrentReleaseTimestamp.value =
await updatesState.getCurrentReleaseTimestamp();
if (
currentReleaseInState === '' ||
currentReleaseInState !== currentVersion
) {
await updatesState.setCurrentRelease(currentVersion);
const newReleaseTimestamp = Date.now();
await updatesState.setCurrentReleaseTimestamp(newReleaseTimestamp);
stateCurrentReleaseTimestamp.value = newReleaseTimestamp;
}
releases.value = await getLatestEnkryptUpdates();
if (releases.value) {

await getShowUpdatesBtn();
}
updatesIsLoaded.value = true;
} catch (error) {
console.error('Failed to init update state:', error);
updatesIsLoaded.value = false;
}
};

const setLastVersionViewed = async (_version: string) => {
return updatesState.setLastVersionViewed(_version);
}

return {
updatesState,
releases,
updatesIsLoaded,
showUpdatesBtn,
init,
currentVersion,
stateCurrentReleaseTimestamp,
getShowUpdatesBtn,
setLastVersionViewed
}
})

16 changes: 9 additions & 7 deletions packages/extension/src/ui/action/views/updates/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ import { Version } from '@/ui/action/types/updates';
import { trackUpdatesEvents } from '@/libs/metrics';
import { UpdatesEventType } from '@/libs/metrics/types';
import { NetworkNames } from '@enkryptcom/types';
import { useUpdatesStore } from '@/ui/action/store/updatesStore';
import { storeToRefs } from 'pinia';
const emit = defineEmits<{
(e: 'close:popup'): void;
Expand All @@ -83,28 +85,28 @@ const props = defineProps({
required: true,
type: String,
},
versions: {
type: Array as PropType<Version[]>,
},
currentNetwork: {
type: String as PropType<NetworkNames>,
required: true,
},
});
const updatesStore = useUpdatesStore();
const { releases } = storeToRefs(updatesStore);
/** -------------------
* Versions
------------------- */
const displayVersions = computed(() => {
if (!props.versions) return [];
const index = props.versions.findIndex(
if (!releases.value) return [];
const versions = releases.value.versions;
const index = versions.findIndex(
version => version.version === props.currentVersion,
);
if (index === -1) {
return props.versions.slice(0, 10);
return versions.slice(0, 10);
} else {
return props.versions.slice(index, index + 10);
return versions.slice(index, index + 10);
}
});
Expand Down

0 comments on commit 9cd3295

Please sign in to comment.