From 96827303c0b0a230ef9ff5d76e06101448c8969a Mon Sep 17 00:00:00 2001 From: Ariel Date: Fri, 28 Feb 2025 22:50:01 +0800 Subject: [PATCH] fix(ui): persistent chat history using session storage (#1442) --- ui/desktop/package-lock.json | 16 +++++++++- ui/desktop/package.json | 3 +- ui/desktop/src/components/ChatView.tsx | 42 +++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/ui/desktop/package-lock.json b/ui/desktop/package-lock.json index 8c2612af..6bc44999 100644 --- a/ui/desktop/package-lock.json +++ b/ui/desktop/package-lock.json @@ -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", diff --git a/ui/desktop/package.json b/ui/desktop/package.json index c881e1e0..ca8de183 100644 --- a/ui/desktop/package.json +++ b/ui/desktop/package.json @@ -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" } } diff --git a/ui/desktop/src/components/ChatView.tsx b/ui/desktop/src/components/ChatView.tsx index ce837c5b..b997a7da 100644 --- a/ui/desktop/src/components/ChatView.tsx +++ b/ui/desktop/src/components/ChatView.tsx @@ -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(() => { + // 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) {