refactor: 优化 RouterProvider 组件的数据加载逻辑

- 使用批量获取方法 `getMany` 替代多个单独的 `get` 调用,简化数据加载过程。
- 更新 `loadInitialData` 函数,确保从存储中获取的默认值更为一致。
- 修改测试用例以验证批量获取的功能,确保组件在不同情况下的正确性。
This commit is contained in:
2026-06-30 22:56:31 +08:00
parent 27e03ae038
commit 17a4dbd8a7
10 changed files with 138 additions and 58 deletions
+9 -1
View File
@@ -7,7 +7,7 @@ import {
type SetStateAction,
} from 'react';
import { storageUtil } from '@/utils/chromeStorage';
import { getSyncSnapshot } from '@/utils/syncSnapshot';
import { getSyncSnapshot, hasSyncSnapshot } from '@/utils/syncSnapshot';
import type { StorageSchema } from '@/types/storage';
export const useStorageState = <K extends keyof StorageSchema>(
@@ -34,6 +34,14 @@ export const useStorageState = <K extends keyof StorageSchema>(
let cancelled = false;
const loadState = async () => {
if (hasSyncSnapshot(key as string)) {
if (!cancelled) {
setIsInitialized(true);
hasLoadedFromStorage.current = true;
}
return;
}
try {
const savedValue = await storageUtil.get(key, defaultValue);
if (cancelled) return;