diff --git a/.gitignore b/.gitignore index a731251..5be3dc7 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,5 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts -.idea \ No newline at end of file +.idea +pnpm-lock.yaml diff --git a/components/Chat/ChatMessage.tsx b/components/Chat/ChatMessage.tsx index bf05825..cf490e3 100644 --- a/components/Chat/ChatMessage.tsx +++ b/components/Chat/ChatMessage.tsx @@ -3,6 +3,7 @@ import { IconCopy, IconEdit, IconRobot, + IconTrash, IconUser, } from '@tabler/icons-react'; import { FC, memo, useContext, useEffect, useRef, useState } from 'react'; @@ -31,7 +32,7 @@ export const ChatMessage: FC = memo(({ message, messageIndex }) => { const { t } = useTranslation('chat'); const { - state: { selectedConversation, conversations }, + state: { selectedConversation, conversations, currentMessage }, dispatch: homeDispatch, } = useContext(HomeContext); @@ -86,6 +87,35 @@ export const ChatMessage: FC = memo(({ message, messageIndex }) => { setIsEditing(false); }; + const handleDeleteMessage = () => { + if (!selectedConversation) return; + + const { messages } = selectedConversation; + const findIndex = messages.findIndex((elm) => elm === message); + + if (findIndex < 0) return; + + if ( + findIndex < messages.length - 1 && + messages[findIndex + 1].role === 'assistant' + ) { + messages.splice(findIndex, 2); + } else { + messages.splice(findIndex, 1); + } + const updatedConversation = { + ...selectedConversation, + messages, + }; + + const { single, all } = updateConversation( + updatedConversation, + conversations, + ); + homeDispatch({ field: 'selectedConversation', value: single }); + homeDispatch({ field: 'conversations', value: all }); + }; + const handlePressEnter = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !isTyping && !e.shiftKey) { e.preventDefault(); @@ -178,17 +208,30 @@ export const ChatMessage: FC = memo(({ message, messageIndex }) => { )} {(window.innerWidth < 640 || !isEditing) && ( - + <> + + + )} ) : (