Skip to content

Commit 90e742d

Browse files
authored
🐛 fix: fix agent config on page init (#2506)
1 parent d61f69a commit 90e742d

File tree

5 files changed

+19
-37
lines changed

5 files changed

+19
-37
lines changed
Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
import { useEffect } from 'react';
2-
31
import { useAgentStore } from '@/store/agent';
4-
import { useChatStore } from '@/store/chat';
52
import { useSessionStore } from '@/store/session';
63

74
export const useInitAgentConfig = () => {
85
const [useFetchAgentConfig] = useAgentStore((s) => [s.useFetchAgentConfig]);
96

10-
const [switchTopic] = useChatStore((s) => [s.switchTopic]);
117
const [sessionId] = useSessionStore((s) => [s.activeId]);
128

13-
useEffect(() => {
14-
// // when activeId changed, switch topic to undefined
15-
const unsubscribe = useSessionStore.subscribe(
16-
(s) => s.activeId,
17-
(activeId) => {
18-
switchTopic();
19-
20-
useAgentStore.setState({ activeId }, false, 'updateActiveId');
21-
},
22-
);
23-
24-
return () => {
25-
unsubscribe();
26-
};
27-
}, []);
28-
299
return useFetchAgentConfig(sessionId);
3010
};

src/app/(main)/chat/@session/features/SessionHydration.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
'use client';
22

3-
import { useResponsive } from 'antd-style';
43
import { useQueryState } from 'nuqs';
54
import { parseAsString } from 'nuqs/server';
65
import { memo, useEffect } from 'react';
76
import { createStoreUpdater } from 'zustand-utils';
87

8+
import { useAgentStore } from '@/store/agent';
9+
import { useChatStore } from '@/store/chat';
910
import { useSessionStore } from '@/store/session';
1011

1112
// sync outside state to useSessionStore
1213
const SessionHydration = memo(() => {
1314
const useStoreUpdater = createStoreUpdater(useSessionStore);
14-
15-
// TODO: 后面考虑可以删除了,用 useServerConfigStore 就不需要再这里注入了
16-
const { mobile } = useResponsive();
17-
useStoreUpdater('isMobile', mobile);
15+
const useAgentStoreUpdater = createStoreUpdater(useAgentStore);
16+
const [switchTopic] = useChatStore((s) => [s.switchTopic]);
1817

1918
// two-way bindings the url and session store
2019
const [session, setSession] = useQueryState(
2120
'session',
22-
parseAsString.withDefault('inbox').withOptions({ history: 'replace', throttleMs: 500 }),
21+
parseAsString.withDefault('inbox').withOptions({ history: 'replace', throttleMs: 50 }),
2322
);
2423
useStoreUpdater('activeId', session);
24+
useAgentStoreUpdater('activeId', session);
2525

2626
useEffect(() => {
2727
const unsubscribe = useSessionStore.subscribe(
2828
(s) => s.activeId,
29-
(state) => setSession(state),
29+
(state) => {
30+
switchTopic();
31+
setSession(state);
32+
},
3033
);
3134

3235
return () => {

src/app/(main)/market/@detail/features/Header.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { App, Button, Typography } from 'antd';
33
import isEqual from 'fast-deep-equal';
44
import { startCase } from 'lodash-es';
55
import { useRouter } from 'next/navigation';
6-
import { memo } from 'react';
6+
import { memo, useState } from 'react';
77
import { useTranslation } from 'react-i18next';
88
import { Center, Flexbox } from 'react-layout-kit';
99

@@ -23,7 +23,7 @@ const Header = memo(() => {
2323
const { styles } = useStyles();
2424
const createSession = useSessionStore((s) => s.createSession);
2525
const agentItem = useMarketStore(agentMarketSelectors.currentAgentItem, isEqual);
26-
26+
const [isLoading, setIsLoading] = useState(false);
2727
const { message } = App.useApp();
2828

2929
const { meta, createAt, author, homepage, config } = agentItem;
@@ -34,15 +34,19 @@ const Header = memo(() => {
3434
const handleAddAgentAndConverse = async () => {
3535
if (!agentItem) return;
3636

37+
setIsLoading(true);
3738
const session = await createSession({ config, meta });
39+
setIsLoading(false);
3840
message.success(t('addAgentSuccess'));
3941
router.push(SESSION_CHAT_URL(session, isMobile));
4042
};
4143

42-
const handleAddAgent = () => {
44+
const handleAddAgent = async () => {
4345
if (!agentItem) return;
46+
setIsLoading(true);
4447
createSession({ config, meta }, false);
4548
message.success(t('addAgentSuccess'));
49+
setIsLoading(false);
4650
};
4751

4852
return (
@@ -56,10 +60,10 @@ const Header = memo(() => {
5660
))}
5761
</Center>
5862
<div className={styles.desc}>{description}</div>
59-
<Button block onClick={handleAddAgentAndConverse} type={'primary'}>
63+
<Button block loading={isLoading} onClick={handleAddAgentAndConverse} type={'primary'}>
6064
{t('addAgentAndConverse')}
6165
</Button>
62-
<Button block onClick={handleAddAgent}>
66+
<Button block loading={isLoading} onClick={handleAddAgent}>
6367
{t('addAgent')}
6468
</Button>
6569
<Flexbox align={'center'} gap={12} horizontal>

src/store/session/slices/session/action.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ describe('SessionAction', () => {
101101
expect(call[1]).toMatchObject({ config: { displayMode: 'docs' } });
102102

103103
expect(createdSessionId).toBe(newSessionId);
104-
expect(mockRouterPush).not.toHaveBeenCalledWith(
105-
SESSION_CHAT_URL(newSessionId, result.current.isMobile),
106-
);
107104
});
108105
});
109106

src/store/session/slices/session/initialState.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export interface SessionState {
88
activeId: string;
99
customSessionGroups: CustomSessionGroup[];
1010
defaultSessions: LobeAgentSession[];
11-
isMobile?: boolean;
1211
isSearching: boolean;
1312
isSessionsFirstFetchFinished: boolean;
1413
pinnedSessions: LobeAgentSession[];
@@ -25,7 +24,6 @@ export const initialSessionState: SessionState = {
2524
activeId: 'inbox',
2625
customSessionGroups: [],
2726
defaultSessions: [],
28-
isMobile: false,
2927
isSearching: false,
3028
isSessionsFirstFetchFinished: false,
3129
pinnedSessions: [],

0 commit comments

Comments
 (0)