Skip to content

Commit

Permalink
🐛 fix: fix agent config on page init (lobehub#2506)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored May 14, 2024
1 parent d61f69a commit 90e742d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 37 deletions.
20 changes: 0 additions & 20 deletions src/app/(main)/chat/(workspace)/_layout/useInitAgentConfig.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
import { useEffect } from 'react';

import { useAgentStore } from '@/store/agent';
import { useChatStore } from '@/store/chat';
import { useSessionStore } from '@/store/session';

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

const [switchTopic] = useChatStore((s) => [s.switchTopic]);
const [sessionId] = useSessionStore((s) => [s.activeId]);

useEffect(() => {
// // when activeId changed, switch topic to undefined
const unsubscribe = useSessionStore.subscribe(
(s) => s.activeId,
(activeId) => {
switchTopic();

useAgentStore.setState({ activeId }, false, 'updateActiveId');
},
);

return () => {
unsubscribe();
};
}, []);

return useFetchAgentConfig(sessionId);
};
17 changes: 10 additions & 7 deletions src/app/(main)/chat/@session/features/SessionHydration.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
'use client';

import { useResponsive } from 'antd-style';
import { useQueryState } from 'nuqs';
import { parseAsString } from 'nuqs/server';
import { memo, useEffect } from 'react';
import { createStoreUpdater } from 'zustand-utils';

import { useAgentStore } from '@/store/agent';
import { useChatStore } from '@/store/chat';
import { useSessionStore } from '@/store/session';

// sync outside state to useSessionStore
const SessionHydration = memo(() => {
const useStoreUpdater = createStoreUpdater(useSessionStore);

// TODO: 后面考虑可以删除了,用 useServerConfigStore 就不需要再这里注入了
const { mobile } = useResponsive();
useStoreUpdater('isMobile', mobile);
const useAgentStoreUpdater = createStoreUpdater(useAgentStore);
const [switchTopic] = useChatStore((s) => [s.switchTopic]);

// two-way bindings the url and session store
const [session, setSession] = useQueryState(
'session',
parseAsString.withDefault('inbox').withOptions({ history: 'replace', throttleMs: 500 }),
parseAsString.withDefault('inbox').withOptions({ history: 'replace', throttleMs: 50 }),
);
useStoreUpdater('activeId', session);
useAgentStoreUpdater('activeId', session);

useEffect(() => {
const unsubscribe = useSessionStore.subscribe(
(s) => s.activeId,
(state) => setSession(state),
(state) => {
switchTopic();
setSession(state);
},
);

return () => {
Expand Down
14 changes: 9 additions & 5 deletions src/app/(main)/market/@detail/features/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { App, Button, Typography } from 'antd';
import isEqual from 'fast-deep-equal';
import { startCase } from 'lodash-es';
import { useRouter } from 'next/navigation';
import { memo } from 'react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Center, Flexbox } from 'react-layout-kit';

Expand All @@ -23,7 +23,7 @@ const Header = memo(() => {
const { styles } = useStyles();
const createSession = useSessionStore((s) => s.createSession);
const agentItem = useMarketStore(agentMarketSelectors.currentAgentItem, isEqual);

const [isLoading, setIsLoading] = useState(false);
const { message } = App.useApp();

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

setIsLoading(true);
const session = await createSession({ config, meta });
setIsLoading(false);
message.success(t('addAgentSuccess'));
router.push(SESSION_CHAT_URL(session, isMobile));
};

const handleAddAgent = () => {
const handleAddAgent = async () => {
if (!agentItem) return;
setIsLoading(true);
createSession({ config, meta }, false);
message.success(t('addAgentSuccess'));
setIsLoading(false);
};

return (
Expand All @@ -56,10 +60,10 @@ const Header = memo(() => {
))}
</Center>
<div className={styles.desc}>{description}</div>
<Button block onClick={handleAddAgentAndConverse} type={'primary'}>
<Button block loading={isLoading} onClick={handleAddAgentAndConverse} type={'primary'}>
{t('addAgentAndConverse')}
</Button>
<Button block onClick={handleAddAgent}>
<Button block loading={isLoading} onClick={handleAddAgent}>
{t('addAgent')}
</Button>
<Flexbox align={'center'} gap={12} horizontal>
Expand Down
3 changes: 0 additions & 3 deletions src/store/session/slices/session/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ describe('SessionAction', () => {
expect(call[1]).toMatchObject({ config: { displayMode: 'docs' } });

expect(createdSessionId).toBe(newSessionId);
expect(mockRouterPush).not.toHaveBeenCalledWith(
SESSION_CHAT_URL(newSessionId, result.current.isMobile),
);
});
});

Expand Down
2 changes: 0 additions & 2 deletions src/store/session/slices/session/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface SessionState {
activeId: string;
customSessionGroups: CustomSessionGroup[];
defaultSessions: LobeAgentSession[];
isMobile?: boolean;
isSearching: boolean;
isSessionsFirstFetchFinished: boolean;
pinnedSessions: LobeAgentSession[];
Expand All @@ -25,7 +24,6 @@ export const initialSessionState: SessionState = {
activeId: 'inbox',
customSessionGroups: [],
defaultSessions: [],
isMobile: false,
isSearching: false,
isSessionsFirstFetchFinished: false,
pinnedSessions: [],
Expand Down

0 comments on commit 90e742d

Please sign in to comment.