Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ› fix: plugins with multiple settings cannot be correctly configured #1991

Merged
merged 3 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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