@@ -10,6 +10,27 @@ import { useThreadContext, MyMessage, ThreadProvider } from "./context/ThreadCon
10
10
import { Thread } from "./assistant-ui/thread" ;
11
11
import { ThreadList } from "./assistant-ui/thread-list" ;
12
12
import { chatStorage , ThreadData as StoredThreadData } from "../utils/chatStorage" ;
13
+ import { useChatStorage } from "../hooks/useChatStorage" ;
14
+ import styled from "styled-components" ;
15
+
16
+
17
+
18
+
19
+ const ChatContainer = styled . div `
20
+ display: flex;
21
+ height: 500px;
22
+
23
+ .aui-thread-list-root {
24
+ width: 250px;
25
+ background-color: #333;
26
+ }
27
+
28
+ .aui-thread-root {
29
+ flex: 1;
30
+ background-color: #f0f0f0;
31
+ }
32
+
33
+ ` ;
13
34
14
35
// Define thread data interfaces to match ExternalStoreThreadData requirements
15
36
interface RegularThreadData {
@@ -45,109 +66,13 @@ function ChatWithThreads() {
45
66
const [ threadList , setThreadList ] = useState < ThreadData [ ] > ( [
46
67
{ threadId : "default" , status : "regular" , title : "New Chat" } as RegularThreadData ,
47
68
] ) ;
48
- const [ isInitialized , setIsInitialized ] = useState ( false ) ;
49
-
50
- // Load data from persistent storage on component mount
51
- useEffect ( ( ) => {
52
- const loadData = async ( ) => {
53
- try {
54
- await chatStorage . initialize ( ) ;
55
-
56
- // Load all threads from storage
57
- const storedThreads = await chatStorage . getAllThreads ( ) ;
58
- if ( storedThreads . length > 0 ) {
59
- // Convert stored threads to UI format
60
- const uiThreads : ThreadData [ ] = storedThreads . map ( stored => ( {
61
- threadId : stored . threadId ,
62
- status : stored . status as "regular" | "archived" ,
63
- title : stored . title ,
64
- } ) ) ;
65
- setThreadList ( uiThreads ) ;
66
-
67
- // Load messages for each thread
68
- const threadMessages = new Map < string , MyMessage [ ] > ( ) ;
69
- for ( const thread of storedThreads ) {
70
- const messages = await chatStorage . getMessages ( thread . threadId ) ;
71
- threadMessages . set ( thread . threadId , messages ) ;
72
- }
73
-
74
- // Ensure default thread exists
75
- if ( ! threadMessages . has ( "default" ) ) {
76
- threadMessages . set ( "default" , [ ] ) ;
77
- }
78
-
79
- setThreads ( threadMessages ) ;
80
-
81
- // Set current thread to the most recently updated one
82
- const latestThread = storedThreads . sort ( ( a , b ) => b . updatedAt - a . updatedAt ) [ 0 ] ;
83
- if ( latestThread ) {
84
- setCurrentThreadId ( latestThread . threadId ) ;
85
- }
86
- } else {
87
- // Initialize with default thread
88
- const defaultThread : StoredThreadData = {
89
- threadId : "default" ,
90
- status : "regular" ,
91
- title : "New Chat" ,
92
- createdAt : Date . now ( ) ,
93
- updatedAt : Date . now ( ) ,
94
- } ;
95
- await chatStorage . saveThread ( defaultThread ) ;
96
- }
97
-
98
- setIsInitialized ( true ) ;
99
- } catch ( error ) {
100
- console . error ( "Failed to load chat data:" , error ) ;
101
- setIsInitialized ( true ) ; // Continue with default state
102
- }
103
- } ;
104
-
105
- loadData ( ) ;
106
- } , [ setCurrentThreadId , setThreads ] ) ;
107
-
108
- // Save thread data whenever threadList changes
109
- useEffect ( ( ) => {
110
- if ( ! isInitialized ) return ;
111
-
112
- const saveThreads = async ( ) => {
113
- try {
114
- for ( const thread of threadList ) {
115
- const storedThread : StoredThreadData = {
116
- threadId : thread . threadId ,
117
- status : thread . status ,
118
- title : thread . title ,
119
- createdAt : Date . now ( ) , // In real app, preserve original createdAt
120
- updatedAt : Date . now ( ) ,
121
- } ;
122
- await chatStorage . saveThread ( storedThread ) ;
123
- }
124
- } catch ( error ) {
125
- console . error ( "Failed to save threads:" , error ) ;
126
- }
127
- } ;
128
-
129
- saveThreads ( ) ;
130
- } , [ threadList , isInitialized ] ) ;
131
-
132
- // Save messages whenever threads change
133
- useEffect ( ( ) => {
134
- if ( ! isInitialized ) return ;
135
-
136
- const saveMessages = async ( ) => {
137
- try {
138
- for ( const [ threadId , messages ] of threads . entries ( ) ) {
139
- await chatStorage . saveMessages ( messages , threadId ) ;
140
- }
141
- } catch ( error ) {
142
- console . error ( "Failed to save messages:" , error ) ;
143
- }
144
- } ;
145
-
146
- saveMessages ( ) ;
147
- } , [ threads , isInitialized ] ) ;
148
-
149
-
150
-
69
+ const { isInitialized } = useChatStorage ( {
70
+ threadList,
71
+ threads,
72
+ setThreadList,
73
+ setThreads,
74
+ setCurrentThreadId,
75
+ } ) ;
151
76
// Get messages for current thread
152
77
const currentMessages = threads . get ( currentThreadId ) || [ ] ;
153
78
@@ -349,10 +274,16 @@ function ChatWithThreads() {
349
274
} ,
350
275
} ) ;
351
276
277
+ if ( ! isInitialized ) {
278
+ return < div > Loading...</ div > ;
279
+ }
280
+
352
281
return (
353
282
< AssistantRuntimeProvider runtime = { runtime } >
354
- < ThreadList />
355
- < Thread />
283
+ < ChatContainer >
284
+ < ThreadList />
285
+ < Thread />
286
+ </ ChatContainer >
356
287
</ AssistantRuntimeProvider >
357
288
) ;
358
289
}
0 commit comments