From b7f519a679f6f033d8e129174e08bca6edff0474 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Fri, 10 Nov 2023 20:51:51 +0800 Subject: [PATCH] split editMessage code --- src/editMessage.tsx | 59 +++++++++ src/editMessageDetail.tsx | 161 ++++++++++++++++++++++++ src/editMessageString.tsx | 84 +++++++++++++ src/message.tsx | 251 +------------------------------------- 4 files changed, 305 insertions(+), 250 deletions(-) create mode 100644 src/editMessage.tsx create mode 100644 src/editMessageDetail.tsx create mode 100644 src/editMessageString.tsx diff --git a/src/editMessage.tsx b/src/editMessage.tsx new file mode 100644 index 0000000..39751d1 --- /dev/null +++ b/src/editMessage.tsx @@ -0,0 +1,59 @@ +import { Tr, langCodeContext, LANG_OPTIONS } from "./translate"; +import { useState, useEffect, StateUpdater } from "preact/hooks"; +import { ChatStore, ChatStoreMessage } from "./app"; +import { calculate_token_length, getMessageText } from "./chatgpt"; +import { isVailedJSON } from "./message"; +import { EditMessageString } from "./editMessageString"; +import { EditMessageDetail } from "./editMessageDetail"; + +interface EditMessageProps { + chat: ChatStoreMessage; + chatStore: ChatStore; + setShowEdit: StateUpdater; + setChatStore: (cs: ChatStore) => void; +} +export function EditMessage(props: EditMessageProps) { + const { setShowEdit, chat, setChatStore, chatStore } = props; + + return ( +
setShowEdit(false)} + > +
{ + event.stopPropagation(); + }} + > + {typeof chat.content === "string" ? ( + + ) : ( + + )} +
+ +
+
+
+ ); +} diff --git a/src/editMessageDetail.tsx b/src/editMessageDetail.tsx new file mode 100644 index 0000000..ef03e14 --- /dev/null +++ b/src/editMessageDetail.tsx @@ -0,0 +1,161 @@ +import { ChatStore, ChatStoreMessage } from "./app"; +import { calculate_token_length } from "./chatgpt"; +import { Tr } from "./translate"; + +interface Props { + chat: ChatStoreMessage; + chatStore: ChatStore; + setChatStore: (cs: ChatStore) => void; + setShowEdit: (se: boolean) => void; +} +export function EditMessageDetail({ + chat, + chatStore, + setChatStore, + setShowEdit, +}: Props) { + if (typeof chat.content !== "object") return
error
; + return ( +
event.stopPropagation()} + > + {chat.content.map((mdt, index) => ( +
+
+ {mdt.type === "text" ? ( + + ) : ( + <> + { + window.open(mdt.image_url?.url, "_blank"); + }} + /> + + + { + if (typeof chat.content === "string") return; + const obj = chat.content[index].image_url; + if (obj === undefined) return; + obj.detail = obj.detail === "high" ? "low" : "high"; + chat.token = calculate_token_length(chat.content); + setChatStore({ ...chatStore }); + }} + > + + + + + )} + + +
+
+ ))} + + +
+ ); +} diff --git a/src/editMessageString.tsx b/src/editMessageString.tsx new file mode 100644 index 0000000..1890c01 --- /dev/null +++ b/src/editMessageString.tsx @@ -0,0 +1,84 @@ +import { ChatStore, ChatStoreMessage } from "./app"; +import { isVailedJSON } from "./message"; +import { calculate_token_length } from "./chatgpt"; + +interface Props { + chat: ChatStoreMessage; + chatStore: ChatStore; + setChatStore: (cs: ChatStore) => void; + setShowEdit: (se: boolean) => void; +} +export function EditMessageString({ + chat, + chatStore, + setChatStore, + setShowEdit, +}: Props) { + if (typeof chat.content !== "string") return
error
; + return ( +
+ {chat.tool_call_id && ( + + + { + chat.tool_call_id = event.target.value; + setChatStore({ ...chatStore }); + }} + /> + + )} + {chat.tool_calls && + chat.tool_calls.map((tool_call) => ( +
+ + + + + + + + + + + + Vailed JSON:{" "} + {isVailedJSON(tool_call.function.arguments) ? "🆗" : "❌"} + + + +
+
+ ))} + +
+ ); +} diff --git a/src/message.tsx b/src/message.tsx index 853098f..a592fd8 100644 --- a/src/message.tsx +++ b/src/message.tsx @@ -8,13 +8,7 @@ import { MessageHide } from "./messageHide"; import { MessageDetail } from "./messageDetail"; import { MessageToolCall } from "./messageToolCall"; import { MessageToolResp } from "./messageToolResp"; - -interface EditMessageProps { - chat: ChatStoreMessage; - chatStore: ChatStore; - setShowEdit: StateUpdater; - setChatStore: (cs: ChatStore) => void; -} +import { EditMessage } from "./editMessage"; export const isVailedJSON = (str: string): boolean => { try { @@ -25,249 +19,6 @@ export const isVailedJSON = (str: string): boolean => { return true; }; -function EditMessage(props: EditMessageProps) { - const { setShowEdit, chat, setChatStore, chatStore } = props; - - return ( -
setShowEdit(false)} - > -
{ - event.stopPropagation(); - }} - > - {typeof chat.content === "string" ? ( -
- {chat.tool_call_id && ( - - - { - chat.tool_call_id = event.target.value; - setChatStore({ ...chatStore }); - }} - /> - - )} - {chat.tool_calls && - chat.tool_calls.map((tool_call) => ( -
- - - - - - - - - - - - Vailed JSON:{" "} - {isVailedJSON(tool_call.function.arguments) ? "🆗" : "❌"} - - - -
-
- ))} - -
- ) : ( -
event.stopPropagation()} - > - {chat.content.map((mdt, index) => ( -
-
- {mdt.type === "text" ? ( - - ) : ( - <> - { - window.open(mdt.image_url?.url, "_blank"); - }} - /> - - - { - if (typeof chat.content === "string") return; - const obj = chat.content[index].image_url; - if (obj === undefined) return; - obj.detail = obj.detail === "high" ? "low" : "high"; - chat.token = calculate_token_length(chat.content); - setChatStore({ ...chatStore }); - }} - > - - - - - )} - - -
-
- ))} - - -
- )} -
- -
-
-
- ); -} - interface Props { messageIndex: number; chatStore: ChatStore;