2024-06-04 10:57:52 +02:00
|
|
|
import { PluginItem } from '@joplin/lib/components/shared/config/plugins/types';
|
|
|
|
import { PluginSettings } from '@joplin/lib/services/plugins/PluginService';
|
|
|
|
import { PluginManifest } from '@joplin/lib/services/plugins/utils/types';
|
|
|
|
import { useMemo, useRef } from 'react';
|
2024-06-25 14:59:59 +02:00
|
|
|
import usePlugin from '@joplin/lib/hooks/usePlugin';
|
2024-06-04 10:57:52 +02:00
|
|
|
|
|
|
|
// initialItem is used when the plugin is not installed. For example, if the plugin item is being
|
|
|
|
// created from search results.
|
|
|
|
const usePluginItem = (id: string, pluginSettings: PluginSettings, initialItem: PluginItem|null): PluginItem => {
|
|
|
|
const plugin = usePlugin(id);
|
|
|
|
|
|
|
|
const lastManifest = useRef<PluginManifest>();
|
|
|
|
if (plugin) {
|
|
|
|
lastManifest.current = plugin.manifest;
|
|
|
|
} else if (!lastManifest.current) {
|
|
|
|
lastManifest.current = initialItem?.manifest;
|
|
|
|
}
|
|
|
|
const manifest = lastManifest.current;
|
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
if (!manifest) return null;
|
|
|
|
const settings = pluginSettings[id];
|
|
|
|
|
2024-06-14 20:36:26 +02:00
|
|
|
const installed = !!settings || !!plugin;
|
|
|
|
|
2024-06-04 10:57:52 +02:00
|
|
|
return {
|
|
|
|
id,
|
|
|
|
manifest,
|
|
|
|
|
2024-06-14 20:36:26 +02:00
|
|
|
installed,
|
|
|
|
enabled: settings?.enabled ?? installed,
|
2024-06-04 10:57:52 +02:00
|
|
|
deleted: settings?.deleted ?? false,
|
|
|
|
hasBeenUpdated: settings?.hasBeenUpdated ?? false,
|
|
|
|
devMode: plugin?.devMode ?? false,
|
|
|
|
builtIn: plugin?.builtIn ?? false,
|
|
|
|
};
|
|
|
|
}, [plugin, id, pluginSettings, manifest]);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default usePluginItem;
|