From 4299338ce900b73b6d1171be735096fc4ccf62a7 Mon Sep 17 00:00:00 2001 From: Andy Brenneke Date: Wed, 18 Oct 2023 11:21:24 -0700 Subject: [PATCH] Finish updater --- packages/app/src/components/SettingsModal.tsx | 63 ++++++++++++++++++- packages/app/src/components/UpdateModal.tsx | 8 ++- packages/app/src/hooks/useCheckForUpdate.tsx | 33 ++++++++-- packages/app/src/state/settings.ts | 8 ++- 4 files changed, 101 insertions(+), 11 deletions(-) diff --git a/packages/app/src/components/SettingsModal.tsx b/packages/app/src/components/SettingsModal.tsx index 103fcf58c..71ece19bc 100644 --- a/packages/app/src/components/SettingsModal.tsx +++ b/packages/app/src/components/SettingsModal.tsx @@ -1,11 +1,12 @@ import { type FC, useState } from 'react'; -import { atom, useRecoilState } from 'recoil'; +import { atom, useRecoilState, useRecoilValue } from 'recoil'; import { defaultExecutorState, executorOptions, previousDataPerNodeToKeepState, recordExecutionsState, settingsState, + skippedMaxVersionState, themeState, themes, } from '../state/settings.js'; @@ -23,6 +24,7 @@ import CrossIcon from '@atlaskit/icon/glyph/cross'; import { css } from '@emotion/react'; import Toggle from '@atlaskit/toggle'; import { KeyValuePairs } from './editors/KeyValuePairEditor'; +import { useCheckForUpdate } from '../hooks/useCheckForUpdate'; interface SettingsModalProps {} @@ -45,7 +47,7 @@ const modalBody = css` } `; -type Pages = 'general' | 'openai' | 'plugins'; +type Pages = 'general' | 'openai' | 'plugins' | 'updates'; const buttonsContainer = css` > button span { @@ -63,7 +65,7 @@ export const SettingsModal: FC = () => { return ( {isOpen && ( - + Settings + + )} + + {skippedMaxVersion && ( + + {() => ( + <> + +
You have skipped version {skippedMaxVersion}. You may update by clicking the button above.
+ + )} +
+ )} + + ); +}; diff --git a/packages/app/src/components/UpdateModal.tsx b/packages/app/src/components/UpdateModal.tsx index c96a3d3f4..f8c4254a5 100644 --- a/packages/app/src/components/UpdateModal.tsx +++ b/packages/app/src/components/UpdateModal.tsx @@ -2,7 +2,7 @@ import { useState, type FC, useEffect } from 'react'; import Modal, { ModalTransition, ModalBody, ModalFooter, ModalHeader, ModalTitle } from '@atlaskit/modal-dialog'; import { useRecoilState, useSetRecoilState } from 'recoil'; -import { updateModalOpenState, updateStatusState } from '../state/settings'; +import { skippedMaxVersionState, updateModalOpenState, updateStatusState } from '../state/settings'; import Button from '@atlaskit/button'; import useAsyncEffect from 'use-async-effect'; import { checkUpdate, installUpdate, onUpdaterEvent } from '@tauri-apps/api/updater'; @@ -26,6 +26,7 @@ export const UpdateModal: FC = () => { const setModalOpen = useSetRecoilState(updateModalOpenState); const [isUpdating, setIsUpdating] = useState(false); const [updateStatus, setUpdateStatus] = useRecoilState(updateStatusState); + const setSkippedMaxVersion = useSetRecoilState(skippedMaxVersionState); const [currentVersion, setCurrentVersion] = useState(''); const [latestVersion, setLatestVersion] = useState(''); @@ -64,7 +65,10 @@ export const UpdateModal: FC = () => { } }, [updateStatus]); - const skipUpdate = () => {}; + const skipUpdate = () => { + setSkippedMaxVersion(latestVersion); + handleModalClose(); + }; const canRender = currentVersion && latestVersion && updateBody; diff --git a/packages/app/src/hooks/useCheckForUpdate.tsx b/packages/app/src/hooks/useCheckForUpdate.tsx index 8fe04de3e..a9f08bc69 100644 --- a/packages/app/src/hooks/useCheckForUpdate.tsx +++ b/packages/app/src/hooks/useCheckForUpdate.tsx @@ -4,8 +4,10 @@ import useAsyncEffect from 'use-async-effect'; import { toast } from 'react-toastify'; import { css } from '@emotion/react'; import { isInTauri } from '../utils/tauri'; -import { useSetRecoilState } from 'recoil'; -import { updateModalOpenState } from '../state/settings'; +import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; +import { checkForUpdatesState, skippedMaxVersionState, updateModalOpenState } from '../state/settings'; +import { gt, lt, lte } from 'semver'; +import { getVersion } from '@tauri-apps/api/app'; const toastStyle = css` display: flex; @@ -38,13 +40,32 @@ const toastStyle = css` } `; -export function useCheckForUpdate() { +export function useCheckForUpdate({ + notifyNoUpdates = false, + force = false, +}: { notifyNoUpdates?: boolean; force?: boolean } = {}) { const setUpdateModalOpen = useSetRecoilState(updateModalOpenState); + const checkForUpdates = useRecoilValue(checkForUpdatesState); + const [skippedMaxVersion, setSkippedMaxVersion] = useRecoilState(skippedMaxVersionState); return async () => { + if (!checkForUpdates) { + return; + } + const { shouldUpdate, manifest } = await checkUpdate(); - if (shouldUpdate) { + if (!manifest) { + return; + } + + const shouldSkip = skippedMaxVersion == null ? false : lte(manifest.version, skippedMaxVersion); + + if (force) { + setSkippedMaxVersion(undefined); + } + + if (shouldUpdate && (force || !shouldSkip)) { toast.success( ({ closeToast }) => (
@@ -53,7 +74,7 @@ export function useCheckForUpdate() { - +
@@ -63,6 +84,8 @@ export function useCheckForUpdate() { closeButton: false, }, ); + } else if (notifyNoUpdates) { + toast.info('Rivet is up to date!'); } }; } diff --git a/packages/app/src/state/settings.ts b/packages/app/src/state/settings.ts index 368bd5904..db9b473fe 100644 --- a/packages/app/src/state/settings.ts +++ b/packages/app/src/state/settings.ts @@ -66,7 +66,13 @@ export const previousDataPerNodeToKeepState = atom({ effects_UNSTABLE: [persistAtom], }); -export const skippedMaxVersion = atom({ +export const checkForUpdatesState = atom({ + key: 'checkForUpdates', + default: true, + effects_UNSTABLE: [persistAtom], +}); + +export const skippedMaxVersionState = atom({ key: 'skippedMaxVersion', default: undefined, effects_UNSTABLE: [persistAtom],