6d2b553ca4
refactor(storage): 将storage工具类重命名为chromeStorage 重命名utils/storage.ts为utils/chromeStorage.ts以更好地反映其功能用途 feat(messages): 简化消息协议定义并移除过时的消息类型 将messages.tsx中的硬编码消息对象替换为ProtocolMap接口定义, 移除了不再使用的popup、content和offscreen相关消息类型, 使消息系统更加简洁和类型安全 ```
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
|
import { storage } 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 storage.get('app/lastRoute');
|
|
|
|
if (lastRoute && lastRoute !== '/' && location.pathname === '/') {
|
|
navigate(lastRoute, { replace: true });
|
|
console.log('跳转路由', lastRoute);
|
|
}
|
|
} catch (err) {
|
|
console.error('恢复路由失败', err);
|
|
} finally {
|
|
isRestored.current = true;
|
|
}
|
|
};
|
|
|
|
restoreRoute().then(() => console.info('恢复路由成功'));
|
|
}, [location, navigate]);
|
|
|
|
useEffect(() => {
|
|
const saveRoute = async () => {
|
|
if (!isRestored.current) return;
|
|
await storage.set('app/lastRoute', location.pathname);
|
|
console.log('保存路由', location.pathname);
|
|
};
|
|
|
|
saveRoute().then(() => console.info('保存路由成功'));
|
|
}, [location]);
|
|
|
|
return null;
|
|
};
|
|
|
|
export default RoutePersistence;
|