Skip to content

Commit

Permalink
feat: toast when upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 21, 2024
1 parent 124b0bd commit 2a71bfc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
33 changes: 17 additions & 16 deletions src/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -43,22 +44,20 @@ function App() {

const windowsElectron = window.electron && getOS() === "Windows"
return (
<>
<RootProviders>
{window.electron && (
<div
className={cn(
"drag-region absolute inset-x-0 top-0 h-12 shrink-0",
windowsElectron && "pointer-events-none z-[9999]",
)}
aria-hidden
>
{windowsElectron && <Titlebar />}
</div>
)}
<AppLayer />
</RootProviders>
</>
<RootProviders>
{window.electron && (
<div
className={cn(
"drag-region absolute inset-x-0 top-0 h-12 shrink-0",
windowsElectron && "pointer-events-none z-[9999]",
)}
aria-hidden
>
{windowsElectron && <Titlebar />}
</div>
)}
<AppLayer />
</RootProviders>
)
}

Expand All @@ -71,6 +70,8 @@ const AppLayer = () => {
time: doneTime,
})
appLog("App is ready", `${doneTime}ms`)

applyAfterReadyCallbacks()
}, [appIsReady])

return appIsReady ? <Outlet /> : <AppSkeleton />
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/atoms/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

Expand Down
28 changes: 27 additions & 1 deletion src/renderer/src/initialize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +28,7 @@ import {
setHydrated,
} from "./hydrate"
import { initPostHog } from "./posthog"
import { pushAfterReadyCallback } from "./queue"
import { initSentry } from "./sentry"

const cleanup = subscribeShouldUseIndexedDB((value) => {
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions src/renderer/src/initialize/queue.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2a71bfc

Please sign in to comment.