♻️ refactor: refactor chat selectors (#10274)

refactor chat selectors to displayMessageSelectors
This commit is contained in:
Arvin Xu
2025-11-19 13:00:03 +08:00
committed by GitHub
parent c5d71fe165
commit 0a056f3f0b
7 changed files with 20 additions and 20 deletions

View File

@@ -4,7 +4,7 @@ import { useAnalytics } from '@lobehub/analytics/react';
import { memo, useCallback, useEffect } from 'react';
import { getChatStoreState } from '@/store/chat';
import { chatSelectors } from '@/store/chat/slices/message/selectors';
import { displayMessageSelectors } from '@/store/chat/selectors';
import { useGlobalStore } from '@/store/global';
import { systemStatusSelectors } from '@/store/global/selectors';
import { getSessionStoreState } from '@/store/session';
@@ -18,7 +18,7 @@ const MainInterfaceTracker = memo(() => {
const activeSessionId = currentSession?.id;
const defaultSessions = sessionSelectors.defaultSessions(getSessionStoreState());
const showChatSideBar = systemStatusSelectors.showChatSideBar(useGlobalStore.getState());
const messages = chatSelectors.activeBaseChats(getChatStoreState());
const messages = displayMessageSelectors.activeDisplayMessages(getChatStoreState());
return {
active_assistant: activeSessionId === 'inbox' ? null : currentSession?.meta?.title || null,
has_chat_history: messages.length > 0,

View File

@@ -1,9 +1,9 @@
import { ARTIFACT_THINKING_TAG } from '@lobechat/const';
import { memo } from 'react';
import Thinking from '@/components/Thinking';
import { ARTIFACT_THINKING_TAG } from '@/const/plugin';
import { useChatStore } from '@/store/chat';
import { chatSelectors } from '@/store/chat/selectors';
import { dbMessageSelectors } from '@/store/chat/selectors';
import { useUserStore } from '@/store/user';
import { userGeneralSettingsSelectors } from '@/store/user/selectors';
@@ -12,7 +12,7 @@ import { isTagClosed } from '../utils';
const Render = memo<MarkdownElementProps>(({ children, id }) => {
const [isGenerating] = useChatStore((s) => {
const message = chatSelectors.getMessageById(id)(s);
const message = dbMessageSelectors.getDbMessageById(id)(s);
return [!isTagClosed(ARTIFACT_THINKING_TAG, message?.content)];
});
const transitionMode = useUserStore(userGeneralSettingsSelectors.transitionMode);

View File

@@ -2,7 +2,7 @@ import { memo } from 'react';
import Thinking from '@/components/Thinking';
import { useChatStore } from '@/store/chat';
import { chatSelectors } from '@/store/chat/selectors';
import { dbMessageSelectors } from '@/store/chat/selectors';
import { useUserStore } from '@/store/user';
import { userGeneralSettingsSelectors } from '@/store/user/selectors';
@@ -17,11 +17,11 @@ const isThinkingClosed = (input: string = '') => {
const Render = memo<MarkdownElementProps>(({ children, id }) => {
const [isGenerating] = useChatStore((s) => {
const message = chatSelectors.getMessageById(id)(s);
const message = dbMessageSelectors.getDbMessageById(id)(s);
return [!isThinkingClosed(message?.content)];
});
const citations = useChatStore((s) => {
const message = chatSelectors.getMessageById(id)(s);
const message = dbMessageSelectors.getDbMessageById(id)(s);
return message?.search?.citations;
});

View File

@@ -3,7 +3,7 @@ import { Skeleton } from 'antd';
import { memo, useRef, useState } from 'react';
import { useChatStore } from '@/store/chat';
import { chatSelectors } from '@/store/chat/selectors';
import { dbMessageSelectors } from '@/store/chat/selectors';
import { useToolStore } from '@/store/tool';
import { pluginSelectors } from '@/store/tool/selectors';
@@ -28,7 +28,7 @@ import {
// just to simplify code a little, don't use this pattern everywhere
const getSettings = (identifier: string) =>
pluginSelectors.getPluginSettingsById(identifier)(useToolStore.getState());
const getMessage = (id: string) => chatSelectors.getMessageById(id)(useChatStore.getState());
const getMessage = (id: string) => dbMessageSelectors.getDbMessageById(id)(useChatStore.getState());
interface IFrameRenderProps {
height?: number;
@@ -61,7 +61,7 @@ const IFrameRender = memo<IFrameRenderProps>(({ url, id, payload, width = 600, h
const iframeWin = iframeRef.current?.contentWindow;
if (iframeWin) {
const message = chatSelectors.getMessageById(id)(useChatStore.getState());
const message = dbMessageSelectors.getDbMessageById(id)(useChatStore.getState());
if (!message) return;
const props = { content: '' };

View File

@@ -8,14 +8,14 @@ import { Center, Flexbox } from 'react-layout-kit';
import Balancer from 'react-wrap-balancer';
import { useChatStore } from '@/store/chat';
import { chatSelectors } from '@/store/chat/selectors';
import { dbMessageSelectors, displayMessageSelectors } from '@/store/chat/selectors';
import ArtifactItem from './Item';
const ArtifactList = () => {
const { t } = useTranslation('portal');
const messages = useChatStore(chatSelectors.currentToolMessages, isEqual);
const isCurrentChatLoaded = useChatStore(chatSelectors.isCurrentChatLoaded);
const messages = useChatStore(dbMessageSelectors.dbToolMessages, isEqual);
const isCurrentChatLoaded = useChatStore(displayMessageSelectors.isCurrentDisplayChatLoaded);
const theme = useTheme();
return !isCurrentChatLoaded ? (

View File

@@ -1,3 +1,4 @@
import { FORM_STYLE } from '@lobechat/const';
import { exportFile } from '@lobechat/utils/client';
import { Button, Form, type FormItemProps, copyToClipboard } from '@lobehub/ui';
import { App, Switch } from 'antd';
@@ -7,12 +8,11 @@ import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';
import { FORM_STYLE } from '@/const/layoutTokens';
import { useIsMobile } from '@/hooks/useIsMobile';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { useChatStore } from '@/store/chat';
import { chatSelectors, topicSelectors } from '@/store/chat/selectors';
import { displayMessageSelectors, topicSelectors } from '@/store/chat/selectors';
import { useStyles } from '../style';
import Preview from './Preview';
@@ -67,7 +67,7 @@ const ShareText = memo(() => {
];
const [systemRole] = useAgentStore((s) => [agentSelectors.currentAgentSystemRole(s)]);
const messages = useChatStore(chatSelectors.activeBaseChats, isEqual);
const messages = useChatStore(displayMessageSelectors.activeDisplayMessages, isEqual);
const topic = useChatStore(topicSelectors.currentActiveTopic, isEqual);
const title = topic?.title || t('shareModal.exportTitle');

View File

@@ -27,7 +27,7 @@ import { ChatTopic, CreateTopicParams } from '@/types/topic';
import { merge } from '@/utils/merge';
import { setNamespace } from '@/utils/storeDebug';
import { chatSelectors } from '../message/selectors';
import { displayMessageSelectors } from '../message/selectors';
import { ChatTopicDispatch, topicReducer } from './reducer';
import { topicSelectors } from './selectors';
@@ -92,7 +92,7 @@ export const chatTopic: StateCreator<
createTopic: async (sessionId, groupId) => {
const { activeId, activeSessionType, internal_createTopic } = get();
const messages = chatSelectors.activeBaseChats(get());
const messages = displayMessageSelectors.activeDisplayMessages(get());
set({ creatingTopic: true }, false, n('creatingTopic/start'));
const topicId = await internal_createTopic({
@@ -109,7 +109,7 @@ export const chatTopic: StateCreator<
saveToTopic: async (sessionId, groupId) => {
// if there is no message, stop
const messages = chatSelectors.activeBaseChats(get());
const messages = displayMessageSelectors.activeDisplayMessages(get());
if (messages.length === 0) return;
const { activeId, activeSessionType, summaryTopicTitle, internal_createTopic } = get();