Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Refactor Code Base #109

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => <LoadingIcon />,
Expand Down Expand Up @@ -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);
Expand Down
60 changes: 19 additions & 41 deletions app/store/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions app/utils/taurinotification.ts
Original file line number Diff line number Diff line change
@@ -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"
});
}
});
}
});
}
}