From 26ca5849aa40eb9abc51bfa37c2e01837594708e Mon Sep 17 00:00:00 2001 From: chrox Date: Tue, 12 Nov 2024 13:39:21 +0100 Subject: [PATCH] Use only one way to get appservice --- apps/readest-app/src/app/library/page.tsx | 4 ++-- apps/readest-app/src/context/EnvContext.tsx | 15 ++++----------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/apps/readest-app/src/app/library/page.tsx b/apps/readest-app/src/app/library/page.tsx index 3d74521a..22fb58d8 100644 --- a/apps/readest-app/src/app/library/page.tsx +++ b/apps/readest-app/src/app/library/page.tsx @@ -11,7 +11,7 @@ import LibraryHeader from '@/app/library/components/LibraryHeader'; import Bookshelf from '@/app/library/components/Bookshelf'; const LibraryPage = () => { - const { envConfig, appService, getAppService } = useEnv(); + const { envConfig, appService } = useEnv(); const { library: libraryBooks, setLibrary } = useReaderStore(); const [loading, setLoading] = useState(true); const isInitiating = useRef(false); @@ -21,7 +21,7 @@ const LibraryPage = () => { if (isInitiating.current) return; isInitiating.current = true; setLoading(true); - getAppService(envConfig).then(async (appService) => { + envConfig.getAppService().then(async (appService) => { console.log('Loading library books...'); setLibrary(await appService.loadLibraryBooks()); setLoading(false); diff --git a/apps/readest-app/src/context/EnvContext.tsx b/apps/readest-app/src/context/EnvContext.tsx index 174aaf82..16663775 100644 --- a/apps/readest-app/src/context/EnvContext.tsx +++ b/apps/readest-app/src/context/EnvContext.tsx @@ -8,7 +8,6 @@ import { AppService } from '@/types/system'; interface EnvContextType { envConfig: EnvConfigType; appService: AppService | null; - getAppService: (envConfig: EnvConfigType) => Promise; } const EnvContext = createContext(undefined); @@ -17,17 +16,11 @@ export const EnvProvider = ({ children }: { children: ReactNode }) => { const [envConfig] = useState(env); const [appService, setAppService] = useState(null); - const getAppService = async (envConfig: EnvConfigType): Promise => { - const service = await envConfig.getAppService(); - setAppService(service); - return service; - }; + React.useEffect(() => { + envConfig.getAppService().then((service) => setAppService(service)); + }, [envConfig]); - return ( - - {children} - - ); + return {children}; }; export const useEnv = (): EnvContextType => {