A local-first personal finance app
Implement outdated version indicator (#463) (#621)
Vincent Schmandt497b2a2009a5
diff --git a/packages/desktop-client/src/components/FinancesApp.js b/packages/desktop-client/src/components/FinancesApp.jsindex 04a26115e8..1a59f5c6c9 100644--- a/packages/desktop-client/src/components/FinancesApp.js+++ b/packages/desktop-client/src/components/FinancesApp.js@@ -19,6 +19,7 @@ import * as actions from 'loot-core/src/client/actions';import { AccountsProvider } from 'loot-core/src/client/data-hooks/accounts';import { PayeesProvider } from 'loot-core/src/client/data-hooks/payees';import { SpreadsheetProvider } from 'loot-core/src/client/SpreadsheetProvider';+import checkForUpdateNotification from 'loot-core/src/client/update-notification';import checkForUpgradeNotifications from 'loot-core/src/client/upgrade-notifications';import * as undo from 'loot-core/src/platform/client/undo';import { BudgetMonthCountProvider } from 'loot-design/src/components/budget/BudgetMonthCountContext';@@ -30,6 +31,7 @@ import Wallet from 'loot-design/src/svg/v1/Wallet';import { isMobile } from '../util';import { getLocationState, makeLocationState } from '../util/location-state';+import { getIsOutdated, getLatestVersion } from '../util/versions';import Account from './accounts/Account';import { default as MobileAccount } from './accounts/MobileAccount';@@ -272,6 +274,17 @@ class FinancesApp extends React.Component {);}, 100);+ setTimeout(async () => {+ await this.props.sync();+ await checkForUpdateNotification(+ this.props.addNotification,+ getIsOutdated,+ getLatestVersion,+ this.props.loadPrefs,+ this.props.savePrefs,+ );+ }, 100);+window.addEventListener('resize', this.handleWindowResize);}diff --git a/packages/desktop-client/src/components/settings/index.js b/packages/desktop-client/src/components/settings/index.jsindex 3a370d72f5..f3550feb0c 100644--- a/packages/desktop-client/src/components/settings/index.js+++ b/packages/desktop-client/src/components/settings/index.js@@ -12,6 +12,7 @@ import { colors } from 'loot-design/src/style';import tokens from 'loot-design/src/tokens';import { withThemeColor } from 'loot-design/src/util/withThemeColor';+import useLatestVersion, { useIsOutdated } from '../../hooks/useLatestVersion';import { isMobile } from '../../util';import { Page } from '../Page';import { useServerVersion } from '../ServerContext';@@ -27,10 +28,22 @@ import { Section, AdvancedToggle } from './UI';function About() {const version = useServerVersion();+ const latestVersion = useLatestVersion();+ const isOutdated = useIsOutdated();return (<Section title="About" style={{ gap: 5 }}>- <Text>Client version: v{window.Actual.ACTUAL_VERSION}</Text>+ <Text>+ Client version: v{window.Actual.ACTUAL_VERSION} (+ {isOutdated ? (+ <a href="https://actualbudget.github.io/docs/Release-Notes">+ new version available: {latestVersion}+ </a>+ ) : (+ <span>latest</span>+ )}+ )+ </Text><Text>Server version: {version}</Text></Section>);diff --git a/packages/desktop-client/src/hooks/useLatestVersion.js b/packages/desktop-client/src/hooks/useLatestVersion.jsnew file mode 100644index 0000000000..436392b809--- /dev/null+++ b/packages/desktop-client/src/hooks/useLatestVersion.js@@ -0,0 +1,30 @@+import { useState, useEffect } from 'react';++import { getIsOutdated, getLatestVersion } from '../util/versions';++function useIsOutdated() {+ let [isOutdated, setIsOutdated] = useState(false);+ const latestVersion = useLatestVersion();++ useEffect(() => {+ (async () => {+ setIsOutdated(await getIsOutdated(latestVersion));+ })();+ }, [latestVersion]);++ return isOutdated;+}++function useLatestVersion() {+ let [latestVersion, setLatestVersion] = useState('');+ useEffect(() => {+ (async () => {+ setLatestVersion(await getLatestVersion());+ })();+ }, []);++ return latestVersion;+}++export default useLatestVersion;+export { useIsOutdated };diff --git a/packages/desktop-client/src/util/versions.js b/packages/desktop-client/src/util/versions.jsnew file mode 100644index 0000000000..46406bc7f0--- /dev/null+++ b/packages/desktop-client/src/util/versions.js@@ -0,0 +1,29 @@+function parseSemanticVersion(versionString) {+ return versionString+ .replace('v', '')+ .split('.')+ .map(n => parseInt(n));+}++export function cmpSemanticVersion(versionStringA, versionStringB) {+ let x = parseSemanticVersion(versionStringA);+ let y = parseSemanticVersion(versionStringB);++ return x[0] - y[0] || x[1] - y[1] || x[2] - y[2];+}++export async function getLatestVersion() {+ let response = await fetch(+ 'https://api.github.com/repos/actualbudget/actual/tags',+ );+ let json = await response.json();+ let tags = json.map(t => t.name).concat([`v${window.Actual.ACTUAL_VERSION}`]);+ tags.sort(cmpSemanticVersion);++ return tags[tags.length - 1];+}++export async function getIsOutdated(latestVersion) {+ let clientVersion = window.Actual.ACTUAL_VERSION;+ return cmpSemanticVersion(clientVersion, latestVersion) < 0;+}diff --git a/packages/loot-core/src/client/update-notification.js b/packages/loot-core/src/client/update-notification.jsnew file mode 100644index 0000000000..d30291eecf--- /dev/null+++ b/packages/loot-core/src/client/update-notification.js@@ -0,0 +1,34 @@+export default async function checkForUpdateNotification(+ addNotification,+ getIsOutdated,+ getLatestVersion,+ loadPrefs,+ savePrefs,+) {+ let latestVersion = await getLatestVersion();+ let isOutdated = await getIsOutdated(latestVersion);+ if (+ !isOutdated ||+ (await loadPrefs())['flags.updateNotificationShownForVersion'] ===+ latestVersion+ ) {+ return;+ }++ await savePrefs({+ 'flags.updateNotificationShownForVersion': latestVersion,+ });+ addNotification({+ type: 'message',+ title: 'A new version of Actual is available!',+ message: `Version ${latestVersion} of Actual was recently released.`,+ sticky: true,+ id: 'update-notification',+ button: {+ title: 'Open changelog',+ action: () => {+ window.open('https://actualbudget.github.io/docs/Release-Notes');+ },+ },+ });+}