fix: fix new chat title subscription

This commit is contained in:
AprilNEA 2023-03-31 03:33:17 +08:00
parent cf41cb1874
commit e361ec0cee
No known key found for this signature in database
GPG Key ID: B93E17BB436B4DE1
2 changed files with 20 additions and 28 deletions

View File

@ -82,20 +82,25 @@ export function Avatar(props: { role: Message["role"] }) {
export function ChatItem(props: { export function ChatItem(props: {
onClick?: () => void; onClick?: () => void;
onDelete?: () => void; onDelete?: () => void;
onEdit?: (title: string) => void;
title: string; title: string;
count: number; count: number;
time: string; time: string;
selected: boolean; selected: boolean;
}) { }) {
const updateTitle = useChatStore((state) => state.updateTitle);
const [isEditingTitle, setIsEditingTitle] = useState(false); const [isEditingTitle, setIsEditingTitle] = useState(false);
const [title, setTitle] = useState(props.title); const [title, setTitle] = useState(props.title);
useEffect(() => {
setTitle(props.title);
}, [props.title]);
function editCompleted() { function editCompleted() {
setIsEditingTitle(false); setIsEditingTitle(false);
// Blank will be restored to its original state // Blank will be restored to its original state
if (!title) setTitle(props.title); if (!title) setTitle(props.title);
props.onEdit && props.onEdit(title); updateTitle(title);
} }
return ( return (
@ -146,19 +151,14 @@ export function ChatItem(props: {
} }
export function ChatList() { export function ChatList() {
const [ const [sessions, selectedIndex, selectSession, removeSession] = useChatStore(
sessions, (state) => [
selectedIndex, state.sessions,
selectSession, state.currentSessionIndex,
removeSession, state.selectSession,
editDialogTitle, state.removeSession,
] = useChatStore((state) => [ ],
state.sessions, );
state.currentSessionIndex,
state.selectSession,
state.removeSession,
state.updateSessionTitle,
]);
return ( return (
<div className={styles["chat-list"]}> <div className={styles["chat-list"]}>
@ -171,7 +171,6 @@ export function ChatList() {
selected={i === selectedIndex} selected={i === selectedIndex}
onClick={() => selectSession(i)} onClick={() => selectSession(i)}
onDelete={() => removeSession(i)} onDelete={() => removeSession(i)}
onEdit={(title: string) => editDialogTitle(i, title)}
/> />
))} ))}
</div> </div>

View File

@ -188,13 +188,13 @@ interface ChatStore {
removeSession: (index: number) => void; removeSession: (index: number) => void;
selectSession: (index: number) => void; selectSession: (index: number) => void;
newSession: () => void; newSession: () => 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,
@ -269,17 +269,6 @@ export const useChatStore = create<ChatStore>()(
}); });
}, },
updateSessionTitle(index: number, title: string) {
set((state) => {
const sessions = state.sessions;
sessions[index].topic = title;
return {
sessions,
};
});
},
newSession() { newSession() {
set((state) => ({ set((state) => ({
currentSessionIndex: 0, currentSessionIndex: 0,
@ -408,6 +397,10 @@ 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();