From 014c8e88e18e8a197c8bf42475db305b4909a3a2 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Wed, 15 Mar 2023 02:53:32 +0800 Subject: [PATCH] remember selected index --- src/app.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/app.tsx b/src/app.tsx index 893296f..cd8fcd3 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -63,6 +63,7 @@ export const newChatStore = ( }; const STORAGE_NAME = "chatgpt-api-web"; +const STORAGE_NAME_SELECTED = `${STORAGE_NAME}-selected`; export function App() { // init all chat store @@ -75,7 +76,12 @@ export function App() { } const [allChatStore, setAllChatStore] = useState(initAllChatStore); - const [selectedChatIndex, setSelectedChatIndex] = useState(0); + const [selectedChatIndex, setSelectedChatIndex] = useState( + parseInt(localStorage.getItem(STORAGE_NAME_SELECTED) ?? "0") + ); + useEffect(() => { + localStorage.setItem(STORAGE_NAME_SELECTED, `${selectedChatIndex}`); + }, [selectedChatIndex]); const chatStore = allChatStore[selectedChatIndex]; const setChatStore = (cs: ChatStore) => {