forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseConversations.ts
More file actions
269 lines (228 loc) · 8.6 KB
/
Copy pathuseConversations.ts
File metadata and controls
269 lines (228 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use client';
import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
import { getConversations, GetConversationsParams } from '@/lib/api';
import type { Conversation, GroupedConversations } from '@/types/conversation';
import { formatRelativeDate } from '@/lib/utils';
import {
getCache,
setCache,
onCacheInvalidation,
invalidationPatterns,
CACHE_TTL,
cacheKeys,
} from '@/lib/cache';
// Cache entry structure
interface CacheEntry {
conversations: Conversation[];
offset: number;
hasMore: boolean;
}
function getCacheKey(folderId?: string, startDate?: Date, endDate?: Date): string {
return cacheKeys.conversations(
folderId,
startDate?.toISOString().split('T')[0],
endDate?.toISOString().split('T')[0]
);
}
function getFromCache(key: string): CacheEntry | null {
const cached = getCache<CacheEntry>(key);
return cached ? cached.data : null;
}
function isCacheStale(key: string): boolean {
const cached = getCache<CacheEntry>(key);
return cached ? cached.isStale : true;
}
function setToCache(key: string, conversations: Conversation[], offset: number, hasMore: boolean): void {
setCache<CacheEntry>(key, { conversations, offset, hasMore }, CACHE_TTL.MEDIUM);
}
interface UseConversationsOptions extends GetConversationsParams {
enabled?: boolean;
}
interface UseConversationsReturn {
conversations: Conversation[];
groupedConversations: GroupedConversations;
loading: boolean;
error: string | null;
hasMore: boolean;
loadMore: () => Promise<void>;
refresh: () => Promise<void>;
}
/**
* Hook to fetch and manage conversations
*/
export function useConversations(
options: UseConversationsOptions = {}
): UseConversationsReturn {
const { enabled = true, limit = 50, ...params } = options;
// Get cache key and check for cached data
const cacheKey = getCacheKey(params.folderId, params.startDate, params.endDate);
const cachedEntry = getFromCache(cacheKey);
const [conversations, setConversations] = useState<Conversation[]>(cachedEntry?.conversations || []);
const [loading, setLoading] = useState(!cachedEntry); // Only show loading if no cache
const [error, setError] = useState<string | null>(null);
const [offset, setOffset] = useState(cachedEntry?.offset || 0);
const [hasMore, setHasMore] = useState(cachedEntry?.hasMore ?? true);
const [hasProcessing, setHasProcessing] = useState(false);
// Track previous params to detect changes
const prevStartDate = useRef(params.startDate?.getTime());
const prevEndDate = useRef(params.endDate?.getTime());
const prevFolderId = useRef(params.folderId);
// Track if a fetch is in progress to prevent concurrent fetches
const fetchingRef = useRef(false);
// Group conversations by date - memoized for performance
const groupedConversations = useMemo<GroupedConversations>(() => {
return conversations.reduce(
(groups, conversation) => {
const date = new Date(conversation.started_at || conversation.created_at);
const dateKey = formatRelativeDate(date);
if (!groups[dateKey]) {
groups[dateKey] = [];
}
groups[dateKey].push(conversation);
return groups;
},
{} as GroupedConversations
);
}, [conversations]);
// Fetch conversations
const fetchConversations = useCallback(
async (currentOffset: number, append: boolean = false, backgroundRefresh: boolean = false) => {
if (!enabled) return;
// Prevent concurrent fetches
if (fetchingRef.current) return;
fetchingRef.current = true;
const key = getCacheKey(params.folderId, params.startDate, params.endDate);
try {
// Only show loading spinner if not a background refresh and no cached data
if (!backgroundRefresh) {
setLoading(true);
}
setError(null);
const data = await getConversations({
limit,
offset: currentOffset,
statuses: params.statuses,
includeDiscarded: params.includeDiscarded,
startDate: params.startDate,
endDate: params.endDate,
folderId: params.folderId,
});
// Sort by date descending
const sorted = data.sort((a, b) => {
const dateA = new Date(a.started_at || a.created_at);
const dateB = new Date(b.started_at || b.created_at);
return dateB.getTime() - dateA.getTime();
});
// Use functional update to avoid conversations in dependency array
const hasMoreData = data.length === limit;
setHasMore(hasMoreData);
setConversations(prev => {
const newConversations = append ? [...prev, ...sorted] : sorted;
// Save to cache
setToCache(key, newConversations, currentOffset, hasMoreData);
return newConversations;
});
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load conversations');
console.error('Failed to fetch conversations:', err);
} finally {
setLoading(false);
fetchingRef.current = false;
}
},
// Only depend on primitive values, not objects
[enabled, limit, params.statuses, params.includeDiscarded, params.startDate, params.endDate, params.folderId]
);
// Initial fetch and refetch when filter params change
useEffect(() => {
const startDateChanged = params.startDate?.getTime() !== prevStartDate.current;
const endDateChanged = params.endDate?.getTime() !== prevEndDate.current;
const folderIdChanged = params.folderId !== prevFolderId.current;
// Update refs
prevStartDate.current = params.startDate?.getTime();
prevEndDate.current = params.endDate?.getTime();
prevFolderId.current = params.folderId;
const key = getCacheKey(params.folderId, params.startDate, params.endDate);
const cached = getFromCache(key);
// If filter params changed, check cache for new filter
if (startDateChanged || endDateChanged || folderIdChanged) {
if (cached) {
// Load from cache immediately
setConversations(cached.conversations);
setOffset(cached.offset);
setHasMore(cached.hasMore);
setLoading(false);
// If cache is stale, do background refresh
if (isCacheStale(key)) {
fetchConversations(0, false, true);
}
} else {
// No cache, do normal fetch
setOffset(0);
setHasMore(true);
fetchConversations(0, false);
}
} else if (conversations.length === 0 && !cached) {
// Initial load with no cache
setOffset(0);
setHasMore(true);
fetchConversations(0, false);
} else if (cached && isCacheStale(key)) {
// Have cached data but it's stale, do background refresh
fetchConversations(0, false, true);
}
}, [params.startDate, params.endDate, params.folderId, fetchConversations]);
// Subscribe to cache invalidation - refetch when conversations are modified
useEffect(() => {
const unsubscribe = onCacheInvalidation((pattern) => {
if (pattern === invalidationPatterns.conversations) {
// Cache was invalidated, do a fresh fetch
fetchConversations(0, false, false);
}
});
return unsubscribe;
}, [fetchConversations]);
// Track if any conversations are processing
useEffect(() => {
const processing = conversations.some(c => c.status === 'processing');
setHasProcessing(processing);
}, [conversations]);
// Poll for updates while conversations are processing with exponential backoff
useEffect(() => {
if (!hasProcessing) return;
let pollCount = 0;
let timeoutId: NodeJS.Timeout;
const poll = () => {
fetchConversations(0, false, true); // Background refresh, skip cache
pollCount++;
// Exponential backoff: 5s, 10s, 20s, 30s (max)
const nextInterval = Math.min(5000 * Math.pow(2, Math.floor(pollCount / 3)), 30000);
timeoutId = setTimeout(poll, nextInterval);
};
// Start polling after initial 5s delay
timeoutId = setTimeout(poll, 5000);
return () => clearTimeout(timeoutId);
}, [hasProcessing, fetchConversations]);
// Load more conversations
const loadMore = useCallback(async () => {
if (loading || !hasMore) return;
const newOffset = offset + limit;
setOffset(newOffset);
await fetchConversations(newOffset, true);
}, [loading, hasMore, offset, limit, fetchConversations]);
// Refresh conversations (reset and reload)
const refresh = useCallback(async () => {
setOffset(0);
setHasMore(true);
await fetchConversations(0, false);
}, [fetchConversations]);
return {
conversations,
groupedConversations,
loading,
error,
hasMore,
loadMore,
refresh,
};
}