delete message (#531)

* feat: delete single message

* improve plugin hotkey support (#426)

* feat: delete single message

* remove logs

* remove logs

---------

Co-authored-by: Ryland <rylandl@qq.com>
Co-authored-by: Dornfeld Capital <75278543+dornfeld-capital@users.noreply.github.com>
Co-authored-by: L.Ryland <41134883+L-Ryland@users.noreply.github.com>
This commit is contained in:
Mckay Wrigley 2023-04-13 05:01:27 -06:00 committed by GitHub
parent f61e91d07f
commit d1eb6ee29b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 13 deletions

1
.gitignore vendored
View File

@ -37,3 +37,4 @@ yarn-error.log*
*.tsbuildinfo *.tsbuildinfo
next-env.d.ts next-env.d.ts
.idea .idea
pnpm-lock.yaml

View File

@ -3,6 +3,7 @@ import {
IconCopy, IconCopy,
IconEdit, IconEdit,
IconRobot, IconRobot,
IconTrash,
IconUser, IconUser,
} from '@tabler/icons-react'; } from '@tabler/icons-react';
import { FC, memo, useContext, useEffect, useRef, useState } from 'react'; import { FC, memo, useContext, useEffect, useRef, useState } from 'react';
@ -31,7 +32,7 @@ export const ChatMessage: FC<Props> = memo(({ message, messageIndex }) => {
const { t } = useTranslation('chat'); const { t } = useTranslation('chat');
const { const {
state: { selectedConversation, conversations }, state: { selectedConversation, conversations, currentMessage },
dispatch: homeDispatch, dispatch: homeDispatch,
} = useContext(HomeContext); } = useContext(HomeContext);
@ -86,6 +87,35 @@ export const ChatMessage: FC<Props> = memo(({ message, messageIndex }) => {
setIsEditing(false); 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<HTMLTextAreaElement>) => { const handlePressEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !isTyping && !e.shiftKey) { if (e.key === 'Enter' && !isTyping && !e.shiftKey) {
e.preventDefault(); e.preventDefault();
@ -178,6 +208,18 @@ export const ChatMessage: FC<Props> = memo(({ message, messageIndex }) => {
)} )}
{(window.innerWidth < 640 || !isEditing) && ( {(window.innerWidth < 640 || !isEditing) && (
<>
<button
className={`absolute translate-x-[1000px] text-gray-500 hover:text-gray-700 focus:translate-x-0 group-hover:translate-x-0 dark:text-gray-400 dark:hover:text-gray-300 ${
window.innerWidth < 640
? 'bottom-1 right-3'
: 'right-6 top-[26px]'
}
`}
onClick={toggleEditing}
>
<IconEdit size={20} />
</button>
<button <button
className={`absolute translate-x-[1000px] text-gray-500 hover:text-gray-700 focus:translate-x-0 group-hover:translate-x-0 dark:text-gray-400 dark:hover:text-gray-300 ${ className={`absolute translate-x-[1000px] text-gray-500 hover:text-gray-700 focus:translate-x-0 group-hover:translate-x-0 dark:text-gray-400 dark:hover:text-gray-300 ${
window.innerWidth < 640 window.innerWidth < 640
@ -185,10 +227,11 @@ export const ChatMessage: FC<Props> = memo(({ message, messageIndex }) => {
: 'right-0 top-[26px]' : 'right-0 top-[26px]'
} }
`} `}
onClick={toggleEditing} onClick={handleDeleteMessage}
> >
<IconEdit size={20} /> <IconTrash size={20} />
</button> </button>
</>
)} )}
</div> </div>
) : ( ) : (