1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-mobile/components/screens/ConfigScreen/plugins/utils/useRepoApi.ts
Henry Heino 55cafb8891
Android: Add support for Markdown editor plugins (#10086)
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
2024-03-11 15:02:15 +00:00

44 lines
1.1 KiB
TypeScript

import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect';
import Logger from '@joplin/utils/Logger';
import RepositoryApi from '@joplin/lib/services/plugins/RepositoryApi';
import Setting from '@joplin/lib/models/Setting';
import { useMemo } from 'react';
const logger = Logger.create('useRepoApi');
interface Props {
reloadRepoCounter: number;
setRepoApiError: (error: string|null)=> void;
onRepoApiLoaded: (repoApi: RepositoryApi)=> void;
}
let repoApi_: RepositoryApi|null = null;
const useRepoApi = ({ reloadRepoCounter, setRepoApiError, onRepoApiLoaded }: Props) => {
const repoApi = useMemo(() => {
repoApi_ ??= RepositoryApi.ofDefaultJoplinRepo(Setting.value('tempDir'));
return repoApi_;
}, []);
useAsyncEffect(async event => {
if (reloadRepoCounter > 0) {
logger.info(`Reloading the plugin repository -- try #${reloadRepoCounter + 1}`);
}
setRepoApiError(null);
try {
await repoApi.initialize();
} catch (error) {
logger.error(error);
setRepoApiError(error);
}
if (!event.cancelled) {
onRepoApiLoaded(repoApi);
}
}, [setRepoApiError, reloadRepoCounter, repoApi]);
return repoApi;
};
export default useRepoApi;