From 7a787d2300c445cf13a082c1f6864677a76cb56e Mon Sep 17 00:00:00 2001 From: olgakup <16910687+olgakup@users.noreply.github.com> Date: Mon, 13 Jan 2025 12:28:16 -0800 Subject: [PATCH] fix: updates not showing on first init --- packages/extension/src/ui/action/App.vue | 77 ++++++++++++++++-------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/packages/extension/src/ui/action/App.vue b/packages/extension/src/ui/action/App.vue index bb5b762a7..5cfcb1a40 100644 --- a/packages/extension/src/ui/action/App.vue +++ b/packages/extension/src/ui/action/App.vue @@ -233,27 +233,66 @@ const enabledTestnetworks = ref([]); /** ------------------- * Updates -------------------*/ -const releases = ref(undefined); +const releases = ref(null); const loadedUpdates = ref(false); const showUpdatesBtn = ref(false); const showUpdatesDialog = ref(false); const stateCurrentReleaseTimestamp = ref(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} A promise that resolves when the update state is initialized. + * @throws Will log an error message if the initialization fails. + */ const initUpdateState = async () => { - 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; + 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} A promise that resolves when the function completes. + */ const getShowUpdatesBtn = async () => { try { const lastVersionViewed = await updatesState.getLastVersionViewed(); @@ -271,6 +310,7 @@ const getShowUpdatesBtn = async () => { console.error('Failed to get show updates button:', error); } }; + const openUpdatesDialog = (_location: UpdatesOpenLocation) => { showUpdatesDialog.value = true; updatesState.setLastVersionViewed(currentVersion); @@ -369,18 +409,6 @@ onMounted(async () => { .then(() => (isLoading.value = false)); } else { init(); - try { - await initUpdateState(); - releases.value = await getLatestEnkryptUpdates(); - if (releases.value) { - await getShowUpdatesBtn(); - } - loadedUpdates.value = true; - } catch (error) { - console.error('Failed to get latest enkrypt updates:', error); - loadedUpdates.value = false; - } - setTimeout(() => { rateState.showPopup().then(show => { if (show) { @@ -400,6 +428,7 @@ onMounted(async () => { }); }, 2000); } + initUpdateState(); } else { openOnboard(); }