Use only one way to get appservice
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user