refactor: abstract restore from plugin function

This commit is contained in:
Xu Quan 2025-06-24 17:50:48 +08:00
parent 662a8a1363
commit c21dd76841
6 changed files with 73 additions and 62 deletions

View file

@ -36,6 +36,11 @@ type RestoreDataFromLocalStorageParams = {
needParse?: boolean;
transform?: (value: any) => unknown;
};
type RestoreDataFromPluginParams = {
pluginKey: string;
key: string;
needParse?: boolean;
};
export type CommonExplorerStore = {
viewMode: ViewMode;
@ -61,12 +66,17 @@ export type CommonExplorerStore = {
pluginKey,
pluginValue,
}: SetValueAndSaveInPluginParams<T>) => Promise<void>;
restoreDataFromLocalStorage: ({
restoreDataFromLocalStorage: <T>({
localStorageKey,
key,
needParse,
transform,
}: RestoreDataFromLocalStorageParams) => void;
}: RestoreDataFromLocalStorageParams) => T;
restoreDataFromPlugin: ({
pluginKey,
key,
needParse,
}: RestoreDataFromPluginParams) => Promise<unknown>;
};
export const createCommonExplorerStore =
@ -166,6 +176,7 @@ export const createCommonExplorerStore =
const finalData = transform ? transform(parsed) : parsed;
set({ [key]: finalData } as Partial<ExplorerStore>);
return finalData;
} catch (error) {
const shortData =
raw.length > 200 ? raw.slice(0, 200) + "..." : raw;
@ -183,5 +194,34 @@ export const createCommonExplorerStore =
);
}
},
restoreDataFromPlugin: () => {},
restoreDataFromPlugin: async ({
pluginKey,
key,
needParse = false,
}: RestoreDataFromPluginParams) => {
const { getDataFromPlugin } = get();
const raw = await getDataFromPlugin<string>(pluginKey);
if (!raw) return;
try {
const parsed = needParse ? JSON.parse(raw) : raw;
set({ [key]: parsed } as Partial<ExplorerStore>);
return parsed;
} catch (error) {
const shortData =
raw.length > 200 ? raw.slice(0, 200) + "..." : raw;
console.error(
`[restoreDataFromPlugin] Failed to restore "${String(
key
)}" from plugin key "${pluginKey}".`,
{
error,
rawDataPreview: shortData,
needParse,
// hasTransform: !!transform,
}
);
}
},
});

View file

@ -60,19 +60,11 @@ export const createPinnedFileSlice =
await _updatePinnedFilePaths(updatedPaths);
},
restorePinnedFiles: async () => {
const { getDataFromPlugin: getData } = get();
const pinnedFilePaths = await getData<string>(
FFS_PINNED_FILE_PATHS_KEY
);
if (pinnedFilePaths) {
try {
const filePaths: string[] = JSON.parse(pinnedFilePaths);
set({
pinnedFilePaths: filePaths,
});
} catch (error) {
console.error("Invalid Json format: ", error);
}
}
const { restoreDataFromPlugin } = get();
await restoreDataFromPlugin({
pluginKey: FFS_PINNED_FILE_PATHS_KEY,
key: "pinnedFilePaths",
needParse: true,
});
},
});

View file

@ -81,14 +81,12 @@ export const createSortFileSlice =
});
},
restoreFileSortRule: async () => {
const { getDataFromPlugin: getData, restoreFilesManualSortOrder } =
const { restoreFilesManualSortOrder, restoreDataFromPlugin } =
get();
const lastFileSortRule = await getData<FileSortRule>(
FFS_FILE_SORT_RULE_KEY
);
if (!lastFileSortRule) return;
set({
fileSortRule: lastFileSortRule as FileSortRule,
const lastFileSortRule = await restoreDataFromPlugin({
pluginKey: FFS_FILE_SORT_RULE_KEY,
key: "fileSortRule",
needParse: true,
});
if (lastFileSortRule === FILE_MANUAL_SORT_RULE) {
await restoreFilesManualSortOrder();

View file

@ -54,26 +54,15 @@ export const createPinnedFolderSlice =
await _updatePinnedFolderPaths(folderPaths);
},
restorePinnedFolders: async () => {
const { getDataFromPlugin: getData } = get();
const pinnedFolderPaths = await getData<string>(
FFS_PINNED_FOLDER_PATHS_KEY
);
if (pinnedFolderPaths) {
try {
const folderPaths: string[] = JSON.parse(pinnedFolderPaths);
set({
pinnedFolderPaths: folderPaths,
});
} catch (error) {
console.error("Invalid Json format: ", error);
}
}
const { restoreDataFromPlugin } = get();
await restoreDataFromPlugin({
pluginKey: FFS_PINNED_FOLDER_PATHS_KEY,
key: "pinnedFolderPaths",
needParse: true,
});
},
updateFolderPinState: async (oldPath: string, newPath: string) => {
const {
pinnedFolderPaths,
_updatePinnedFolderPaths,
} = get();
const { pinnedFolderPaths, _updatePinnedFolderPaths } = get();
if (!pinnedFolderPaths.includes(oldPath)) return;
const updatedPaths = replaceItemInArray(
pinnedFolderPaths,

View file

@ -86,13 +86,12 @@ export const createSortFolderSlice =
});
},
restoreFolderSortRule: async () => {
const { restoreFoldersManualSortOrder, getDataFromPlugin } = get();
const lastFolderSortRule = await getDataFromPlugin<FolderSortRule>(
FFS_FOLDER_SORT_RULE_KEY
);
if (!lastFolderSortRule) return;
set({
folderSortRule: lastFolderSortRule,
const { restoreFoldersManualSortOrder, restoreDataFromPlugin } =
get();
const lastFolderSortRule = await restoreDataFromPlugin({
pluginKey: FFS_FOLDER_SORT_RULE_KEY,
key: "folderSortRule",
needParse: true,
});
if (lastFolderSortRule === FOLDER_MANUAL_SORT_RULE) {
await restoreFoldersManualSortOrder();

View file

@ -50,18 +50,11 @@ export const createPinnedTagSlice =
);
},
restorePinnedTags: async () => {
const { getDataFromPlugin: getData } = get();
const pinnedTagPaths = await getData<string>(
FFS_PINNED_TAG_PATHS_KEY
);
if (!pinnedTagPaths) return;
try {
const tagPaths: string[] = JSON.parse(pinnedTagPaths);
set({
pinnedTagPaths: tagPaths,
});
} catch (error) {
console.error("Invalid Json format: ", error);
}
const { restoreDataFromPlugin } = get();
await restoreDataFromPlugin({
pluginKey: FFS_PINNED_TAG_PATHS_KEY,
key: "pinnedTagPaths",
needParse: true,
});
},
});