Skip to content

Commit

Permalink
⚡️ perf: improve chat session performance (lobehub#2613)
Browse files Browse the repository at this point in the history
* 🎨 chore: improve initial state

* ⚡️ perf: improve session performance
  • Loading branch information
arvinxx authored May 22, 2024
1 parent 6842e46 commit c7fc8da
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/app/(main)/chat/@session/default.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { Suspense, lazy } from 'react';

import ServerLayout from '@/components/server/ServerLayout';

import Desktop from './_layout/Desktop';
import Mobile from './_layout/Mobile';
import SessionHydration from './features/SessionHydration';
import SessionListContent from './features/SessionListContent';
import SkeletonList from './features/SkeletonList';

const SessionListContent = lazy(() => import('./features/SessionListContent'));

const Layout = ServerLayout({ Desktop, Mobile });

const Session = () => {
return (
<>
<Layout>
<SessionListContent />
<Suspense fallback={<SkeletonList />}>
<SessionListContent />
</Suspense>
</Layout>
<SessionHydration />
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useSessionStore } from '@/store/session';
import { sessionSelectors } from '@/store/session/selectors';
import { LobeAgentSession } from '@/types/session';

import SkeletonList from '../SkeletonList';
import SkeletonList from '../../SkeletonList';
import AddButton from './AddButton';
import SessionItem from './Item';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { memo } from 'react';

import { useSessionStore } from '@/store/session';

import SkeletonList from '../SkeletonList';
import SessionList from './List';
import SkeletonList from './SkeletonList';

const SearchMode = memo(() => {
const [sessionSearchKeywords, useSearchSessions] = useSessionStore((s) => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { Skeleton } from 'antd';
import { createStyles } from 'antd-style';
import { memo } from 'react';
Expand Down
5 changes: 5 additions & 0 deletions src/store/session/slices/session/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ export const createSessionSlice: StateCreator<

useFetchSessions: () =>
useClientDataSWR<ChatSessionList>(FETCH_SESSIONS_KEY, sessionService.getGroupedSessions, {
fallbackData: {
sessionGroups: [],
sessions: [],
},
onSuccess: (data) => {
if (
get().isSessionsFirstFetchFinished &&
Expand All @@ -209,6 +213,7 @@ export const createSessionSlice: StateCreator<
);
set({ isSessionsFirstFetchFinished: true }, false, n('useFetchSessions/onSuccess', data));
},
suspense: true,
}),
useSearchSessions: (keyword) =>
useSWR<LobeSessions>(
Expand Down
6 changes: 1 addition & 5 deletions src/store/session/slices/session/initialState.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { CustomSessionGroup, LobeAgentSession, LobeSessionGroups } from '@/types/session';
import { LobeAgentSession } from '@/types/session';

export interface SessionState {
/**
* @title 当前活动的会话
* @description 当前正在编辑或查看的会话
*/
activeId: string;
customSessionGroups: CustomSessionGroup[];
defaultSessions: LobeAgentSession[];
isSearching: boolean;
isSessionsFirstFetchFinished: boolean;
pinnedSessions: LobeAgentSession[];
searchKeywords: string;
sessionGroups: LobeSessionGroups;
sessionSearchKeywords?: string;
/**
* it means defaultSessions
Expand All @@ -22,12 +20,10 @@ export interface SessionState {

export const initialSessionState: SessionState = {
activeId: 'inbox',
customSessionGroups: [],
defaultSessions: [],
isSearching: false,
isSessionsFirstFetchFinished: false,
pinnedSessions: [],
searchKeywords: '',
sessionGroups: [],
sessions: [],
};
9 changes: 8 additions & 1 deletion src/store/session/slices/sessionGroup/initialState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { CustomSessionGroup, LobeSessionGroups } from '@/types/session';

export interface SessionGroupState {
activeGroupId?: string;
customSessionGroups: CustomSessionGroup[];
sessionGroups: LobeSessionGroups;
}

export const initSessionGroupState: SessionGroupState = {};
export const initSessionGroupState: SessionGroupState = {
customSessionGroups: [],
sessionGroups: [],
};

0 comments on commit c7fc8da

Please sign in to comment.