1
0
mirror of https://github.com/lencx/ChatGPT.git synced 2024-10-01 01:06:13 -04:00

Manually check for updates

This commit is contained in:
tk103331 2023-01-04 12:43:26 +08:00
parent b8614e73ff
commit c19698bc41
4 changed files with 94 additions and 3 deletions

View File

@ -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<tauri::Wry>) {
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()),

View File

@ -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<Wry>,
update: UpdateResponse<Wry>
) -> 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(())
}

View File

@ -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;

30
src/layout/index.tsx vendored
View File

@ -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<ChatLayoutProps> = ({ children }) => {
const location = useLocation();
const go = useNavigate();
const checkAppUpdate = async () => {
try {
await checkUpdate();
}catch (e) {
console.log(e)
}
}
return (
<Layout style={{ minHeight: '100vh' }} hasSider>
<Sider
@ -34,6 +49,19 @@ const ChatLayout: FC<ChatLayoutProps> = ({ children }) => {
}}
>
<div className="chat-logo"><img src="/logo.png" /></div>
<div className="chat-info">
<span>{appName}</span>
</div>
<div className="chat-info">
<span>{appVersion}</span>
<span> </span>
{
<Tooltip title="click to check update">
<a onClick={checkAppUpdate}><SyncOutlined /></a>
</Tooltip>
}
</div>
<Menu
defaultSelectedKeys={[location.pathname]}
mode="inline"