用户输入框内容会随着Session切换而切换

This commit is contained in:
dakai 2023-04-14 01:34:06 +08:00
parent ef269acc20
commit c67af258d3
3 changed files with 29 additions and 20 deletions

View File

@ -87,19 +87,12 @@ export function ChatItem(props: {
} }
export function ChatList() { export function ChatList() {
const [ const [sidebarCollapse, sessions, selectedIndex, selectSession, moveSession] =
sidebarCollapse, useChatStore((state) => [
sessions,
selectedIndex,
selectSession,
removeSession,
moveSession,
] = useChatStore((state) => [
state.sidebarCollapse, state.sidebarCollapse,
state.sessions, state.sessions,
state.currentSessionIndex, state.currentSessionIndex,
state.selectSession, state.selectSession,
state.removeSession,
state.moveSession, state.moveSession,
]); ]);
const chatStore = useChatStore(); const chatStore = useChatStore();

View File

@ -1,4 +1,4 @@
import { useDebounce, useDebouncedCallback } from "use-debounce"; import { useDebouncedCallback } from "use-debounce";
import { memo, useState, useRef, useEffect, useLayoutEffect } from "react"; import { memo, useState, useRef, useEffect, useLayoutEffect } from "react";
import SendWhiteIcon from "../icons/send-white.svg"; import SendWhiteIcon from "../icons/send-white.svg";
@ -37,7 +37,6 @@ import {
isMobileScreen, isMobileScreen,
selectOrCopy, selectOrCopy,
autoGrowTextArea, autoGrowTextArea,
getCSSVar,
} from "../utils"; } from "../utils";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
@ -414,7 +413,7 @@ export function Chat() {
const fontSize = useChatStore((state) => state.config.fontSize); const fontSize = useChatStore((state) => state.config.fontSize);
const inputRef = useRef<HTMLTextAreaElement>(null); const inputRef = useRef<HTMLTextAreaElement>(null);
const [userInput, setUserInput] = useState(""); const [userInput, setUserInput] = useState(session.userInput);
const [beforeInput, setBeforeInput] = useState(""); const [beforeInput, setBeforeInput] = useState("");
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const { submitKey, shouldSubmit } = useSubmitHandler(); const { submitKey, shouldSubmit } = useSubmitHandler();
@ -471,8 +470,23 @@ export function Chat() {
}, },
); );
const updateUserInput = (text: string) => {
session.userInput = text;
};
useEffect(() => {
setUserInput(session.userInput);
}, [session]);
// useEffect(() => {
// updateUserInput(userInput);
// }, [userInput]);
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(measure, [userInput]); useEffect(() => {
measure;
updateUserInput(userInput);
}, [userInput]);
// only search prompts when user input is short // only search prompts when user input is short
const SEARCH_TEXT_LIMIT = 30; const SEARCH_TEXT_LIMIT = 30;
@ -586,7 +600,7 @@ export function Chat() {
: [], : [],
) )
.concat( .concat(
userInput.length > 0 && config.sendPreviewBubble userInput && userInput.length > 0 && config.sendPreviewBubble
? [ ? [
{ {
...createMessage({ ...createMessage({

View File

@ -174,6 +174,7 @@ export interface ChatSession {
stat: ChatStat; stat: ChatStat;
lastUpdate: string; lastUpdate: string;
lastSummarizeIndex: number; lastSummarizeIndex: number;
userInput: string;
} }
const DEFAULT_TOPIC = Locale.Store.DefaultTopic; const DEFAULT_TOPIC = Locale.Store.DefaultTopic;
@ -199,6 +200,7 @@ function createEmptySession(): ChatSession {
}, },
lastUpdate: createDate, lastUpdate: createDate,
lastSummarizeIndex: 0, lastSummarizeIndex: 0,
userInput: "",
}; };
} }