Files
readest/apps/readest-app/src/hooks/useCustomFonts.ts
T
Huang Xin 2d30868d23 fix(fonts): hydrate custom fonts on library page, closes #4178 (#4191)
Custom fonts vanished from the Font panel after an app restart unless a
book was opened first. The custom-font store is hydrated only by the
reader's FoliateViewer (on book open) or by useReplicaPull (gated on a
signed-in user), so opening Settings straight from the library left the
store empty.

Add a useCustomFonts hook that loads persisted custom fonts on mount,
unconditional of auth or book state, and mount it on the library page.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 20:19:45 +02:00

29 lines
1.2 KiB
TypeScript

import { useEffect } from 'react';
import { useEnv } from '@/context/EnvContext';
import { useSettingsStore } from '@/store/settingsStore';
import { useCustomFontStore } from '@/store/customFontStore';
/**
* Hydrate the custom-font store from persisted `settings.customFonts`.
*
* The reader hydrates the store inside FoliateViewer when a book opens,
* and `useReplicaPull` hydrates it during a sync — but that pull is
* gated on a signed-in user. Without this hook, opening Settings
* straight from the library (no book opened, no account) leaves the
* store empty, so imported custom fonts vanish from the Font panel
* after an app restart until a book is opened. Mount this on the
* library page so the panel always sees the persisted fonts.
*/
export const useCustomFonts = () => {
const { envConfig, appService } = useEnv();
const { settings } = useSettingsStore();
const { loadCustomFonts } = useCustomFontStore();
useEffect(() => {
if (!appService) return;
if (!settings?.customFonts) return;
void loadCustomFonts(envConfig);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [appService, settings?.customFonts, envConfig]);
};