9ff6b8985c
- 创建 CLAUDE.md 项目开发指导文档 - 提取公共常量到 components/constants.ts - 修复 TimestampToDatetime 重复插件扩展问题 - 修复 DatetimeToTimestamp 时区解析问题 - 优化 RoutePersistence 移除不必要依赖 - 添加 Vitest 测试框架配置 - 为所有组件编写单元测试 (16个测试用例) - 更新 .gitignore 忽略测试结果目录 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
|
import { storageUtil } from '@/utils/chromeStorage';
|
|
|
|
const RoutePersistence = () => {
|
|
const location = useLocation();
|
|
const navigate = useNavigate();
|
|
|
|
const isRestored = useRef(false);
|
|
|
|
useEffect(() => {
|
|
const restoreRoute = async () => {
|
|
if (isRestored.current) return;
|
|
|
|
try {
|
|
const lastRoute = await storageUtil.get('app/lastRoute');
|
|
|
|
if (lastRoute && lastRoute !== '/' && location.pathname === '/') {
|
|
navigate(lastRoute, { replace: true });
|
|
}
|
|
} catch (err) {
|
|
console.error('恢复路由失败', err);
|
|
} finally {
|
|
isRestored.current = true;
|
|
}
|
|
};
|
|
|
|
restoreRoute();
|
|
}, [navigate, location.pathname]);
|
|
|
|
useEffect(() => {
|
|
const saveRoute = async () => {
|
|
if (!isRestored.current) return;
|
|
await storageUtil.set('app/lastRoute', location.pathname);
|
|
};
|
|
|
|
saveRoute();
|
|
}, [location]);
|
|
|
|
return null;
|
|
};
|
|
|
|
export default RoutePersistence;
|