1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-16 00:14:34 +02:00

Mobile: Plugin settings screen: Fix plugin states not set correctly when installing multiple plugins at once (#10580)

Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
This commit is contained in:
Henry Heino
2024-06-14 11:36:44 -07:00
committed by GitHub
parent ce22d8238c
commit 4751b4dd74
6 changed files with 44 additions and 22 deletions

View File

@ -1,11 +1,12 @@
import { _ } from '../../../../locale';
import PluginService, { PluginSettings, defaultPluginSetting } from '../../../../services/plugins/PluginService';
import type * as React from 'react';
import shim from '../../../../shim';
import produce from 'immer';
import { ItemEvent, OnPluginSettingChangeHandler } from './types';
const useOnDeleteHandler = (
pluginSettings: PluginSettings,
pluginSettingsRef: React.RefObject<PluginSettings>,
onSettingsChange: OnPluginSettingChangeHandler,
deleteNow: boolean,
) => {
@ -15,11 +16,6 @@ const useOnDeleteHandler = (
const confirmed = await shim.showConfirmationDialog(_('Delete plugin "%s"?', item.manifest.name));
if (!confirmed) return;
let newSettings = produce(pluginSettings, (draft: PluginSettings) => {
if (!draft[item.manifest.id]) draft[item.manifest.id] = defaultPluginSetting();
draft[item.manifest.id].deleted = true;
});
if (deleteNow) {
const pluginService = PluginService.instance();
@ -29,12 +25,28 @@ const useOnDeleteHandler = (
if (plugin) {
await pluginService.unloadPlugin(item.manifest.id);
}
newSettings = await pluginService.uninstallPlugins(newSettings);
}
onSettingsChange({ value: newSettings });
}, [pluginSettings, onSettingsChange, deleteNow]);
// Important: To avoid race conditions, don't run any async code between fetching plugin
// settings from the ref and calling onSettingsChange.
let newSettings = pluginSettingsRef.current;
if (deleteNow) {
newSettings = produce(newSettings, (draft: PluginSettings) => {
delete draft[item.manifest.id];
});
onSettingsChange({ value: newSettings });
await PluginService.instance().uninstallPlugin(item.manifest.id);
} else {
// Setting .deleted causes the app to delete this plugin on startup.
newSettings = produce(newSettings, (draft: PluginSettings) => {
if (!draft[item.manifest.id]) draft[item.manifest.id] = defaultPluginSetting();
draft[item.manifest.id].deleted = true;
});
onSettingsChange({ value: newSettings });
}
}, [pluginSettingsRef, onSettingsChange, deleteNow]);
};
export default useOnDeleteHandler;

View File

@ -20,13 +20,13 @@ const itemEvent = ({
} as ItemEvent);
const callHook = (isUpdate: boolean, pluginEnabled = true, pluginInstalledViaGUI = true) => () => useOnInstallHandler(
setInstallingPluginIds,
{
{ current: {
[pluginId]: pluginInstalledViaGUI ? {
enabled: pluginEnabled,
deleted: false,
hasBeenUpdated: false,
} : undefined,
},
} },
repoApi,
onPluginSettingsChange,
isUpdate,

View File

@ -13,7 +13,7 @@ type GetRepoApiCallback = ()=> RepositoryApi;
const useOnInstallHandler = (
setInstallingPluginIds: React.Dispatch<React.SetStateAction<Record<string, boolean>>>,
pluginSettings: PluginSettings,
pluginSettingsRef: React.RefObject<PluginSettings>,
getRepoApi: GetRepoApiCallback|RepositoryApi,
onPluginSettingsChange: OnPluginSettingChangeHandler,
isUpdate: boolean,
@ -45,6 +45,7 @@ const useOnInstallHandler = (
}
if (!installError) {
const pluginSettings = pluginSettingsRef.current;
const newSettings = produce(pluginSettings, (draft: PluginSettings) => {
draft[pluginId] = defaultPluginSetting();
if (isUpdate) {
@ -71,7 +72,7 @@ const useOnInstallHandler = (
{ buttons: [_('OK')] },
);
}
}, [getRepoApi, isUpdate, pluginSettings, onPluginSettingsChange, setInstallingPluginIds]);
}, [getRepoApi, isUpdate, pluginSettingsRef, onPluginSettingsChange, setInstallingPluginIds]);
};
export default useOnInstallHandler;