Enhance message handling in chatSlice

- Added condition to check if `action.payload.hasDone` is true, then update the hash of the last two messages in `state.messages`.
- Fixed the `splice` method in `deleteMessage.fulfilled` to correctly remove the message from `state.messages`.
- Updated `stopGenerating` dispatch in `ChatPanel.tsx` to include the message in the payload.
- Modified the delete message action in `MessageHeader.tsx` to handle the case when `item.hash` is not present.
This commit is contained in:
Rankin Zheng 2023-07-19 16:03:39 +08:00
parent 70be0acd16
commit 8a5ecbc001
3 changed files with 24 additions and 6 deletions

View File

@ -69,7 +69,7 @@ const StopButton = () => {
}
}}
onClick={() => {
dispatch(stopGenerating({ hasDone: false }));
dispatch(stopGenerating({ hasDone: false, message: null }));
messageUtil.sendMessage({
command: 'stopDevChat'
});
@ -124,8 +124,8 @@ const chatPanel = () => {
dispatch(startResponsing(message.text));
timer.start();
});
messageUtil.registerHandler('receiveMessage', (message: { text: string; isError: boolean }) => {
dispatch(stopGenerating({ hasDone: true }));
messageUtil.registerHandler('receiveMessage', (message: { text: string; isError: boolean, hash }) => {
dispatch(stopGenerating({ hasDone: true, message: message }));
if (message.isError) {
dispatch(happendError(message.text));
}

View File

@ -15,7 +15,8 @@ import {
} from './inputSlice';
import {
deleteMessage
deleteMessage,
popMessage
} from './chatSlice';
const MessageHeader = (props: any) => {
@ -71,10 +72,15 @@ const MessageHeader = (props: any) => {
<IconEdit size="1.125rem" />
</ActionIcon>
</Tooltip >}
{showDelete && hash && <Tooltip sx={{ padding: '3px', fontSize: 'var(--vscode-editor-font-size)' }} label="Delete message" withArrow position="left" color="gray">
{showDelete && hash !== 'message' && <Tooltip sx={{ padding: '3px', fontSize: 'var(--vscode-editor-font-size)' }} label="Delete message" withArrow position="left" color="gray">
<ActionIcon size='sm'
onClick={() => {
dispatch(deleteMessage(item));
if (item.hash) {
dispatch(deleteMessage(item));
} else {
dispatch(popMessage());
dispatch(popMessage());
}
}}>
<IconTrash size="1.125rem" />
</ActionIcon>

View File

@ -85,6 +85,17 @@ export const chatSlice = createSlice({
state.generating = false;
state.responsed = false;
state.hasDone = action.payload.hasDone;
if (action.payload.hasDone) {
const { hash } = action.payload.message;
const messagesLength = state.messages.length;
if (messagesLength > 1) {
state.messages[messagesLength - 2].hash = hash;
state.messages[messagesLength - 1].hash = hash;
} else if (messagesLength > 0) {
state.messages[messagesLength - 1].hash = hash;
}
}
},
startResponsing: (state, action) => {
state.responsed = true;
@ -151,6 +162,7 @@ export const chatSlice = createSlice({
.addCase(deleteMessage.fulfilled, (state, action) => {
const { hash } = action.payload;
const index = state.messages.findIndex((item: any) => item.hash === hash);
debugger
if (index > -1) {
state.messages.splice(index);
}