Use only one way to get appservice

This commit is contained in:
chrox
2024-11-12 13:39:21 +01:00
parent e83f8c569f
commit 26ca5849aa
2 changed files with 6 additions and 13 deletions
+2 -2
View File
@@ -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);
+4 -11
View File
@@ -8,7 +8,6 @@ import { AppService } from '@/types/system';
interface EnvContextType {
envConfig: EnvConfigType;
appService: AppService | null;
getAppService: (envConfig: EnvConfigType) => Promise<AppService>;
}
const EnvContext = createContext<EnvContextType | undefined>(undefined);
@@ -17,17 +16,11 @@ export const EnvProvider = ({ children }: { children: ReactNode }) => {
const [envConfig] = useState<EnvConfigType>(env);
const [appService, setAppService] = useState<AppService | null>(null);
const getAppService = async (envConfig: EnvConfigType): Promise<AppService> => {
const service = await envConfig.getAppService();
setAppService(service);
return service;
};
React.useEffect(() => {
envConfig.getAppService().then((service) => setAppService(service));
}, [envConfig]);
return (
<EnvContext.Provider value={{ envConfig, appService, getAppService }}>
{children}
</EnvContext.Provider>
);
return <EnvContext.Provider value={{ envConfig, appService }}>{children}</EnvContext.Provider>;
};
export const useEnv = (): EnvContextType => {