diff --git a/src/app/mod.rs b/src/app/mod.rs index f57f1f8..6ec01dd 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -151,7 +151,7 @@ pub fn run() -> Result<()> { tauri::WindowEvent::CloseRequested { api, .. } => { info!(" [窗口] 关闭请求 - 隐藏窗口到托盘"); // 隐藏窗口而不是关闭 - window.hide().unwrap(); + let _ = window.hide(); api.prevent_close(); } tauri::WindowEvent::Focused(focused) => { diff --git a/web/src/App.tsx b/web/src/App.tsx index eab3b17..0f4ccbe 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -27,19 +27,28 @@ function App() { applyTheme(currentTheme) } catch (e) { console.error('Failed to load theme:', e) + // 使用默认主题,不影响页面显示 + applyTheme('system') } } loadTheme() // 监听主题变化事件 - const unlisten = window.__TAURI__.listen('theme-change', (event) => { + let unlistenFn: (() => void) | null = null + window.__TAURI__.listen('theme-change', (event) => { const newTheme = event.payload as string setTheme(newTheme as Theme) applyTheme(newTheme as Theme) + }).then(fn => { + unlistenFn = fn + }).catch(e => { + console.error('Failed to setup theme listener:', e) }) return () => { - unlisten.then(fn => fn()) + if (unlistenFn) { + unlistenFn() + } } }, []) diff --git a/web/src/components/ErrorBoundary.tsx b/web/src/components/ErrorBoundary.tsx new file mode 100644 index 0000000..3834338 --- /dev/null +++ b/web/src/components/ErrorBoundary.tsx @@ -0,0 +1,89 @@ +import { Component, ErrorInfo, ReactNode } from 'react' + +interface Props { + children: ReactNode + fallback?: ReactNode +} + +interface State { + hasError: boolean + error: Error | null +} + +export class ErrorBoundary extends Component { + public state: State = { + hasError: false, + error: null + } + + public static getDerivedStateFromError(error: Error): State { + return { hasError: true, error } + } + + public componentDidCatch(error: Error, errorInfo: ErrorInfo): void { + console.error('ErrorBoundary caught an error:', error, errorInfo) + } + + public render(): ReactNode { + if (this.state.hasError) { + if (this.props.fallback) { + return this.props.fallback + } + + return ( +
+

+ 发生错误 +

+

+ 抱歉,页面加载时出现问题。请尝试刷新页面或重启应用。 +

+ {this.state.error && ( +
+ + 错误详情(点击展开) + + {this.state.error.toString()} +
+ )} + +
+ ) + } + + return this.props.children + } +} + +export default ErrorBoundary diff --git a/web/src/main.tsx b/web/src/main.tsx index 791f139..481bd1a 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -1,10 +1,13 @@ import React from 'react' import ReactDOM from 'react-dom/client' import App from './App' +import ErrorBoundary from './components/ErrorBoundary' import './index.css' ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - + + + , )