Skip to content

Commit

Permalink
fix: updates not showing on first init
Browse files Browse the repository at this point in the history
  • Loading branch information
olgakup committed Jan 13, 2025
1 parent 6889a49 commit 7a787d2
Showing 1 changed file with 53 additions and 24 deletions.
77 changes: 53 additions & 24 deletions packages/extension/src/ui/action/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -233,27 +233,66 @@ const enabledTestnetworks = ref<string[]>([]);
/** -------------------
* Updates
-------------------*/
const releases = ref<Updates | undefined>(undefined);
const releases = ref<Updates | null>(null);
const loadedUpdates = ref<boolean>(false);
const showUpdatesBtn = ref<boolean>(false);
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 () => {
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<void>} A promise that resolves when the function completes.
*/
const getShowUpdatesBtn = async () => {
try {
const lastVersionViewed = await updatesState.getLastVersionViewed();
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -400,6 +428,7 @@ onMounted(async () => {
});
}, 2000);
}
initUpdateState();
} else {
openOnboard();
}
Expand Down

0 comments on commit 7a787d2

Please sign in to comment.