Skip to content

Commit

Permalink
🐛 fix: 修正被移除插件无法看到的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Sep 3, 2023
1 parent e36e621 commit c17eb56
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/features/AgentSetting/AgentPlugin/LocalPluginItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const MarketList = memo<{ id: string }>(({ id }) => {

const [showModal, setModal] = useState(false);

const updateConfig = useStore((s) => s.toggleAgentPlugin);
const [plugins, hasPlugin] = useStore((s) => [s.config.plugins || [], !!s.config.plugins]);
const [toggleAgentPlugin, hasPlugin] = useStore((s) => [s.toggleAgentPlugin, !!s.config.plugins]);
const plugins = useStore((s) => s.config.plugins || []);

const [useFetchPluginList, fetchPluginManifest, deleteCustomPlugin, updateCustomPlugin] =
usePluginStore((s) => [
Expand All @@ -36,6 +36,7 @@ const MarketList = memo<{ id: string }>(({ id }) => {
mode={'edit'}
onDelete={() => {
deleteCustomPlugin(id);
toggleAgentPlugin(id);
}}
onOpenChange={setModal}
onSave={(value) => {
Expand All @@ -53,7 +54,7 @@ const MarketList = memo<{ id: string }>(({ id }) => {
}
loading={pluginManifestLoading[id]}
onChange={(checked) => {
updateConfig(id);
toggleAgentPlugin(id);
if (checked) {
fetchPluginManifest(id);
}
Expand Down
89 changes: 67 additions & 22 deletions src/features/AgentSetting/AgentPlugin/MarketList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Avatar, Form, Icon, Tooltip } from '@lobehub/ui';
import { Button, Skeleton, Switch, Tag } from 'antd';
import { createStyles } from 'antd-style';
import isEqual from 'fast-deep-equal';
import { LucideBlocks, LucideStore } from 'lucide-react';
import { LucideBlocks, LucideStore, LucideTrash2 } from 'lucide-react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';
import DevModal from 'src/features/PluginDevModal';

import { FORM_STYLE } from '@/const/layoutTokens';
import { pluginHelpers, usePluginStore } from '@/store/plugin';
import { pluginHelpers, pluginSelectors, usePluginStore } from '@/store/plugin';

import { useStore } from '../store';
import LocalPluginItem from './LocalPluginItem';
Expand Down Expand Up @@ -37,11 +37,7 @@ const MarketList = memo(() => {

const [showModal, setModal] = useState(false);

// useEffect(() => {
// setModal(true);
// }, []);

const [plugins, hasPlugin, toggleAgentPlugin] = useStore((s) => [
const [userEnabledPlugins, hasPlugin, toggleAgentPlugin] = useStore((s) => [
s.config.plugins || [],
!!s.config.plugins,
s.toggleAgentPlugin,
Expand All @@ -57,6 +53,7 @@ const MarketList = memo(() => {
const pluginManifestLoading = usePluginStore((s) => s.pluginManifestLoading, isEqual);
const pluginList = usePluginStore((s) => s.pluginList, isEqual);
const customPluginList = usePluginStore((s) => s.customPluginList, isEqual);
const totalList = usePluginStore(pluginSelectors.pluginList, isEqual);

useFetchPluginList();

Expand Down Expand Up @@ -100,7 +97,9 @@ const MarketList = memo(() => {
<Switch
checked={
// 如果在加载中,说明激活了
pluginManifestLoading[identifier] || !hasPlugin ? false : plugins.includes(identifier)
pluginManifestLoading[identifier] || !hasPlugin
? false
: userEnabledPlugins.includes(identifier)
}
loading={pluginManifestLoading[identifier]}
onChange={(checked) => {
Expand Down Expand Up @@ -132,6 +131,35 @@ const MarketList = memo(() => {
tag: identifier,
}));

// =========== Deprecated Plugin List =========== //

// 基于 userEnabledPlugins 和完整的 totalList,检查出不在 totalList 中的插件
const deprecatedList = userEnabledPlugins
.filter((pluginId) => totalList.findIndex((p) => p.identifier === pluginId) < 0)
.map((id) => ({
avatar: <Avatar avatar={'♻️'} />,
children: (
<Switch
checked={true}
onChange={(checked) => {
togglePlugin(id, checked);
}}
/>
),
label: (
<Flexbox align={'center'} gap={8} horizontal>
{id}
<Tag bordered={false} color={'red'}>
{t('list.item.deprecated.title', { ns: 'plugin' })}
</Tag>
</Flexbox>
),
minWidth: undefined,
tag: id,
}));

const hasDeprecated = deprecatedList.length > 0;

return (
<>
<DevModal
Expand All @@ -148,27 +176,44 @@ const MarketList = memo(() => {
<Form
items={[
{
children: isEmpty ? loadingList : [...customList, ...list],
children: isEmpty ? loadingList : [...deprecatedList, ...customList, ...list],
extra: (
<Tooltip title={t('settingPlugin.addTooltip')}>
<Button
icon={<Icon icon={LucideBlocks} />}
onClick={(e) => {
e.stopPropagation();
setModal(true);
}}
size={'small'}
>
{t('settingPlugin.add')}
</Button>
</Tooltip>
<Flexbox gap={8} horizontal>
{hasDeprecated ? (
<Tooltip title={t('settingPlugin.clearDeprecated')}>
<Button
icon={<Icon icon={LucideTrash2} />}
onClick={(e) => {
e.stopPropagation();
for (const i of deprecatedList) {
togglePlugin(i.tag as string, false);
}
}}
size={'small'}
type={'text'}
></Button>
</Tooltip>
) : null}
<Tooltip title={t('settingPlugin.addTooltip')}>
<Button
icon={<Icon icon={LucideBlocks} />}
onClick={(e) => {
e.stopPropagation();
setModal(true);
}}
size={'small'}
>
{t('settingPlugin.add')}
</Button>
</Tooltip>
</Flexbox>
),
icon: LucideStore,
title: t('settingPlugin.title'),
},
]}
{...FORM_STYLE}
/>{' '}
/>
</>
);
});
Expand Down
1 change: 1 addition & 0 deletions src/locales/default/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default {
},
list: {
item: {
'deprecated.title': '已删除',
'local.config': '配置',
'local.title': '自定义',
},
Expand Down
2 changes: 1 addition & 1 deletion src/locales/default/setting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export default {
danger: {
clear: {
Expand Down Expand Up @@ -131,6 +130,7 @@ export default {
settingPlugin: {
add: '添加',
addTooltip: '添加自定义插件',
clearDeprecated: '移除无效插件',
config: '{{id}} 插件配置',
title: '插件列表',
},
Expand Down

0 comments on commit c17eb56

Please sign in to comment.