2021-01-07 18:30:53 +02:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
import PluginService, { defaultPluginSetting, PluginSettings } from '@joplin/lib/services/plugins/PluginService';
|
|
|
|
import produce from 'immer';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import Logger from '@joplin/lib/Logger';
|
2021-01-27 01:56:35 +02:00
|
|
|
import { ItemEvent } from './PluginBox';
|
2021-01-07 18:30:53 +02:00
|
|
|
|
|
|
|
const logger = Logger.create('useOnInstallHandler');
|
|
|
|
|
2021-01-20 00:58:09 +02:00
|
|
|
export interface OnPluginSettingChangeEvent {
|
|
|
|
value: PluginSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
type OnPluginSettingChangeHandler = (event: OnPluginSettingChangeEvent)=> void;
|
|
|
|
|
|
|
|
export default function(setInstallingPluginIds: Function, pluginSettings: PluginSettings, repoApi: Function, onPluginSettingsChange: OnPluginSettingChangeHandler, isUpdate: boolean) {
|
2021-01-27 01:56:35 +02:00
|
|
|
return useCallback(async (event: ItemEvent) => {
|
|
|
|
const pluginId = event.item.manifest.id;
|
2021-01-07 18:30:53 +02:00
|
|
|
|
|
|
|
setInstallingPluginIds((prev: any) => {
|
|
|
|
return {
|
|
|
|
...prev, [pluginId]: true,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
let installError = null;
|
|
|
|
|
|
|
|
try {
|
2021-01-20 00:58:09 +02:00
|
|
|
if (isUpdate) {
|
|
|
|
await PluginService.instance().updatePluginFromRepo(repoApi(), pluginId);
|
|
|
|
} else {
|
|
|
|
await PluginService.instance().installPluginFromRepo(repoApi(), pluginId);
|
|
|
|
}
|
2021-01-07 18:30:53 +02:00
|
|
|
} catch (error) {
|
|
|
|
installError = error;
|
|
|
|
logger.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!installError) {
|
|
|
|
const newSettings = produce(pluginSettings, (draft: PluginSettings) => {
|
|
|
|
draft[pluginId] = defaultPluginSetting();
|
2021-03-23 11:01:34 +02:00
|
|
|
if (isUpdate) {
|
2021-03-24 16:15:49 +02:00
|
|
|
if (pluginSettings[pluginId]) {
|
|
|
|
draft[pluginId].enabled = pluginSettings[pluginId].enabled;
|
|
|
|
}
|
2021-03-23 11:01:34 +02:00
|
|
|
draft[pluginId].hasBeenUpdated = true;
|
|
|
|
}
|
2021-01-07 18:30:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
onPluginSettingsChange({ value: newSettings });
|
|
|
|
}
|
|
|
|
|
|
|
|
setInstallingPluginIds((prev: any) => {
|
|
|
|
return {
|
|
|
|
...prev, [pluginId]: false,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (installError) alert(_('Could not install plugin: %s', installError.message));
|
|
|
|
}, [pluginSettings, onPluginSettingsChange]);
|
|
|
|
}
|