Skip to content

Commit 5b86355

Browse files
authored
fix: demo conversations now show without API connection (#57)
* fix: demo conversations now show without API connection - Separated demo and API conversation handling - Initialize demo conversations in store on component mount - Fixed dependency issues in useEffect hooks - Improved error handling for API connection failures - Added better logging for conversation initialization This fixes the issue where demo conversations wouldn't show unless connected to an API server. * fix: address comments
1 parent c9f52cc commit 5b86355

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

src/components/Conversations.tsx

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { toConversationItems } from '@/utils/conversation';
1111
import { demoConversations, type DemoConversation } from '@/democonversations';
1212
import { useSearchParams, useNavigate } from 'react-router-dom';
1313
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';
1519

1620
interface Props {
1721
className?: string;
@@ -27,15 +31,26 @@ const Conversations: FC<Props> = ({ route }) => {
2731
const { api, isConnected$, connectionConfig } = useApi();
2832
const queryClient = useQueryClient();
2933
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
3237
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
3348
if (conversationParam) {
3449
selectedConversation$.set(conversationParam);
3550
}
3651
}, [conversationParam]);
3752

38-
// Fetch conversations from API with proper caching
53+
// Fetch conversations from API
3954
const {
4055
data: apiConversations = [],
4156
isError,
@@ -45,11 +60,6 @@ const Conversations: FC<Props> = ({ route }) => {
4560
} = useQuery({
4661
queryKey: ['conversations', connectionConfig.baseUrl, isConnected],
4762
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-
}
5363
try {
5464
const conversations = await api.getConversations();
5565
console.log('Fetched conversations:', conversations);
@@ -60,7 +70,7 @@ const Conversations: FC<Props> = ({ route }) => {
6070
}
6171
},
6272
enabled: isConnected,
63-
staleTime: 0, // Always refetch when query is invalidated
73+
staleTime: 0,
6474
gcTime: 5 * 60 * 1000,
6575
refetchOnWindowFocus: false,
6676
});
@@ -70,30 +80,44 @@ const Conversations: FC<Props> = ({ route }) => {
7080
console.error('Conversation query error:', error);
7181
}
7282

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');
78105
void initializeConversations(
79106
api,
80107
apiConversations.map((c) => c.name),
81108
10
82109
);
83110
}
111+
}, [isConnected, apiConversations, api]);
84112

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]);
97121

98122
const handleSelectConversation = useCallback(
99123
(id: string) => {
@@ -112,16 +136,15 @@ const Conversations: FC<Props> = ({ route }) => {
112136
[queryClient, navigate, route]
113137
);
114138

115-
const conversation$ = useObservable<ConversationItem | undefined>(undefined);
116-
117-
// Update conversation$ when selectedConversation$ changes
139+
// Update conversation$ when selected conversation changes
118140
useObserveEffect(selectedConversation$, ({ value: selectedConversation }) => {
119141
conversation$.set(allConversations.find((conv) => conv.name === selectedConversation));
120142
});
121143

122-
// Update conversation$ when allConversations changes
144+
// Update conversation$ when available conversations change
123145
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));
125148
// eslint-disable-next-line react-hooks/exhaustive-deps
126149
}, [allConversations]);
127150

src/stores/conversations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function updateConversationData(id: string, data: ConversationResponse) {
8787
conversations$.get(id)?.data.set(data);
8888
}
8989

90-
// Bulk initialize conversations with their data
90+
// Initialize conversations with their data
9191
export async function initializeConversations(
9292
api: { getConversation: (id: string) => Promise<ConversationResponse> },
9393
conversationIds: string[],

0 commit comments

Comments
 (0)