diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs index 690860f..3b83a44 100644 --- a/src-tauri/src/app/menu.rs +++ b/src-tauri/src/app/menu.rs @@ -24,6 +24,7 @@ pub fn init() -> Menu { MenuItem::About(name.into(), AboutMetadata::default()).into(), #[cfg(not(target_os = "macos"))] CustomMenuItem::new("about".to_string(), "About ChatGPT").into(), + CustomMenuItem::new("check_update".to_string(), "Check for Updates").into(), MenuItem::Services.into(), MenuItem::Hide.into(), MenuItem::HideOthers.into(), @@ -192,6 +193,17 @@ pub fn menu_handler(event: WindowMenuEvent) { format!("Version {}", tauri_conf.package.version.unwrap()), ); } + "check_update" => { + tauri::async_runtime::spawn(async move { + let result = app.updater().check().await; + let update_resp = result.unwrap(); + if update_resp.is_update_available() { + tauri::async_runtime::spawn(async move { + utils::prompt_for_install(app, update_resp).await.unwrap(); + }); + } + }); + } // Preferences "control_center" => window::control_window(&app), "restart" => tauri::api::process::restart(&app.env()), diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index 61a153d..0d5145d 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -8,7 +8,8 @@ use std::{ path::{Path, PathBuf}, process::Command, }; -use tauri::{utils::config::Config, Manager}; +use tauri::{utils::config::Config, Manager, AppHandle, Wry}; +use tauri::updater::UpdateResponse; pub fn chat_root() -> PathBuf { tauri::api::path::home_dir().unwrap().join(".chatgpt") @@ -128,3 +129,50 @@ pub async fn get_data( Ok(None) } } +// Copy private api in tauri/updater/mod.rs. TODO: refactor to public api +// Prompt a dialog asking if the user want to install the new version +// Maybe we should add an option to customize it in future versions. +pub async fn prompt_for_install ( + app: AppHandle, + update: UpdateResponse +) -> Result<()> { + let windows = app.windows(); + let parent_window = windows.values().next(); + let package_info = app.package_info().clone(); + + let body = update.body().unwrap(); + // todo(lemarier): We should review this and make sure we have + // something more conventional. + let should_install = tauri::api::dialog::blocking::ask( + parent_window, + format!(r#"A new version of {} is available! "#, package_info.name), + format!( + r#"{} {} is now available -- you have {}. + +Would you like to install it now? + +Release Notes: +{}"#, + package_info.name, update.latest_version(), package_info.version, body), + ); + + if should_install { + // Launch updater download process + // macOS we display the `Ready to restart dialog` asking to restart + // Windows is closing the current App and launch the downloaded MSI when ready (the process stop here) + // Linux we replace the AppImage by launching a new install, it start a new AppImage instance, so we're closing the previous. (the process stop here) + update.download_and_install().await?; + + // Ask user if we need to restart the application + let should_exit = tauri::api::dialog::blocking::ask( + parent_window, + "Ready to Restart", + "The installation was successful, do you want to restart the application now?", + ); + if should_exit { + app.restart(); + } + } + + Ok(()) +} diff --git a/src/layout/index.scss b/src/layout/index.scss index 9c0a917..00ef8bf 100644 --- a/src/layout/index.scss +++ b/src/layout/index.scss @@ -7,7 +7,10 @@ height: 48px; } } - +.chat-info { + text-align: center; + font-weight: bold; +} .ant-layout-sider-trigger { user-select: none; -webkit-user-select: none; diff --git a/src/layout/index.tsx b/src/layout/index.tsx index 36c514e..9ca866a 100644 --- a/src/layout/index.tsx +++ b/src/layout/index.tsx @@ -1,12 +1,19 @@ import { FC, useState } from 'react'; -import { Layout, Menu } from 'antd'; +import {Layout, Menu, Button, Tooltip, message} from 'antd'; +import { SyncOutlined } from '@ant-design/icons'; import { useNavigate, useLocation } from 'react-router-dom'; +import { getName, getVersion } from '@tauri-apps/api/app'; +import { checkUpdate, installUpdate } from '@tauri-apps/api/updater'; +import { relaunch } from '@tauri-apps/api/process'; import Routes, { menuItems } from '@/routes'; import './index.scss'; const { Content, Footer, Sider } = Layout; +const appName = await getName(); +const appVersion = await getVersion(); + interface ChatLayoutProps { children?: React.ReactNode; } @@ -16,6 +23,14 @@ const ChatLayout: FC = ({ children }) => { const location = useLocation(); const go = useNavigate(); + const checkAppUpdate = async () => { + try { + await checkUpdate(); + }catch (e) { + console.log(e) + } + } + return ( = ({ children }) => { }} >
+
+ {appName} +
+
+ {appVersion} + + { + + + + } +
+