2024-03-11 17:02:15 +02:00
|
|
|
import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect';
|
|
|
|
import Logger from '@joplin/utils/Logger';
|
2024-03-29 14:40:54 +02:00
|
|
|
import RepositoryApi, { InstallMode } from '@joplin/lib/services/plugins/RepositoryApi';
|
2024-04-03 19:51:09 +02:00
|
|
|
import Setting, { AppType } from '@joplin/lib/models/Setting';
|
2024-03-11 17:02:15 +02:00
|
|
|
import { useMemo } from 'react';
|
2024-03-29 14:40:54 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
2024-04-03 19:51:09 +02:00
|
|
|
import PluginService from '@joplin/lib/services/plugins/PluginService';
|
2024-03-11 17:02:15 +02:00
|
|
|
|
|
|
|
const logger = Logger.create('useRepoApi');
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
reloadRepoCounter: number;
|
|
|
|
setRepoApiError: (error: string|null)=> void;
|
|
|
|
onRepoApiLoaded: (repoApi: RepositoryApi)=> void;
|
|
|
|
}
|
|
|
|
|
|
|
|
let repoApi_: RepositoryApi|null = null;
|
|
|
|
|
2024-03-29 14:40:54 +02:00
|
|
|
export const resetRepoApi = () => {
|
|
|
|
repoApi_ = null;
|
|
|
|
};
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
const useRepoApi = ({ reloadRepoCounter, setRepoApiError, onRepoApiLoaded }: Props) => {
|
|
|
|
const repoApi = useMemo(() => {
|
2024-03-29 14:40:54 +02:00
|
|
|
const installMode = shim.mobilePlatform() === 'ios' ? InstallMode.Restricted : InstallMode.Default;
|
2024-04-03 19:51:09 +02:00
|
|
|
const appInfo = { type: AppType.Mobile, version: PluginService.instance().appVersion };
|
|
|
|
repoApi_ ??= RepositoryApi.ofDefaultJoplinRepo(Setting.value('tempDir'), appInfo, installMode);
|
2024-03-11 17:02:15 +02:00
|
|
|
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;
|