From 71d07359178be7c290e42eba06e4297af98e3b3b Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Sat, 4 Jan 2025 14:18:57 +0100 Subject: [PATCH] Various fixes (#99) * Add shortcut to page turning with the space key on keyboard * Properly handle auto refresh token with supabase * Fix settings not stored in direct quit of app, closes #94 --- .../app/reader/components/ReaderContent.tsx | 2 ++ apps/readest-app/src/context/AuthContext.tsx | 20 +++++++++++++------ apps/readest-app/src/helpers/shortcuts.ts | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/readest-app/src/app/reader/components/ReaderContent.tsx b/apps/readest-app/src/app/reader/components/ReaderContent.tsx index 84c6c19e..3e2823fe 100644 --- a/apps/readest-app/src/app/reader/components/ReaderContent.tsx +++ b/apps/readest-app/src/app/reader/components/ReaderContent.tsx @@ -88,6 +88,7 @@ const ReaderContent: React.FC<{ ids?: string; settings: SystemSettings }> = ({ i const { book } = getBookData(bookKey) || {}; const { isPrimary } = getViewState(bookKey) || {}; if (isPrimary && book && config) { + const settings = useSettingsStore.getState().settings; await saveConfig(envConfig, bookKey, config, settings); } }; @@ -110,6 +111,7 @@ const ReaderContent: React.FC<{ ids?: string; settings: SystemSettings }> = ({ i }; const handleCloseBooks = async () => { + const settings = useSettingsStore.getState().settings; await Promise.all(bookKeys.map((key) => saveConfigAndCloseBook(key))); await saveSettings(envConfig, settings); }; diff --git a/apps/readest-app/src/context/AuthContext.tsx b/apps/readest-app/src/context/AuthContext.tsx index a417da67..3feb9e38 100644 --- a/apps/readest-app/src/context/AuthContext.tsx +++ b/apps/readest-app/src/context/AuthContext.tsx @@ -29,10 +29,8 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { }); useEffect(() => { - const fetchSession = async () => { - const { - data: { session }, - } = await supabase.auth.getSession(); + const syncSession = (session: { access_token: string; user: User } | null) => { + console.log('Syncing session'); if (session) { const { access_token, user } = session; setToken(access_token); @@ -46,10 +44,20 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { setUser(null); } }; + const fetchSession = async () => { + const { data } = await supabase.auth.getSession(); + syncSession(data.session); + }; - console.log('Fetching session'); fetchSession(); - }, [token]); + const { data: subscription } = supabase.auth.onAuthStateChange((_, session) => { + syncSession(session); + }); + + return () => { + subscription?.subscription.unsubscribe(); + }; + }, []); const login = (newToken: string, newUser: User) => { console.log('Logging in'); diff --git a/apps/readest-app/src/helpers/shortcuts.ts b/apps/readest-app/src/helpers/shortcuts.ts index fcab5dc1..fea9f863 100644 --- a/apps/readest-app/src/helpers/shortcuts.ts +++ b/apps/readest-app/src/helpers/shortcuts.ts @@ -28,7 +28,7 @@ const DEFAULT_SHORTCUTS: ShortcutConfig = { onReloadPage: ['shift+r'], onQuitApp: ['ctrl+q', 'cmd+q'], onGoLeft: ['ArrowLeft', 'PageUp', 'h'], - onGoRight: ['ArrowRight', 'PageDown', 'l'], + onGoRight: ['ArrowRight', 'PageDown', 'l', ' '], onGoNext: ['ArrowDown', 'j'], onGoPrev: ['ArrowUp', 'k'], onGoBack: ['shift+ArrowLeft', 'shift+h'],