Skip to content

Commit

Permalink
🐛 fix: plugins with multiple settings cannot be correctly configured (#…
Browse files Browse the repository at this point in the history
…1991)

* 🐛 fix: plugins with multiple settings cannot be correctly configured

* ♻️ refactor: merge plugin setting in the action layer instead of UI

* ✅ test: add a test to ensure plugin settings are correctly merged
  • Loading branch information
YangHanlin committed Apr 30, 2024
1 parent b0ccfdb commit 0c041aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/store/tool/slices/plugin/action.test.ts
Expand Up @@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';

import { pluginService } from '@/services/plugin';
import { LobeTool } from '@/types/tool';
import { merge } from '@/utils/merge';

import { useToolStore } from '../../store';

Expand Down Expand Up @@ -91,6 +92,24 @@ describe('useToolStore:plugin', () => {

expect(pluginService.updatePluginSettings).toBeCalledWith(pluginId, newSettings);
});

it('should merge settings for a plugin with existing settings', async () => {
const pluginId = 'test-plugin';
const existingSettings = { setting1: 'old-value', setting2: 'old-value' };
const newSettings = { setting1: 'new-value' };
const mergedSettings = merge(existingSettings, newSettings);
useToolStore.setState({
installedPlugins: [{ identifier: pluginId, settings: existingSettings }] as LobeTool[],
});

const { result } = renderHook(() => useToolStore());

await act(async () => {
await result.current.updatePluginSettings(pluginId, newSettings);
});

expect(pluginService.updatePluginSettings).toBeCalledWith(pluginId, mergedSettings);
});
});

describe('removeAllPlugins', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/store/tool/slices/plugin/action.ts
Expand Up @@ -3,6 +3,7 @@ import useSWR, { SWRResponse } from 'swr';
import { StateCreator } from 'zustand/vanilla';

import { pluginService } from '@/services/plugin';
import { merge } from '@/utils/merge';

import { ToolStore } from '../../store';
import { pluginStoreSelectors } from '../store/selectors';
Expand Down Expand Up @@ -44,7 +45,11 @@ export const createPluginSlice: StateCreator<
await get().refreshPlugins();
},
updatePluginSettings: async (id, settings) => {
await pluginService.updatePluginSettings(id, settings);
const previousSettings = pluginSelectors.getPluginSettingsById(id)(get());

const nextSettings = merge(previousSettings, settings);
await pluginService.updatePluginSettings(id, nextSettings);

await get().refreshPlugins();
},
useCheckPluginsIsInstalled: (plugins) => useSWR(plugins, get().checkPluginsIsInstalled),
Expand Down

0 comments on commit 0c041aa

Please sign in to comment.