diff --git a/app/components/chat.tsx b/app/components/chat.tsx index fd62bf25ccd..dea078e1383 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -98,6 +98,7 @@ import { ExportMessageModal } from "./exporter"; import { getClientConfig } from "../config/client"; import { useAllModels } from "../utils/hooks"; import { appWindow } from '@tauri-apps/api/window'; +import { sendDesktopNotification } from "../utils/taurinotification"; const Markdown = dynamic(async () => (await import("./markdown")).Markdown, { loading: () => , @@ -692,21 +693,11 @@ function usePinApp() { const togglePinApp = async () => { if (pinApp) { await appWindow.setAlwaysOnTop(false); - window.__TAURI__?.notification.sendNotification({ - title: "ChatGPT Next Web", - body: Locale.Chat.Actions.PinAppContent.UnPinned, - icon: `${ChatGptIcon.src}`, - sound: "Default" - }); + sendDesktopNotification(Locale.Chat.Actions.PinAppContent.UnPinned); showToast(Locale.Chat.Actions.PinAppContent.UnPinned); } else { await appWindow.setAlwaysOnTop(true); - window.__TAURI__?.notification.sendNotification({ - title: "ChatGPT Next Web", - body: Locale.Chat.Actions.PinAppContent.Pinned, - icon: `${ChatGptIcon.src}`, - sound: "Default" - }); + sendDesktopNotification(Locale.Chat.Actions.PinAppContent.Pinned); showToast(Locale.Chat.Actions.PinAppContent.Pinned); } setPinApp(!pinApp); diff --git a/app/store/update.ts b/app/store/update.ts index 333d978f831..716fafd9eda 100644 --- a/app/store/update.ts +++ b/app/store/update.ts @@ -5,6 +5,7 @@ import { createPersistStore } from "../utils/store"; import ChatGptIcon from "../icons/chatgpt.png"; import Locale from "../locales"; import { showToast } from "../components/ui-lib"; +import { sendDesktopNotification } from "../utils/taurinotification"; const ONE_MINUTE = 60 * 1000; const isApp = !!getClientConfig()?.isApp; @@ -100,47 +101,24 @@ export const useUpdateStore = createPersistStore( set(() => ({ remoteVersion: remoteId, })); - if (window.__TAURI__?.notification && isApp) { - // Check if notification permission is granted - await window.__TAURI__?.notification.isPermissionGranted().then((granted) => { - if (!granted) { - return; - } else { - // Request permission to show notifications - window.__TAURI__?.notification.requestPermission().then((permission) => { - if (permission === 'granted') { - if (version === remoteId) { - // Show a notification using Tauri - window.__TAURI__?.notification.sendNotification({ - title: "ChatGPT Next Web", - body: `${Locale.Settings.Update.IsLatest}`, - icon: `${ChatGptIcon.src}`, - sound: "Default" - }); - } else { - const updateMessage = Locale.Settings.Update.FoundUpdate(`${remoteId}`); - // Show a notification for the new version using Tauri - window.__TAURI__?.notification.sendNotification({ - title: "ChatGPT Next Web", - body: updateMessage, - icon: `${ChatGptIcon.src}`, - sound: "Default" - }); - // this a wild for updating client app - window.__TAURI__?.updater.checkUpdate().then((updateResult) => { - if (updateResult.status === "DONE") { - window.__TAURI__?.updater.installUpdate(); - showToast(Locale.Settings.Update.UpdateSuccessful); - } - }).catch((e) => { - console.error("[Check Update Error]", e); - showToast(Locale.Settings.Update.UpdateFailed); - }); - } - } - }); - } - }); + + if (isApp) { + if (remoteId !== version) { + const foundUpdateMessage = Locale.Settings.Update.FoundUpdate(`${remoteId}`); + // Show a notification for the new version using Tauri Notification + sendDesktopNotification(foundUpdateMessage); + // this a wild for updating desktop app using Tauri Updater + window.__TAURI__?.updater.checkUpdate().then((updateResult) => { + if (updateResult.status === "DONE") { + window.__TAURI__?.updater.installUpdate(); + } + }).catch((e) => { + console.error("[Check Update Error]", e); + showToast(Locale.Settings.Update.UpdateFailed); + }); + } else { + sendDesktopNotification(Locale.Settings.Update.IsLatest); + } } console.log("[Got Upstream] ", remoteId); } catch (error) { diff --git a/app/utils/taurinotification.ts b/app/utils/taurinotification.ts new file mode 100644 index 00000000000..9ded8a6b4e6 --- /dev/null +++ b/app/utils/taurinotification.ts @@ -0,0 +1,30 @@ +import { getClientConfig } from "../config/client"; +import ChatGptIcon from "../icons/chatgpt.png"; + +export async function sendDesktopNotification(body: string) { + const isApp = getClientConfig(); + + if (window.__TAURI__?.notification && isApp) { + // Check if notification permission is granted + await window.__TAURI__?.notification. + isPermissionGranted().then((granted) => { + if (!granted) { + return; + } else { + // Request permission to show notifications + window.__TAURI__?.notification. + requestPermission().then((permission) => { + if (permission === 'granted') { + // Show a notification using Tauri + window.__TAURI__?.notification.sendNotification({ + title: "ChatGPT Next Web", + body: body, + icon: `${ChatGptIcon.src}`, + sound: "Default" + }); + } + }); + } + }); + } +}