Refactor CurrentMessage component to use MobX
- Replaced Redux hooks with MobX hooks in CurrentMessage component. - Removed Redux actions and replaced them with equivalent MobX actions. - Updated useEffect hooks to use MobX actions instead of Redux actions. - Replaced Redux selectors with equivalent MobX observables. - Converted CurrentMessage and MessageBlink components to observer components.
This commit is contained in:
parent
5bbe9963b8
commit
8e944b4958
@ -2,20 +2,13 @@
|
|||||||
import React, { useEffect } from "react";
|
import React, { useEffect } from "react";
|
||||||
import { keyframes } from "@emotion/react";
|
import { keyframes } from "@emotion/react";
|
||||||
import { Container, Text } from "@mantine/core";
|
import { Container, Text } from "@mantine/core";
|
||||||
import { useAppDispatch, useAppSelector } from '@/views/hooks';
|
|
||||||
import CodeBlock from "@/views/components/CodeBlock";
|
import CodeBlock from "@/views/components/CodeBlock";
|
||||||
|
import { observer } from "mobx-react-lite";
|
||||||
|
import { useMst } from "@/views/stores/RootStore";
|
||||||
|
|
||||||
import {
|
|
||||||
newMessage,
|
|
||||||
updateLastMessage,
|
|
||||||
selectGenerating,
|
|
||||||
selectCurrentMessage,
|
|
||||||
selecLastMessage,
|
|
||||||
selectResponsed,
|
|
||||||
} from '@/views/reducers/chatSlice';
|
|
||||||
|
|
||||||
const MessageBlink = () => {
|
const MessageBlink = observer(() => {
|
||||||
const responsed = useAppSelector(selectResponsed);
|
const { chat } = useMst();
|
||||||
|
|
||||||
const blink = keyframes({
|
const blink = keyframes({
|
||||||
'50%': { opacity: 0 },
|
'50%': { opacity: 0 },
|
||||||
@ -24,11 +17,11 @@ const MessageBlink = () => {
|
|||||||
return <Text sx={{
|
return <Text sx={{
|
||||||
animation: `${blink} 0.5s infinite;`,
|
animation: `${blink} 0.5s infinite;`,
|
||||||
width: 5,
|
width: 5,
|
||||||
marginTop: responsed ? 0 : '1em',
|
marginTop: chat.responsed ? 0 : '1em',
|
||||||
backgroundColor: 'black',
|
backgroundColor: 'black',
|
||||||
display: 'block'
|
display: 'block'
|
||||||
}}>|</Text>;
|
}}>|</Text>;
|
||||||
};
|
});
|
||||||
|
|
||||||
const getBlocks = (message) => {
|
const getBlocks = (message) => {
|
||||||
const messageText = message || '';
|
const messageText = message || '';
|
||||||
@ -51,36 +44,31 @@ const getBlocks = (message) => {
|
|||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CurrentMessage = (props: any) => {
|
const CurrentMessage = observer((props: any) => {
|
||||||
const { width } = props;
|
const { width } = props;
|
||||||
|
const { chat } = useMst();
|
||||||
const dispatch = useAppDispatch();
|
|
||||||
const currentMessage = useAppSelector(selectCurrentMessage);
|
|
||||||
const lastMessage = useAppSelector(selecLastMessage);
|
|
||||||
const generating = useAppSelector(selectGenerating);
|
|
||||||
const responsed = useAppSelector(selectResponsed);
|
|
||||||
|
|
||||||
// split blocks
|
// split blocks
|
||||||
const messageBlocks = getBlocks(currentMessage);
|
const messageBlocks = getBlocks(chat.currentMessage);
|
||||||
const lastMessageBlocks = getBlocks(lastMessage?.message);
|
const lastMessageBlocks = getBlocks(chat.lastMessage?.message);
|
||||||
const fixedCount = lastMessageBlocks.length;
|
const fixedCount = lastMessageBlocks.length;
|
||||||
const receivedCount = messageBlocks.length;
|
const receivedCount = messageBlocks.length;
|
||||||
const renderBlocks = messageBlocks.splice(-1);
|
const renderBlocks = messageBlocks.splice(-1);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (generating) {
|
if (chat.generating) {
|
||||||
// new a bot message
|
// new a bot message
|
||||||
dispatch(newMessage({ type: 'bot', message: currentMessage }));
|
chat.newMessage({ type: 'bot', message: chat.currentMessage });
|
||||||
}
|
}
|
||||||
}, [generating]);
|
}, [chat.generating]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (receivedCount - fixedCount >= 1 || !responsed) {
|
if (receivedCount - fixedCount >= 1 || !chat.responsed) {
|
||||||
dispatch(updateLastMessage({ type: 'bot', message: currentMessage }));
|
chat.updateLastMessage({ type: 'bot', message: chat.currentMessage });
|
||||||
}
|
}
|
||||||
}, [currentMessage, responsed]);
|
}, [chat.currentMessage, chat.responsed]);
|
||||||
|
|
||||||
return generating
|
return chat.generating
|
||||||
? <Container
|
? <Container
|
||||||
sx={{
|
sx={{
|
||||||
margin: 0,
|
margin: 0,
|
||||||
@ -94,6 +82,6 @@ const CurrentMessage = (props: any) => {
|
|||||||
<MessageBlink />
|
<MessageBlink />
|
||||||
</Container>
|
</Container>
|
||||||
: <></>;
|
: <></>;
|
||||||
};
|
});
|
||||||
|
|
||||||
export default CurrentMessage;
|
export default CurrentMessage;
|
Loading…
x
Reference in New Issue
Block a user