From e2dfda9f1d447cab0db9f761b0f10e7b87a9e4a8 Mon Sep 17 00:00:00 2001 From: BiggerRain <15911122312@163.COM> Date: Mon, 6 Jan 2025 10:50:44 +0800 Subject: [PATCH] fix: Esc key failure issue & removing unused code related to the tauri-nspanel plugin & introduct the server variable name (#85) * fix: Esc key failure issue * feat: add env * chore: modify the environment variable name * chore: modify websocket id name * chore: remove tauri-nspanel --- .env | 3 ++ .vscode/settings.json | 1 + package.json | 1 + pnpm-lock.yaml | 22 ++++++++ src-tauri/Cargo.lock | 37 ------------- src-tauri/Cargo.toml | 2 +- src-tauri/capabilities/default.json | 3 ++ src-tauri/src/lib.rs | 84 ++++++++++++++--------------- src/api/request.ts | 6 ++- src/api/tauriFetchClient.ts | 4 +- src/components/ChatAI/Chat.tsx | 3 +- src/components/ChatAI/index.tsx | 3 +- src/hooks/useEscape.ts | 1 + src/utils/env.ts | 7 +++ src/vite-env.d.ts | 10 ++++ vite.config.ts | 8 ++- 16 files changed, 108 insertions(+), 87 deletions(-) create mode 100644 .env create mode 100644 src/utils/env.ts diff --git a/.env b/.env new file mode 100644 index 00000000..8f03c3af --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +COCO_SERVER_URL=https://coco.infini.cloud # http://localhost:2900 + +COCO_WEBSOCKET_URL=wss://coco.infini.cloud/ws # ws://localhost:2900/ws \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 88b3a4b7..2afdfe94 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -33,6 +33,7 @@ "unlisten", "unlistener", "unminimize", + "VITE", "webviews", "zustand" ], diff --git a/package.json b/package.json index d9b47c34..84423815 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@tauri-apps/plugin-window": "2.0.0-alpha.1", "axios": "^1.7.7", "clsx": "^2.1.1", + "dotenv": "^16.4.7", "i18next": "^23.16.2", "lodash": "^4.17.21", "lucide-react": "^0.453.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a71c6f01..6ec0b1b3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,6 +41,9 @@ importers: clsx: specifier: ^2.1.1 version: 2.1.1 + dotenv: + specifier: ^16.4.7 + version: 16.4.7 i18next: specifier: ^23.16.2 version: 23.16.2 @@ -533,46 +536,55 @@ packages: resolution: {integrity: sha512-0KXvIJQMOImLCVCz9uvvdPgfyWo93aHHp8ui3FrtOP57svqrF/roSSR5pjqL2hcMp0ljeGlU4q9o/rQaAQ3AYA==} cpu: [arm] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.24.0': resolution: {integrity: sha512-it2BW6kKFVh8xk/BnHfakEeoLPv8STIISekpoF+nBgWM4d55CZKc7T4Dx1pEbTnYm/xEKMgy1MNtYuoA8RFIWw==} cpu: [arm] os: [linux] + libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.24.0': resolution: {integrity: sha512-i0xTLXjqap2eRfulFVlSnM5dEbTVque/3Pi4g2y7cxrs7+a9De42z4XxKLYJ7+OhE3IgxvfQM7vQc43bwTgPwA==} cpu: [arm64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.24.0': resolution: {integrity: sha512-9E6MKUJhDuDh604Qco5yP/3qn3y7SLXYuiC0Rpr89aMScS2UAmK1wHP2b7KAa1nSjWJc/f/Lc0Wl1L47qjiyQw==} cpu: [arm64] os: [linux] + libc: [musl] '@rollup/rollup-linux-powerpc64le-gnu@4.24.0': resolution: {integrity: sha512-2XFFPJ2XMEiF5Zi2EBf4h73oR1V/lycirxZxHZNc93SqDN/IWhYYSYj8I9381ikUFXZrz2v7r2tOVk2NBwxrWw==} cpu: [ppc64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.24.0': resolution: {integrity: sha512-M3Dg4hlwuntUCdzU7KjYqbbd+BLq3JMAOhCKdBE3TcMGMZbKkDdJ5ivNdehOssMCIokNHFOsv7DO4rlEOfyKpg==} cpu: [riscv64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-s390x-gnu@4.24.0': resolution: {integrity: sha512-mjBaoo4ocxJppTorZVKWFpy1bfFj9FeCMJqzlMQGjpNPY9JwQi7OuS1axzNIk0nMX6jSgy6ZURDZ2w0QW6D56g==} cpu: [s390x] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.24.0': resolution: {integrity: sha512-ZXFk7M72R0YYFN5q13niV0B7G8/5dcQ9JDp8keJSfr3GoZeXEoMHP/HlvqROA3OMbMdfr19IjCeNAnPUG93b6A==} cpu: [x64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-musl@4.24.0': resolution: {integrity: sha512-w1i+L7kAXZNdYl+vFvzSZy8Y1arS7vMgIy8wusXJzRrPyof5LAb02KGr1PD2EkRcl73kHulIID0M501lN+vobQ==} cpu: [x64] os: [linux] + libc: [musl] '@rollup/rollup-win32-arm64-msvc@4.24.0': resolution: {integrity: sha512-VXBrnPWgBpVDCVY6XF3LEW0pOU51KbaHhccHw6AS6vBWIC60eqsH19DAeeObl+g8nKAz04QFdl/Cefta0xQtUQ==} @@ -631,24 +643,28 @@ packages: engines: {node: '>= 10'} cpu: [arm64] os: [linux] + libc: [glibc] '@tauri-apps/cli-linux-arm64-musl@2.0.3': resolution: {integrity: sha512-I4MVD7nf6lLLRmNQPpe5beEIFM6q7Zkmh77ROA5BNu/+vHNL5kiTMD+bmd10ZL2r753A6pO7AvqkIxcBuIl0tg==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] + libc: [musl] '@tauri-apps/cli-linux-x64-gnu@2.0.3': resolution: {integrity: sha512-C6Jkx2zZGKkoi+sg5FK9GoH/0EvAaOgrZfF5azV5EALGba46g7VpWcZgp9zFUd7K2IzTi+0OOY8TQ2OVfKZgew==} engines: {node: '>= 10'} cpu: [x64] os: [linux] + libc: [glibc] '@tauri-apps/cli-linux-x64-musl@2.0.3': resolution: {integrity: sha512-qi4ghmTfSAl+EEUDwmwI9AJUiOLNSmU1RgiGgcPRE+7A/W+Am9UnxYySAiRbB/gJgTl9sj/pqH5Y9duP1/sqHg==} engines: {node: '>= 10'} cpu: [x64] os: [linux] + libc: [musl] '@tauri-apps/cli-win32-arm64-msvc@2.0.3': resolution: {integrity: sha512-UXxHkYmFesC97qVmZre4vY7oDxRDtC2OeKNv0bH+iSnuUp/ROxzJYGyaelnv9Ybvgl4YVqDCnxgB28qMM938TA==} @@ -1237,6 +1253,10 @@ packages: dompurify@3.1.6: resolution: {integrity: sha512-cTOAhc36AalkjtBpfG6O8JimdTMWNXjiePT2xQH/ppBGi/4uIpmj8eKyIkMJErXWARyINV/sB38yf8JCLF5pbQ==} + dotenv@16.4.7: + resolution: {integrity: sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==} + engines: {node: '>=12'} + eastasianwidth@0.2.0: resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} @@ -3317,6 +3337,8 @@ snapshots: dompurify@3.1.6: {} + dotenv@16.4.7: {} + eastasianwidth@0.2.0: {} electron-to-chromium@1.5.36: {} diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 71531b8d..d15a822e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -546,7 +546,6 @@ dependencies = [ "serde_json", "tauri", "tauri-build", - "tauri-nspanel", "tauri-plugin-autostart", "tauri-plugin-global-shortcut", "tauri-plugin-http", @@ -2328,17 +2327,6 @@ dependencies = [ "malloc_buf", ] -[[package]] -name = "objc-foundation" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" -dependencies = [ - "block", - "objc", - "objc_id", -] - [[package]] name = "objc-sys" version = "0.3.5" @@ -2557,15 +2545,6 @@ dependencies = [ "objc2-foundation", ] -[[package]] -name = "objc_id" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" -dependencies = [ - "objc", -] - [[package]] name = "object" version = "0.36.5" @@ -4023,22 +4002,6 @@ dependencies = [ "tauri-utils", ] -[[package]] -name = "tauri-nspanel" -version = "2.0.0" -source = "git+https://github.com/ahkohd/tauri-nspanel?branch=v2#23b30f0f1974c35673db3234f1f1bd214fa9c4e9" -dependencies = [ - "bitflags 2.6.0", - "block", - "cocoa", - "core-foundation 0.10.0", - "core-graphics", - "objc", - "objc-foundation", - "objc_id", - "tauri", -] - [[package]] name = "tauri-plugin" version = "2.0.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 08222887..08d9a507 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -27,7 +27,7 @@ tauri-plugin-http = "2" tauri-plugin-websocket = "2" tauri-plugin-theme = "2.1.2" -tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v2" } +# tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v2" } [profile.dev] incremental = true # Compile your binary in smaller steps. diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index f7546f1b..7f2ff6a4 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -49,6 +49,9 @@ "allow": [ { "url": "http://localhost:2900" + }, + { + "url": "https://coco.infini.cloud" } ], "deny": [] diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 31c896af..ceb69e2a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,7 +1,7 @@ use std::{fs::create_dir, io::Read}; use tauri::{AppHandle, Emitter, Listener, Manager, Runtime, WebviewWindow}; -use tauri_nspanel::{panel_delegate, ManagerExt, WebviewWindowExt}; +// use tauri_nspanel::{panel_delegate, ManagerExt, WebviewWindowExt}; use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut}; @@ -32,28 +32,28 @@ fn change_window_height(handle: AppHandle, height: u32) { window.set_size(size).unwrap(); } -#[tauri::command] -fn show_panel(handle: AppHandle) { - let panel = handle.get_webview_panel("main").unwrap(); +// #[tauri::command] +// fn show_panel(handle: AppHandle) { +// let panel = handle.get_webview_panel("main").unwrap(); - panel.show(); -} +// panel.show(); +// } -#[tauri::command] -fn hide_panel(handle: AppHandle) { - let panel = handle.get_webview_panel("main").unwrap(); +// #[tauri::command] +// fn hide_panel(handle: AppHandle) { +// let panel = handle.get_webview_panel("main").unwrap(); - panel.order_out(None); -} +// panel.order_out(None); +// } -#[tauri::command] -fn close_panel(handle: AppHandle) { - let panel = handle.get_webview_panel("main").unwrap(); +// #[tauri::command] +// fn close_panel(handle: AppHandle) { +// let panel = handle.get_webview_panel("main").unwrap(); - panel.released_when_closed(true); +// panel.released_when_closed(true); - panel.close(); -} +// panel.close(); +// } #[derive(serde::Deserialize)] struct ThemeChangedPayload { @@ -65,7 +65,7 @@ pub fn run() { let mut ctx = tauri::generate_context!(); tauri::Builder::default() - .plugin(tauri_nspanel::init()) + // .plugin(tauri_nspanel::init()) .plugin(tauri_plugin_http::init()) .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_autostart::init( @@ -81,9 +81,9 @@ pub fn run() { change_autostart, hide_coco, switch_tray_icon, - show_panel, - hide_panel, - close_panel + // show_panel, + // hide_panel, + // close_panel ]) .setup(|app| { init(app.app_handle()); @@ -108,33 +108,33 @@ pub fn run() { .expect("error while running tauri application"); } -fn init(app_handle: &AppHandle) { - let window: WebviewWindow = app_handle.get_webview_window("main").unwrap(); +fn init(_app_handle: &AppHandle) { + // let window: WebviewWindow = app_handle.get_webview_window("main").unwrap(); - let panel = window.to_panel().unwrap(); + // let panel = window.to_panel().unwrap(); - let delegate = panel_delegate!(MyPanelDelegate { - window_did_become_key, - window_did_resign_key - }); + // let delegate = panel_delegate!(MyPanelDelegate { + // window_did_become_key, + // window_did_resign_key + // }); - let handle = app_handle.to_owned(); + // let handle = app_handle.to_owned(); - delegate.set_listener(Box::new(move |delegate_name: String| { - match delegate_name.as_str() { - "window_did_become_key" => { - let app_name = handle.package_info().name.to_owned(); + // delegate.set_listener(Box::new(move |delegate_name: String| { + // match delegate_name.as_str() { + // "window_did_become_key" => { + // let app_name = handle.package_info().name.to_owned(); - println!("[info]: {:?} panel becomes key window!", app_name); - } - "window_did_resign_key" => { - println!("[info]: panel resigned from key window!"); - } - _ => (), - } - })); + // println!("[info]: {:?} panel becomes key window!", app_name); + // } + // "window_did_resign_key" => { + // println!("[info]: panel resigned from key window!"); + // } + // _ => (), + // } + // })); - panel.set_delegate(delegate); + // panel.set_delegate(delegate); } fn enable_shortcut(app: &mut tauri::App) { diff --git a/src/api/request.ts b/src/api/request.ts index 05e11a6d..0f9cb3db 100644 --- a/src/api/request.ts +++ b/src/api/request.ts @@ -1,6 +1,10 @@ import { isTauri } from "@tauri-apps/api/core"; import { fetch as tauriFetchModule } from "@tauri-apps/plugin-http"; +import { clientEnv } from "@/utils/env"; + +const baseURL = `${clientEnv.COCO_SERVER_URL}` + // Use a conditional fetch depending on whether it's in a Tauri environment or web let customFetch: typeof window.fetch | typeof tauriFetchModule = window.fetch; @@ -8,8 +12,6 @@ if (isTauri()) { customFetch = tauriFetchModule; } -const baseURL = "http://localhost:2900"; - interface FetchRequestConfig { url: string; method?: "GET" | "POST" | "PUT" | "DELETE"; diff --git a/src/api/tauriFetchClient.ts b/src/api/tauriFetchClient.ts index 1703af96..5d8d7501 100644 --- a/src/api/tauriFetchClient.ts +++ b/src/api/tauriFetchClient.ts @@ -1,6 +1,8 @@ import { fetch } from "@tauri-apps/plugin-http"; -const baseURL = "http://localhost:2900"; +import { clientEnv } from "@/utils/env"; + +const baseURL = `${clientEnv.COCO_SERVER_URL}` interface FetchRequestConfig { url: string; diff --git a/src/components/ChatAI/Chat.tsx b/src/components/ChatAI/Chat.tsx index 3e2274c2..4eebc4dd 100644 --- a/src/components/ChatAI/Chat.tsx +++ b/src/components/ChatAI/Chat.tsx @@ -14,6 +14,7 @@ import { tauriFetch } from "../../api/tauriFetchClient"; import { useWebSocket } from "../../hooks/useWebSocket"; import { useChatStore } from "../../stores/chatStore"; import { useWindows } from "../../hooks/useWindows"; +import { clientEnv } from "@/utils/env"; interface ChatAIProps { inputValue: string; @@ -51,7 +52,7 @@ const ChatAI = forwardRef( const curIdRef = useRef(curId); curIdRef.current = curId; const { messages, setMessages } = useWebSocket( - "ws://localhost:2900/ws", + `${clientEnv.COCO_WEBSOCKET_URL}`, (msg) => { console.log("msg", msg); if (msg.includes("websocket-session-id")) { diff --git a/src/components/ChatAI/index.tsx b/src/components/ChatAI/index.tsx index fc651374..df0281b7 100644 --- a/src/components/ChatAI/index.tsx +++ b/src/components/ChatAI/index.tsx @@ -9,6 +9,7 @@ import type { Chat, Message } from "./types"; import { tauriFetch } from "../../api/tauriFetchClient"; import { useWebSocket } from "../../hooks/useWebSocket"; import { useWindows } from "../../hooks/useWindows"; +import { clientEnv } from "@/utils/env"; interface ChatAIProps {} @@ -33,7 +34,7 @@ export default function ChatAI({}: ChatAIProps) { const curIdRef = useRef(curId); curIdRef.current = curId; const { messages, setMessages } = useWebSocket( - "ws://localhost:2900/ws", + `${clientEnv.COCO_WEBSOCKET_URL}`, (msg) => { if (msg.includes("websocket-session-id")) { const array = msg.split(" "); diff --git a/src/hooks/useEscape.ts b/src/hooks/useEscape.ts index 3eaf262d..64125b51 100644 --- a/src/hooks/useEscape.ts +++ b/src/hooks/useEscape.ts @@ -14,6 +14,7 @@ const useEscape = () => { useEffect(() => { if(!isTauri()) return; + const unlisten = listen("tauri://focus", () => { // Add event listener for keydown window.addEventListener("keydown", handleEscape); diff --git a/src/utils/env.ts b/src/utils/env.ts new file mode 100644 index 00000000..d1483aeb --- /dev/null +++ b/src/utils/env.ts @@ -0,0 +1,7 @@ +export const clientEnv = { + COCO_SERVER_URL: import.meta.env.COCO_SERVER_URL ?? "https://coco.infini.cloud", + COCO_WEBSOCKET_URL: import.meta.env.COCO_WEBSOCKET_URL ?? "wss://coco.infini.cloud/ws", +}; + +// http://localhost:2900 +// ws://localhost:2900/ws diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 11f02fe2..c075c48c 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1 +1,11 @@ /// + +interface ImportMetaEnv { + readonly COCO_SERVER_URL: string; + readonly COCO_WEBSOCKET_URL: string; + // more env variables... +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +} diff --git a/vite.config.ts b/vite.config.ts index 081229c3..92ee6527 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,8 +1,12 @@ import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import path from 'path'; +import { config } from "dotenv"; + +config(); const host = process.env.TAURI_DEV_HOST; +// console.log("process.env", process.env) // https://vitejs.dev/config/ export default defineConfig(async () => ({ @@ -34,12 +38,12 @@ export default defineConfig(async () => ({ }, proxy: { "/chat": { - target: "http://localhost:2900", + target: process.env.COCO_SERVER_URL, changeOrigin: true, secure: false, }, "/query": { - target: "http://localhost:2900", + target: process.env.COCO_SERVER_URL, changeOrigin: true, secure: false, },