Files
readest/apps/readest-app/src/app/reader/hooks/useProgressAutoSave.ts
T
Huang Xin 1936136596 fix: resolve various tracked exceptions in ph (#3584)
* fix: handle synced bookmarks without cfi

* chore: clean up some exceptions in ph
2026-03-22 08:16:38 +01:00

32 lines
1.0 KiB
TypeScript

import { useCallback, useEffect } from 'react';
import { useEnv } from '@/context/EnvContext';
import { useBookDataStore } from '@/store/bookDataStore';
import { useReaderStore } from '@/store/readerStore';
import { useSettingsStore } from '@/store/settingsStore';
import { debounce } from '@/utils/debounce';
export const useProgressAutoSave = (bookKey: string) => {
const { envConfig } = useEnv();
const { getConfig, saveConfig } = useBookDataStore();
const { getProgress } = useReaderStore();
const progress = getProgress(bookKey);
// eslint-disable-next-line react-hooks/exhaustive-deps
const saveBookConfig = useCallback(
debounce(() => {
setTimeout(async () => {
const config = getConfig(bookKey);
if (!config) return;
const settings = useSettingsStore.getState().settings;
await saveConfig(envConfig, bookKey, config, settings);
}, 500);
}, 1000),
[],
);
useEffect(() => {
saveBookConfig();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [progress, bookKey]);
};