@@ -11,7 +11,11 @@ import { toConversationItems } from '@/utils/conversation';
11
11
import { demoConversations , type DemoConversation } from '@/democonversations' ;
12
12
import { useSearchParams , useNavigate } from 'react-router-dom' ;
13
13
import { Memo , use$ , useObservable , useObserveEffect } from '@legendapp/state/react' ;
14
- import { initializeConversations , selectedConversation$ } from '@/stores/conversations' ;
14
+ import {
15
+ initializeConversations ,
16
+ selectedConversation$ ,
17
+ initConversation ,
18
+ } from '@/stores/conversations' ;
15
19
16
20
interface Props {
17
21
className ?: string ;
@@ -27,15 +31,26 @@ const Conversations: FC<Props> = ({ route }) => {
27
31
const { api, isConnected$, connectionConfig } = useApi ( ) ;
28
32
const queryClient = useQueryClient ( ) ;
29
33
const isConnected = use$ ( isConnected$ ) ;
30
-
31
- // Update selected conversation when URL param changes
34
+ const conversation$ = useObservable < ConversationItem | undefined > ( undefined ) ;
35
+ // Track demo initialization
36
+ // Initialize demo conversations and handle selection on mount
32
37
useEffect ( ( ) => {
38
+ // Initialize demos in store
39
+ demoConversations . forEach ( ( conv ) => {
40
+ initConversation ( conv . name , {
41
+ log : conv . messages ,
42
+ logfile : conv . name ,
43
+ branches : { } ,
44
+ } ) ;
45
+ } ) ;
46
+
47
+ // Handle initial conversation selection
33
48
if ( conversationParam ) {
34
49
selectedConversation$ . set ( conversationParam ) ;
35
50
}
36
51
} , [ conversationParam ] ) ;
37
52
38
- // Fetch conversations from API with proper caching
53
+ // Fetch conversations from API
39
54
const {
40
55
data : apiConversations = [ ] ,
41
56
isError,
@@ -45,11 +60,6 @@ const Conversations: FC<Props> = ({ route }) => {
45
60
} = useQuery ( {
46
61
queryKey : [ 'conversations' , connectionConfig . baseUrl , isConnected ] ,
47
62
queryFn : async ( ) => {
48
- console . log ( 'Fetching conversations, connection state:' , isConnected ) ;
49
- if ( ! isConnected ) {
50
- console . warn ( 'Attempting to fetch conversations while disconnected' ) ;
51
- return [ ] ;
52
- }
53
63
try {
54
64
const conversations = await api . getConversations ( ) ;
55
65
console . log ( 'Fetched conversations:' , conversations ) ;
@@ -60,7 +70,7 @@ const Conversations: FC<Props> = ({ route }) => {
60
70
}
61
71
} ,
62
72
enabled : isConnected ,
63
- staleTime : 0 , // Always refetch when query is invalidated
73
+ staleTime : 0 ,
64
74
gcTime : 5 * 60 * 1000 ,
65
75
refetchOnWindowFocus : false ,
66
76
} ) ;
@@ -70,30 +80,44 @@ const Conversations: FC<Props> = ({ route }) => {
70
80
console . error ( 'Conversation query error:' , error ) ;
71
81
}
72
82
73
- // Combine demo and API conversations and initialize store
74
- const allConversations : ConversationItem [ ] = useMemo ( ( ) => {
75
- // Initialize API conversations in store
76
- if ( apiConversations . length ) {
77
- console . log ( '[Conversations] Initializing conversations in store' ) ;
83
+ // Prepare demo conversation items
84
+ const demoItems = useMemo (
85
+ ( ) =>
86
+ demoConversations . map ( ( conv : DemoConversation ) => ( {
87
+ name : conv . name ,
88
+ lastUpdated : conv . lastUpdated ,
89
+ messageCount : conv . messages . length ,
90
+ readonly : true ,
91
+ } ) ) ,
92
+ [ ]
93
+ ) ;
94
+
95
+ // Handle API conversations separately
96
+ const apiItems = useMemo ( ( ) => {
97
+ if ( ! isConnected ) return [ ] ;
98
+ return toConversationItems ( apiConversations ) ;
99
+ } , [ isConnected , apiConversations ] ) ;
100
+
101
+ // Initialize API conversations in store when available
102
+ useEffect ( ( ) => {
103
+ if ( isConnected && apiConversations . length ) {
104
+ console . log ( '[Conversations] Initializing API conversations' ) ;
78
105
void initializeConversations (
79
106
api ,
80
107
apiConversations . map ( ( c ) => c . name ) ,
81
108
10
82
109
) ;
83
110
}
111
+ } , [ isConnected , apiConversations , api ] ) ;
84
112
85
- return [
86
- // Convert demo conversations to ConversationItems
87
- ...demoConversations . map ( ( conv : DemoConversation ) => ( {
88
- name : conv . name ,
89
- lastUpdated : conv . lastUpdated ,
90
- messageCount : conv . messages . length ,
91
- readonly : true ,
92
- } ) ) ,
93
- // Convert API conversations to ConversationItems
94
- ...toConversationItems ( apiConversations ) ,
95
- ] ;
96
- } , [ apiConversations , api ] ) ;
113
+ // Combine demo and API conversations
114
+ const allConversations = useMemo ( ( ) => {
115
+ console . log ( '[Conversations] Combining conversations' , {
116
+ demoCount : demoItems . length ,
117
+ apiCount : apiItems . length ,
118
+ } ) ;
119
+ return [ ...demoItems , ...apiItems ] ;
120
+ } , [ demoItems , apiItems ] ) ;
97
121
98
122
const handleSelectConversation = useCallback (
99
123
( id : string ) => {
@@ -112,16 +136,15 @@ const Conversations: FC<Props> = ({ route }) => {
112
136
[ queryClient , navigate , route ]
113
137
) ;
114
138
115
- const conversation$ = useObservable < ConversationItem | undefined > ( undefined ) ;
116
-
117
- // Update conversation$ when selectedConversation$ changes
139
+ // Update conversation$ when selected conversation changes
118
140
useObserveEffect ( selectedConversation$ , ( { value : selectedConversation } ) => {
119
141
conversation$ . set ( allConversations . find ( ( conv ) => conv . name === selectedConversation ) ) ;
120
142
} ) ;
121
143
122
- // Update conversation$ when allConversations changes
144
+ // Update conversation$ when available conversations change
123
145
useEffect ( ( ) => {
124
- conversation$ . set ( allConversations . find ( ( conv ) => conv . name === selectedConversation$ . get ( ) ) ) ;
146
+ const selected = selectedConversation$ . get ( ) ;
147
+ conversation$ . set ( allConversations . find ( ( conv ) => conv . name === selected ) ) ;
125
148
// eslint-disable-next-line react-hooks/exhaustive-deps
126
149
} , [ allConversations ] ) ;
127
150
0 commit comments