feat: close #539 add delete message button

This commit is contained in:
Yidadaa 2023-04-16 18:55:29 +08:00
parent 124938ecc9
commit 12d4081311
11 changed files with 66 additions and 37 deletions

View File

@ -536,21 +536,45 @@ export function Chat(props: {
} }
}; };
const onResend = (botIndex: number) => { const findLastUesrIndex = (messageId: number) => {
// find last user input message and resend // find last user input message and resend
for (let i = botIndex; i >= 0; i -= 1) { let lastUserMessageIndex: number | null = null;
if (messages[i].role === "user") { for (let i = 0; i < session.messages.length; i += 1) {
setIsLoading(true); const message = session.messages[i];
chatStore if (message.id === messageId) {
.onUserInput(messages[i].content) break;
.then(() => setIsLoading(false)); }
chatStore.updateCurrentSession((session) => if (message.role === "user") {
session.messages.splice(i, 2), lastUserMessageIndex = i;
);
inputRef.current?.focus();
return;
} }
} }
return lastUserMessageIndex;
};
const deleteMessage = (userIndex: number) => {
chatStore.updateCurrentSession((session) =>
session.messages.splice(userIndex, 2),
);
};
const onDelete = (botMessageId: number) => {
const userIndex = findLastUesrIndex(botMessageId);
if (userIndex === null) return;
deleteMessage(userIndex);
};
const onResend = (botMessageId: number) => {
// find last user input message and resend
const userIndex = findLastUesrIndex(botMessageId);
if (userIndex === null) return;
setIsLoading(true);
chatStore
.onUserInput(session.messages[userIndex].content)
.then(() => setIsLoading(false));
deleteMessage(userIndex);
inputRef.current?.focus();
}; };
const config = useChatStore((state) => state.config); const config = useChatStore((state) => state.config);
@ -722,12 +746,20 @@ export function Chat(props: {
{Locale.Chat.Actions.Stop} {Locale.Chat.Actions.Stop}
</div> </div>
) : ( ) : (
<div <>
className={styles["chat-message-top-action"]} <div
onClick={() => onResend(i)} className={styles["chat-message-top-action"]}
> onClick={() => onDelete(message.id ?? i)}
{Locale.Chat.Actions.Retry} >
</div> {Locale.Chat.Actions.Delete}
</div>
<div
className={styles["chat-message-top-action"]}
onClick={() => onResend(message.id ?? i)}
>
{Locale.Chat.Actions.Retry}
</div>
</>
)} )}
<div <div

View File

@ -17,6 +17,7 @@ const cn = {
Copy: "复制", Copy: "复制",
Stop: "停止", Stop: "停止",
Retry: "重试", Retry: "重试",
Delete: "删除",
}, },
Rename: "重命名对话", Rename: "重命名对话",
Typing: "正在输入…", Typing: "正在输入…",

View File

@ -19,6 +19,7 @@ const de: LocaleType = {
Copy: "Kopieren", Copy: "Kopieren",
Stop: "Stop", Stop: "Stop",
Retry: "Wiederholen", Retry: "Wiederholen",
Delete: "Delete",
}, },
Rename: "Chat umbenennen", Rename: "Chat umbenennen",
Typing: "Tippen...", Typing: "Tippen...",

View File

@ -19,6 +19,7 @@ const en: LocaleType = {
Copy: "Copy", Copy: "Copy",
Stop: "Stop", Stop: "Stop",
Retry: "Retry", Retry: "Retry",
Delete: "Delete",
}, },
Rename: "Rename Chat", Rename: "Rename Chat",
Typing: "Typing…", Typing: "Typing…",

View File

@ -19,6 +19,7 @@ const es: LocaleType = {
Copy: "Copiar", Copy: "Copiar",
Stop: "Detener", Stop: "Detener",
Retry: "Reintentar", Retry: "Reintentar",
Delete: "Delete",
}, },
Rename: "Renombrar chat", Rename: "Renombrar chat",
Typing: "Escribiendo...", Typing: "Escribiendo...",

View File

@ -54,23 +54,13 @@ export function getLang(): Lang {
const lang = getLanguage(); const lang = getLanguage();
if (lang.includes("zh") || lang.includes("cn")) { for (const option of AllLangs) {
return "cn"; if (lang.includes(option)) {
} else if (lang.includes("tw")) { return option;
return "tw"; }
} else if (lang.includes("es")) {
return "es";
} else if (lang.includes("it")) {
return "it";
} else if (lang.includes("tr")) {
return "tr";
} else if (lang.includes("jp")) {
return "jp";
} else if (lang.includes("de")) {
return "de";
} else {
return "en";
} }
return "en";
} }
export function changeLang(lang: Lang) { export function changeLang(lang: Lang) {
@ -87,4 +77,4 @@ export default {
tr: TR, tr: TR,
jp: JP, jp: JP,
de: DE, de: DE,
}[getLang()]; }[getLang()] as typeof CN;

View File

@ -19,6 +19,7 @@ const it: LocaleType = {
Copy: "Copia", Copy: "Copia",
Stop: "Stop", Stop: "Stop",
Retry: "Riprova", Retry: "Riprova",
Delete: "Delete",
}, },
Rename: "Rinomina Chat", Rename: "Rinomina Chat",
Typing: "Typing…", Typing: "Typing…",

View File

@ -18,6 +18,7 @@ const jp = {
Copy: "コピー", Copy: "コピー",
Stop: "停止", Stop: "停止",
Retry: "リトライ", Retry: "リトライ",
Delete: "Delete",
}, },
Rename: "チャットの名前を変更", Rename: "チャットの名前を変更",
Typing: "入力中…", Typing: "入力中…",
@ -178,6 +179,4 @@ const jp = {
}, },
}; };
export type LocaleType = typeof jp;
export default jp; export default jp;

View File

@ -19,6 +19,7 @@ const tr: LocaleType = {
Copy: "Kopyala", Copy: "Kopyala",
Stop: "Durdur", Stop: "Durdur",
Retry: "Tekrar Dene", Retry: "Tekrar Dene",
Delete: "Delete",
}, },
Rename: "Sohbeti Yeniden Adlandır", Rename: "Sohbeti Yeniden Adlandır",
Typing: "Yazıyor…", Typing: "Yazıyor…",

View File

@ -18,6 +18,7 @@ const tw: LocaleType = {
Copy: "複製", Copy: "複製",
Stop: "停止", Stop: "停止",
Retry: "重試", Retry: "重試",
Delete: "刪除",
}, },
Rename: "重命名對話", Rename: "重命名對話",
Typing: "正在輸入…", Typing: "正在輸入…",

View File

@ -386,6 +386,7 @@ export const useChatStore = create<ChatStore>()(
const botMessage: Message = createMessage({ const botMessage: Message = createMessage({
role: "assistant", role: "assistant",
streaming: true, streaming: true,
id: userMessage.id! + 1,
}); });
// get recent messages // get recent messages