fix(ui): persistent chat history using session storage (#1442)
This commit is contained in:
Generated
+15
-1
@@ -43,7 +43,8 @@
|
||||
"react-toastify": "^8.0.0",
|
||||
"tailwind-merge": "^2.5.4",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
"unist-util-visit": "^5.0.0"
|
||||
"unist-util-visit": "^5.0.0",
|
||||
"uuid": "^11.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@electron-forge/cli": "^7.5.0",
|
||||
@@ -16402,6 +16403,19 @@
|
||||
"node": ">= 0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/uuid": {
|
||||
"version": "11.1.0",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.0.tgz",
|
||||
"integrity": "sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==",
|
||||
"funding": [
|
||||
"https://github.com/sponsors/broofa",
|
||||
"https://github.com/sponsors/ctavan"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"uuid": "dist/esm/bin/uuid"
|
||||
}
|
||||
},
|
||||
"node_modules/validate-npm-package-license": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz",
|
||||
|
||||
@@ -99,6 +99,7 @@
|
||||
"react-toastify": "^8.0.0",
|
||||
"tailwind-merge": "^2.5.4",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
"unist-util-visit": "^5.0.0"
|
||||
"unist-util-visit": "^5.0.0",
|
||||
"uuid": "^11.1.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { getApiUrl } from '../config';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import BottomMenu from './BottomMenu';
|
||||
import FlappyGoose from './FlappyGoose';
|
||||
import GooseMessage from './GooseMessage';
|
||||
@@ -23,9 +24,33 @@ export interface ChatType {
|
||||
}
|
||||
|
||||
export default function ChatView({ setView }: { setView: (view: View) => void }) {
|
||||
// Generate or retrieve a unique window ID
|
||||
const [windowId] = useState(() => {
|
||||
// Check if we already have a window ID in sessionStorage
|
||||
const existingId = window.sessionStorage.getItem('goose-window-id');
|
||||
if (existingId) {
|
||||
return existingId;
|
||||
}
|
||||
// Create a new ID if none exists
|
||||
const newId = uuidv4();
|
||||
window.sessionStorage.setItem('goose-window-id', newId);
|
||||
return newId;
|
||||
});
|
||||
|
||||
const [chat, setChat] = useState<ChatType>(() => {
|
||||
// Try to load saved chat from sessionStorage
|
||||
const savedChat = window.sessionStorage.getItem(`goose-chat-${windowId}`);
|
||||
if (savedChat) {
|
||||
try {
|
||||
return JSON.parse(savedChat);
|
||||
} catch (e) {
|
||||
console.error('Failed to parse saved chat:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Return default chat if no saved chat exists
|
||||
return {
|
||||
id: 1,
|
||||
id: Date.now(),
|
||||
title: 'Chat 1',
|
||||
messages: [],
|
||||
};
|
||||
@@ -75,10 +100,19 @@ export default function ChatView({ setView }: { setView: (view: View) => void })
|
||||
},
|
||||
});
|
||||
|
||||
// Update chat messages when they change
|
||||
// Update chat messages when they change and save to sessionStorage
|
||||
useEffect(() => {
|
||||
setChat((prevChat) => ({ ...prevChat, messages }));
|
||||
}, [messages]);
|
||||
setChat((prevChat) => {
|
||||
const updatedChat = { ...prevChat, messages };
|
||||
// Save to sessionStorage
|
||||
try {
|
||||
window.sessionStorage.setItem(`goose-chat-${windowId}`, JSON.stringify(updatedChat));
|
||||
} catch (e) {
|
||||
console.error('Failed to save chat to sessionStorage:', e);
|
||||
}
|
||||
return updatedChat;
|
||||
});
|
||||
}, [messages, windowId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (messages.length > 0) {
|
||||
|
||||
Reference in New Issue
Block a user