fix chatstore state

This commit is contained in:
2023-03-17 10:42:16 +08:00
parent f59b63884d
commit 14457cbb5f

View File

@@ -74,14 +74,20 @@ export function App() {
return JSON.parse(val) as ChatStore; return JSON.parse(val) as ChatStore;
}; };
const chatStore = getChatStoreByIndex(selectedChatIndex); const [chatStore, _setChatStore] = useState(
getChatStoreByIndex(selectedChatIndex)
);
const setChatStore = (cs: ChatStore) => { const setChatStore = (cs: ChatStore) => {
console.log("saved chat", selectedChatIndex, chatStore); console.log("saved chat", selectedChatIndex, chatStore);
localStorage.setItem( localStorage.setItem(
`${STORAGE_NAME}-${selectedChatIndex}`, `${STORAGE_NAME}-${selectedChatIndex}`,
JSON.stringify(cs) JSON.stringify(cs)
); );
_setChatStore(cs);
}; };
useEffect(() => {
_setChatStore(getChatStoreByIndex(selectedChatIndex));
}, [selectedChatIndex]);
return ( return (
<div className="flex text-sm h-screen bg-slate-200 dark:bg-slate-800 dark:text-white"> <div className="flex text-sm h-screen bg-slate-200 dark:bg-slate-800 dark:text-white">