f0bb1b21ab
- 将 utils/chromeStorage.ts 中的 storage 导出重命名为 storageUtil - 更新 RoutePersistence.tsx 中的导入和引用以使用新名称 - 优化 recordUtils.tsx 中 chrome.downloads 的链式调用格式 使工具类的命名更加清晰,避免与全局 storage 概念混淆
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 { 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 });
|
|
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 storageUtil.set('app/lastRoute', location.pathname);
|
|
console.log('保存路由', location.pathname);
|
|
};
|
|
|
|
saveRoute().then(() => console.info('保存路由成功'));
|
|
}, [location]);
|
|
|
|
return null;
|
|
};
|
|
|
|
export default RoutePersistence;
|