fix: 前端 HIGH/MEDIUM — timer 清理 + 历史持久化 + Error Boundary + console 移除
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { useCallback, useRef } from 'react';
|
||||
import { useCallback, useRef, useEffect } from 'react';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { Store } from '@tauri-apps/plugin-store';
|
||||
import { useQrState } from '../store/qrContext';
|
||||
import type { HistoryEntry } from '../types';
|
||||
|
||||
const HISTORY_KEY = 'qr-history';
|
||||
|
||||
interface QrResponse {
|
||||
svg: string;
|
||||
@@ -15,6 +19,13 @@ export function useQrEncode() {
|
||||
const modeRef = useRef(state.mode);
|
||||
modeRef.current = state.mode;
|
||||
|
||||
// 组件卸载时清理定时器
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timerRef.current) clearTimeout(timerRef.current);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const encode = useCallback((text: string) => {
|
||||
if (!text.trim()) {
|
||||
dispatch({ type: 'SET_PREVIEW', payload: null });
|
||||
@@ -33,18 +44,26 @@ export function useQrEncode() {
|
||||
});
|
||||
dispatch({ type: 'SET_PREVIEW', payload: result });
|
||||
|
||||
// 保存到历史
|
||||
dispatch({
|
||||
type: 'ADD_HISTORY',
|
||||
payload: {
|
||||
id: Date.now().toString(),
|
||||
mode: modeRef.current,
|
||||
content: text,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
});
|
||||
// 保存到历史(内存 + 持久化)
|
||||
const entryId = Date.now().toString();
|
||||
const entry: HistoryEntry = {
|
||||
id: entryId,
|
||||
mode: modeRef.current,
|
||||
content: text,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
dispatch({ type: 'ADD_HISTORY', payload: entry });
|
||||
|
||||
// 持久化到 tauri-plugin-store
|
||||
try {
|
||||
const store = await Store.load('history.json');
|
||||
const current = await store.get<HistoryEntry[]>(HISTORY_KEY) || [];
|
||||
const updated = [entry, ...current].slice(0, 50);
|
||||
await store.set(HISTORY_KEY, updated);
|
||||
await store.save();
|
||||
} catch { /* store 不可用时静默忽略 */ }
|
||||
} catch (e) {
|
||||
console.error('QR 编码失败:', e);
|
||||
// 编码失败已在 dispatch SET_PREVIEW(null) 中处理,无需额外日志
|
||||
dispatch({ type: 'SET_PREVIEW', payload: null });
|
||||
}
|
||||
}, 200);
|
||||
|
||||
Reference in New Issue
Block a user