From 2a71bfc0756011c0226c16ef53a755a47cf44acb Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 21 Aug 2024 23:03:38 +0800 Subject: [PATCH] feat: toast when upgrade Signed-off-by: Innei --- src/renderer/src/App.tsx | 33 ++++++++++++++-------------- src/renderer/src/atoms/app.ts | 2 +- src/renderer/src/initialize/index.ts | 28 ++++++++++++++++++++++- src/renderer/src/initialize/queue.ts | 16 ++++++++++++++ 4 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 src/renderer/src/initialize/queue.ts diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 0fc520bb5..5715e70ed 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -5,6 +5,7 @@ import { Outlet } from "react-router-dom" import { env } from "../../env.js" import { useAppIsReady } from "./atoms/app" import { useUISettingKey } from "./atoms/settings/ui" +import { applyAfterReadyCallbacks } from "./initialize/queue.js" import { appLog } from "./lib/log" import { cn, getOS } from "./lib/utils" import { Titlebar } from "./modules/app/Titlebar.js" @@ -43,22 +44,20 @@ function App() { const windowsElectron = window.electron && getOS() === "Windows" return ( - <> - - {window.electron && ( -
- {windowsElectron && } -
- )} - -
- + + {window.electron && ( +
+ {windowsElectron && } +
+ )} + +
) } @@ -71,6 +70,8 @@ const AppLayer = () => { time: doneTime, }) appLog("App is ready", `${doneTime}ms`) + + applyAfterReadyCallbacks() }, [appIsReady]) return appIsReady ? : diff --git a/src/renderer/src/atoms/app.ts b/src/renderer/src/atoms/app.ts index bc77af214..6a5807919 100644 --- a/src/renderer/src/atoms/app.ts +++ b/src/renderer/src/atoms/app.ts @@ -3,7 +3,7 @@ import { getStorageNS } from "@renderer/lib/ns" import { atom } from "jotai" import { atomWithStorage } from "jotai/utils" -export const [, , useAppIsReady, , , setAppIsReady] = createAtomHooks( +export const [, , useAppIsReady, , appIsReady, setAppIsReady] = createAtomHooks( atom(false), ) diff --git a/src/renderer/src/initialize/index.ts b/src/renderer/src/initialize/index.ts index 68f538edf..0213c22b3 100644 --- a/src/renderer/src/initialize/index.ts +++ b/src/renderer/src/initialize/index.ts @@ -13,6 +13,7 @@ import duration from "dayjs/plugin/duration" import localizedFormat from "dayjs/plugin/localizedFormat" import relativeTime from "dayjs/plugin/relativeTime" import { enableMapSet } from "immer" +import { createElement } from "react" import { toast } from "sonner" import { subscribeNetworkStatus } from "../atoms/network" @@ -27,6 +28,7 @@ import { setHydrated, } from "./hydrate" import { initPostHog } from "./posthog" +import { pushAfterReadyCallback } from "./queue" import { initSentry } from "./sentry" const cleanup = subscribeShouldUseIndexedDB((value) => { @@ -64,7 +66,31 @@ export const initializeApp = async () => { if (lastVersion && lastVersion !== APP_VERSION) { appLog(`Upgrade from ${lastVersion} to ${APP_VERSION}`) - // TODO + + pushAfterReadyCallback(() => { + setTimeout(() => { + toast.success( + // `App is upgraded to ${APP_VERSION}, enjoy the new features! 🎉`, + createElement("div", { + children: [ + "App is upgraded to ", + createElement( + "a", + { + href: `${repository.url}/releases/tag/${APP_VERSION}`, + target: "_blank", + className: "underline", + }, + createElement("strong", { + children: APP_VERSION, + }), + ), + ", enjoy the new features! 🎉", + ], + }), + ) + }, 1000) + }) } localStorage.setItem(appVersionKey, APP_VERSION) diff --git a/src/renderer/src/initialize/queue.ts b/src/renderer/src/initialize/queue.ts new file mode 100644 index 000000000..1991e541a --- /dev/null +++ b/src/renderer/src/initialize/queue.ts @@ -0,0 +1,16 @@ +import { appIsReady } from "@renderer/atoms/app" + +const afterReadyCallbackQueue = [] as Array<() => void> + +export const pushAfterReadyCallback = (callback: () => void) => { + if (appIsReady()) { + callback() + } else { + afterReadyCallbackQueue.push(callback) + } +} + +export const applyAfterReadyCallbacks = () => { + afterReadyCallbackQueue.forEach((callback) => callback()) + afterReadyCallbackQueue.length = 0 +}