Skip to content

Commit

Permalink
🐛 fix: 修复订阅下载和删除角色时的体验
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmclin2 committed Jul 14, 2024
1 parent 2259ea2 commit cf9e64e
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 145 deletions.
1 change: 0 additions & 1 deletion src/features/Actions/Agent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const ThemeButton = memo(() => {
overlayInnerStyle={{
padding: 0,
}}
trigger={['click']}
>
<ActionIcon icon={PlusCircle} title={t('agent.create')} />
</Popover>
Expand Down
30 changes: 0 additions & 30 deletions src/features/Actions/UnSubscribeButton.tsx

This file was deleted.

35 changes: 35 additions & 0 deletions src/features/MarketInfo/actions/ChatButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Button } from 'antd';
import { useRouter } from 'next/navigation';
import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { useSessionStore } from '@/store/session';
import { Agent } from '@/types/agent';

interface ChatButtonProps {
agent: Agent;
}

const ChatButton = memo<ChatButtonProps>(({ agent }) => {
const router = useRouter();
const { t } = useTranslation('chat');
const createSession = useSessionStore((s) => s.createSession);

const [closePanel] = useGlobalStore((s) => [s.closePanel]);
return (
<Button
key="chat"
onClick={() => {
createSession(agent);
router.push('/chat');
closePanel('market');
}}
type={'primary'}
>
{t('chat')}
</Button>
);
});

export default ChatButton;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Popover, Progress, Space, message } from 'antd';
import { Button, Popover, Progress, Space } from 'antd';
import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';
Expand All @@ -11,16 +11,11 @@ interface SubscribeButtonProps {
agent: Agent;
}

