diff --git a/client/components/app/SideRail.vue b/client/components/app/SideRail.vue index c6b54c5a..eddcdf26 100644 --- a/client/components/app/SideRail.vue +++ b/client/components/app/SideRail.vue @@ -73,6 +73,12 @@

{{ numIssues }}

+ +
+

v{{ $config.version }}

+ Update +

{{ Source }}

+
@@ -82,6 +88,12 @@ export default { return {} }, computed: { + Source() { + return this.$store.state.Source + }, + isMobileLandscape() { + return this.$store.state.globals.isMobileLandscape + }, isShowingBookshelfToolbar() { if (!this.$route.name) return false return this.$route.name.startsWith('library') @@ -131,6 +143,21 @@ export default { }, numIssues() { return this.$store.state.libraries.issues || 0 + }, + versionData() { + return this.$store.state.versionData || {} + }, + hasUpdate() { + return !!this.versionData.hasUpdate + }, + latestVersion() { + return this.versionData.latestVersion + }, + githubTagUrl() { + return this.versionData.githubTagUrl + }, + streamLibraryItem() { + return this.$store.state.streamLibraryItem } }, methods: {}, diff --git a/client/layouts/default.vue b/client/layouts/default.vue index 5da2a9e8..8b2eea53 100644 --- a/client/layouts/default.vue +++ b/client/layouts/default.vue @@ -515,23 +515,12 @@ export default { this.$store.commit('globals/updateWindowSize', { width: window.innerWidth, height: window.innerHeight }) }, checkVersionUpdate() { - // Version check is only run if time since last check was 5 minutes - const VERSION_CHECK_BUFF = 1000 * 60 * 5 // 5 minutes - var lastVerCheck = localStorage.getItem('lastVerCheck') || 0 - if (Date.now() - Number(lastVerCheck) > VERSION_CHECK_BUFF) { - this.$store - .dispatch('checkForUpdate') - .then((res) => { - localStorage.setItem('lastVerCheck', Date.now()) - if (res && res.hasUpdate) this.showUpdateToast(res) - }) - .catch((err) => console.error(err)) - - if (this.$route.query.error) { - this.$toast.error(this.$route.query.error) - this.$router.replace(this.$route.path) - } - } + this.$store + .dispatch('checkForUpdate') + .then((res) => { + if (res && res.hasUpdate) this.showUpdateToast(res) + }) + .catch((err) => console.error(err)) } }, beforeMount() { @@ -551,6 +540,11 @@ export default { } this.checkVersionUpdate() + + if (this.$route.query.error) { + this.$toast.error(this.$route.query.error) + this.$router.replace(this.$route.path) + } }, beforeDestroy() { window.removeEventListener('resize', this.resize) diff --git a/client/plugins/version.js b/client/plugins/version.js index 225c5bff..b1f9d577 100644 --- a/client/plugins/version.js +++ b/client/plugins/version.js @@ -23,14 +23,17 @@ function parseSemver(ver) { } return null } + +export const currentVersion = packagejson.version + export async function checkForUpdate() { if (!packagejson.version) { - return + return null } var currVerObj = parseSemver('v' + packagejson.version) if (!currVerObj) { console.error('Invalid version', packagejson.version) - return + return null } var largestVer = null await axios.get(`https://api.github.com/repos/advplyr/audiobookshelf/releases`).then((res) => { @@ -49,7 +52,7 @@ export async function checkForUpdate() { }) if (!largestVer) { console.error('No valid version tags to compare with') - return + return null } return { diff --git a/client/store/index.js b/client/store/index.js index 2f5ce23d..ef3915f1 100644 --- a/client/store/index.js +++ b/client/store/index.js @@ -1,4 +1,4 @@ -import { checkForUpdate } from '@/plugins/version' +import { checkForUpdate, currentVersion } from '@/plugins/version' import Vue from 'vue' export const state = () => ({ @@ -65,15 +65,44 @@ export const actions = { }) }, checkForUpdate({ commit }) { - return checkForUpdate() - .then((res) => { - commit('setVersionData', res) - return res - }) - .catch((error) => { - console.error('Update check failed', error) - return false - }) + const VERSION_CHECK_BUFF = 1000 * 60 * 5 // 5 minutes + var lastVerCheck = localStorage.getItem('lastVerCheck') || 0 + var savedVersionData = localStorage.getItem('versionData') + if (savedVersionData) { + try { + savedVersionData = JSON.parse(localStorage.getItem('versionData')) + } catch (error) { + console.error('Failed to parse version data', error) + savedVersionData = null + localStorage.removeItem('versionData') + } + } + + var shouldCheckForUpdate = Date.now() - Number(lastVerCheck) > VERSION_CHECK_BUFF + if (!shouldCheckForUpdate && savedVersionData && savedVersionData.version !== currentVersion) { + // Version mismatch between saved data so check for update anyway + shouldCheckForUpdate = true + } + + if (shouldCheckForUpdate) { + return checkForUpdate() + .then((res) => { + if (res) { + localStorage.setItem('lastVerCheck', Date.now()) + localStorage.setItem('versionData', JSON.stringify(res)) + + commit('setVersionData', res) + } + return res && res.hasUpdate + }) + .catch((error) => { + console.error('Update check failed', error) + return false + }) + } else if (savedVersionData) { + commit('setVersionData', savedVersionData) + } + return null } }