forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseConversation.ts
More file actions
71 lines (59 loc) · 1.67 KB
/
Copy pathuseConversation.ts
File metadata and controls
71 lines (59 loc) · 1.67 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
'use client';
import { useState, useEffect, useCallback } from 'react';
import { getConversation } from '@/lib/api';
import type { Conversation } from '@/types/conversation';
interface UseConversationOptions {
enabled?: boolean;
}
interface UseConversationReturn {
conversation: Conversation | null;
loading: boolean;
error: string | null;
refresh: () => Promise<void>;
update: (conversation: Conversation) => void;
}
/**
* Hook to fetch a single conversation by ID
*/
export function useConversation(
id: string | null,
options: UseConversationOptions = {}
): UseConversationReturn {
const { enabled = true } = options;
const [conversation, setConversation] = useState<Conversation | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchConversation = useCallback(async () => {
if (!enabled || !id) {
setLoading(false);
return;
}
try {
setLoading(true);
setError(null);
const data = await getConversation(id);
setConversation(data);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load conversation');
console.error('Failed to fetch conversation:', err);
} finally {
setLoading(false);
}
}, [enabled, id]);
useEffect(() => {
fetchConversation();
}, [fetchConversation]);
const refresh = useCallback(async () => {
await fetchConversation();
}, [fetchConversation]);
const update = useCallback((updatedConversation: Conversation) => {
setConversation(updatedConversation);
}, []);
return {
conversation,
loading,
error,
refresh,
update,
};
}