const SubscribeButton = memo((props: SubscribeButtonProps) => {
const [removeLocalAgent, subscribed] = useAgentStore((s) => [
s.removeLocalAgent,
agentSelectors.subscribed(s),
]);

const { fetchAgentData, percent, downloading } = useDownloadAgent();

const Subscribe = memo((props: SubscribeButtonProps) => {
const { agent } = props;
const subscribed = useAgentStore((s) => agentSelectors.subscribed(s));

const { fetchAgentData, percent, downloading } = useDownloadAgent();
const isSubscribed = subscribed(agent.agentId);

const { t } = useTranslation('common');
Expand Down Expand Up @@ -49,20 +44,14 @@ const SubscribeButton = memo((props: SubscribeButtonProps) => {
key={'subscribe'}
disabled={downloading}
onClick={async () => {
if (isSubscribed) {
removeLocalAgent(agent.agentId).then(() => {
message.success(t('actions.unsubscribeSuccess'));
});
} else {
await fetchAgentData(agent);
}
await fetchAgentData(agent);
}}
type={isSubscribed ? 'default' : 'primary'}
>
{isSubscribed ? t('actions.unsubscribe') : t('actions.downloadSubscribe')}
{t('actions.downloadSubscribe')}
</Button>
</Popover>
);
});

export default SubscribeButton;
export default Subscribe;
43 changes: 43 additions & 0 deletions src/features/MarketInfo/actions/UnSubscribe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Button, Popconfirm, message } from 'antd';
import React, { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useAgentStore } from '@/store/agent';
import { useSessionStore } from '@/store/session';
import { Agent } from '@/types/agent';

interface UnSubscribeButtonProps {
agent: Agent;
}

const UnSubscribe = memo((props: UnSubscribeButtonProps) => {
const { agent } = props;
const removeLocalAgent = useAgentStore((s) => s.removeLocalAgent);
const removeSessionByAgentId = useSessionStore((s) => s.removeSessionByAgentId);
const [loading, setLoading] = useState(false);

const { t } = useTranslation(['common', 'role']);

return (
<Popconfirm
cancelText={t('cancel')}
description={t('delRoleDesc', { name: agent?.meta.name, ns: 'role' })}
key="delete"
okText={t('confirm')}
okButtonProps={{ loading: loading }}
onConfirm={async () => {
if (!agent) return;
setLoading(true);
await removeLocalAgent(agent.agentId);
removeSessionByAgentId(agent.agentId);
setLoading(false);
message.success(t('actions.unsubscribeSuccess'));
}}
title={t('actions.unsubscribe') + '?'}
>
<Button>{t('actions.unsubscribe')}</Button>
</Popconfirm>
);
});

export default UnSubscribe;
36 changes: 10 additions & 26 deletions src/features/MarketInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { DraggablePanel } from '@lobehub/ui';
import { Button } from 'antd';
import { createStyles } from 'antd-style';
import { useRouter } from 'next/navigation';
import React, { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import Author from '@/components/Author';
import AgentCard from '@/components/agent/AgentCard';
import SystemRole from '@/components/agent/SystemRole';
import { SIDEBAR_MAX_WIDTH, SIDEBAR_WIDTH } from '@/constants/token';
import SubscribeButton from '@/features/MarketInfo/SubscribeButton';
import { agentSelectors, useAgentStore } from '@/store/agent';
import { useGlobalStore } from '@/store/global';
import { marketStoreSelectors, useMarketStore } from '@/store/market';
import { useSessionStore } from '@/store/session';

import ChatButton from './actions/ChatButton';
import Subscribe from './actions/Subscribe';
import UnSubscribe from './actions/UnSubscribe';

const useStyles = createStyles(({ css, token }) => ({
content: css`
Expand All @@ -29,8 +27,6 @@ const useStyles = createStyles(({ css, token }) => ({

const Header = () => {
const { styles } = useStyles();
const router = useRouter();
const { t } = useTranslation('chat');
const [tempId, setTempId] = useState<string>('');
const [showAgentSidebar, activateAgent, deactivateAgent, currentAgentItem] = useMarketStore(
(s) => [
Expand All @@ -40,34 +36,22 @@ const Header = () => {
marketStoreSelectors.currentAgentItem(s),
],
);
const [closePanel] = useGlobalStore((s) => [s.closePanel]);
const [subscribed] = useAgentStore((s) => [agentSelectors.subscribed(s)]);

const createSession = useSessionStore((s) => s.createSession);

const actions = [];
if (currentAgentItem) {
const isSubscribed = subscribed(currentAgentItem.agentId);

if (isSubscribed) {
actions.push(
<Button
key="chat"
onClick={() => {
createSession(currentAgentItem);
router.push('/chat');
closePanel('market');
}}
type={'primary'}
>
{t('chat')}
</Button>,
<ChatButton key="chat" agent={currentAgentItem} />,
<UnSubscribe key="unsubscribe" agent={currentAgentItem} />,
);
} else {
actions.push(
<Subscribe agent={currentAgentItem} key={`${currentAgentItem.agentId}-subscribe`} />,
);
}

actions.push(
<SubscribeButton agent={currentAgentItem} key={`${currentAgentItem.agentId}-subscribe`} />,
);
}

return (
Expand Down
62 changes: 0 additions & 62 deletions src/features/RoleInfo/index.tsx

This file was deleted.

10 changes: 7 additions & 3 deletions src/features/RoleList/List/Item/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export default (props: ActionsProps) => {
const { t } = useTranslation('common');
const [removeLocalAgent] = useAgentStore((s) => [s.removeLocalAgent]);
const currentAgent = useAgentStore((s) => s.getAgentById(id));
const createSession = useSessionStore((s) => s.createSession);
const [createSession, removeSessionByAgentId] = useSessionStore((s) => [
s.createSession,
s.removeSessionByAgentId,
]);

const items: MenuProps['items'] = [
{
Expand All @@ -43,8 +46,9 @@ export default (props: ActionsProps) => {
modal.confirm({
centered: true,
okButtonProps: { danger: true },
onOk: () => {
removeLocalAgent(id);
async onOk() {
await removeLocalAgent(id);
removeSessionByAgentId(id);
},
okText: t('actions.del'),
cancelText: t('cancel'),
Expand Down
6 changes: 3 additions & 3 deletions src/features/SessionList/List/Item/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (props: ActionsProps) => {
const { id, setOpen } = props;
const { modal } = App.useApp();
const { t } = useTranslation('common');
const [removeSession] = useSessionStore((s) => [s.removeSession]);
const [removeSessionByAgentId] = useSessionStore((s) => [s.removeSessionByAgentId]);

const items: MenuProps['items'] = [
{
Expand All @@ -27,8 +27,8 @@ export default (props: ActionsProps) => {
modal.confirm({
centered: true,
okButtonProps: { danger: true },
onOk: () => {
removeSession(id);
async onOk() {
removeSessionByAgentId(id);
},
okText: t('actions.del'),
cancelText: t('cancel'),
Expand Down
4 changes: 2 additions & 2 deletions src/store/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface SessionStore {
/**
* 移除会话
*/
removeSession: (id: string) => void;
removeSessionByAgentId: (id: string) => void;
/**
* 发送消息
* @param message 消息内容
Expand Down Expand Up @@ -392,7 +392,7 @@ export const createSessionStore: StateCreator<SessionStore, [['zustand/devtools'

fetchAIResponse(contextMessages, assistantId);
},
removeSession: (id) => {
removeSessionByAgentId: (id) => {
const { sessionList, activeId } = get();

const sessions = produce(sessionList, (draft) => {
Expand Down

0 comments on commit cf9e64e

Please sign in to comment.