fix: prevent the title from being empty, optimization Logic

This commit is contained in:
AprilNEA 2023-03-31 03:16:54 +08:00
parent 2da7e875d7
commit cf41cb1874
No known key found for this signature in database
GPG Key ID: B93E17BB436B4DE1
2 changed files with 21 additions and 34 deletions

View File

@ -88,10 +88,16 @@ export function ChatItem(props: {
time: string; time: string;
selected: boolean; selected: boolean;
}) { }) {
const [dialog, setDialog] = useState({ const [isEditingTitle, setIsEditingTitle] = useState(false);
isEdit: false, const [title, setTitle] = useState(props.title);
title: props.title,
}); function editCompleted() {
setIsEditingTitle(false);
// Blank will be restored to its original state
if (!title) setTitle(props.title);
props.onEdit && props.onEdit(title);
}
return ( return (
<div <div
className={`${styles["chat-item"]} ${ className={`${styles["chat-item"]} ${
@ -100,17 +106,14 @@ export function ChatItem(props: {
onClick={props.onClick} onClick={props.onClick}
> >
<div className={styles["chat-item-title"]}> <div className={styles["chat-item-title"]}>
{dialog.isEdit ? ( {isEditingTitle ? (
<input <input
autoFocus autoFocus
type="text" type="text"
className={styles["chat-item-edit-input"]} className={styles["chat-item-edit-input"]}
value={dialog.title} value={title}
onChange={(data) => { onChange={(data) => {
setDialog({ setTitle(data.target.value);
...dialog,
title: data.target.value,
});
}} }}
onBlur={editCompleted} onBlur={editCompleted}
onKeyDown={(event) => { onKeyDown={(event) => {
@ -120,7 +123,7 @@ export function ChatItem(props: {
}} }}
/> />
) : ( ) : (
<div>{dialog.title}</div> title
)} )}
</div> </div>
<div className={styles["chat-item-info"]}> <div className={styles["chat-item-info"]}>
@ -132,25 +135,14 @@ export function ChatItem(props: {
<div className={styles["chat-item-delete"]} onClick={props.onDelete}> <div className={styles["chat-item-delete"]} onClick={props.onDelete}>
<DeleteIcon /> <DeleteIcon />
</div> </div>
<div className={styles["chat-item-edit"]} onClick={editStart}> <div
className={styles["chat-item-edit"]}
onClick={() => setIsEditingTitle(true)}
>
<EditIcon /> <EditIcon />
</div> </div>
</div> </div>
); );
function editStart() {
setDialog({
...dialog,
isEdit: true,
});
}
function editCompleted() {
setDialog({
...dialog,
isEdit: false,
});
props.onEdit && props.onEdit(dialog.title);
}
} }
export function ChatList() { export function ChatList() {
@ -165,7 +157,7 @@ export function ChatList() {
state.currentSessionIndex, state.currentSessionIndex,
state.selectSession, state.selectSession,
state.removeSession, state.removeSession,
state.editDialogTitle, state.updateSessionTitle,
]); ]);
return ( return (

View File

@ -188,14 +188,13 @@ interface ChatStore {
removeSession: (index: number) => void; removeSession: (index: number) => void;
selectSession: (index: number) => void; selectSession: (index: number) => void;
newSession: () => void; newSession: () => void;
editDialogTitle: (index: number, title: string) => void; updateSessionTitle: (index: number, title: string) => void;
currentSession: () => ChatSession; currentSession: () => ChatSession;
onNewMessage: (message: Message) => void; onNewMessage: (message: Message) => void;
onUserInput: (content: string) => Promise<void>; onUserInput: (content: string) => Promise<void>;
summarizeSession: () => void; summarizeSession: () => void;
updateStat: (message: Message) => void; updateStat: (message: Message) => void;
updateCurrentSession: (updater: (session: ChatSession) => void) => void; updateCurrentSession: (updater: (session: ChatSession) => void) => void;
updateTitle: (title: string) => void;
updateMessage: ( updateMessage: (
sessionIndex: number, sessionIndex: number,
messageIndex: number, messageIndex: number,
@ -270,7 +269,7 @@ export const useChatStore = create<ChatStore>()(
}); });
}, },
editDialogTitle(index: number, title: string) { updateSessionTitle(index: number, title: string) {
set((state) => { set((state) => {
const sessions = state.sessions; const sessions = state.sessions;
sessions[index].topic = title; sessions[index].topic = title;
@ -409,10 +408,6 @@ export const useChatStore = create<ChatStore>()(
set(() => ({ sessions })); set(() => ({ sessions }));
}, },
updateTitle(title: string) {
get().updateCurrentSession((session) => (session.topic = title));
},
summarizeSession() { summarizeSession() {
const session = get().currentSession(); const session = get().currentSession();