@@ -11,68 +11,9 @@ import {
1111 ActivityIndicator ,
1212} from "react-native" ;
1313import { useLocalSearchParams } from "expo-router" ;
14- import { useQuery , useMutation } from "convex/react" ;
15- import { makeFunctionReference } from "convex/server" ;
1614import { useAuth } from "../../../src/contexts/AuthContext" ;
17- import type { Id } from "@opencom/convex/dataModel" ;
18-
19- interface Message {
20- _id : string ;
21- content : string ;
22- senderType : "user" | "visitor" | "agent" | "bot" ;
23- createdAt : number ;
24- }
25-
26- type ConversationRecord = {
27- _id : Id < "conversations" > ;
28- visitorId ?: Id < "visitors" > ;
29- status : "open" | "closed" | "snoozed" ;
30- } ;
31-
32- type VisitorRecord = {
33- _id : Id < "visitors" > ;
34- name ?: string ;
35- email ?: string ;
36- readableId ?: string ;
37- location ?: { city ?: string ; country ?: string } ;
38- device ?: { browser ?: string ; os ?: string } ;
39- } ;
40-
41- const conversationGetQueryRef = makeFunctionReference <
42- "query" ,
43- { id : Id < "conversations" > } ,
44- ConversationRecord | null
45- > ( "conversations:get" ) ;
46-
47- const visitorGetQueryRef = makeFunctionReference <
48- "query" ,
49- { id : Id < "visitors" > } ,
50- VisitorRecord | null
51- > ( "visitors:get" ) ;
52-
53- const messagesListQueryRef = makeFunctionReference <
54- "query" ,
55- { conversationId : Id < "conversations" > } ,
56- Message [ ]
57- > ( "messages:list" ) ;
58-
59- const sendMessageMutationRef = makeFunctionReference <
60- "mutation" ,
61- { conversationId : Id < "conversations" > ; senderId : string ; senderType : "agent" ; content : string } ,
62- Id < "messages" >
63- > ( "messages:send" ) ;
64-
65- const updateConversationStatusMutationRef = makeFunctionReference <
66- "mutation" ,
67- { id : Id < "conversations" > ; status : "open" | "closed" | "snoozed" } ,
68- null
69- > ( "conversations:updateStatus" ) ;
70-
71- const markConversationReadMutationRef = makeFunctionReference <
72- "mutation" ,
73- { id : Id < "conversations" > ; readerType : "agent" | "visitor" } ,
74- null
75- > ( "conversations:markAsRead" ) ;
15+ import { useConversationConvex } from "../../../src/hooks/convex/useConversationConvex" ;
16+ import type { MobileConversationMessage as Message } from "../../../src/hooks/convex/types" ;
7617
7718function formatTime ( timestamp : number ) : string {
7819 return new Date ( timestamp ) . toLocaleTimeString ( [ ] , {
@@ -86,32 +27,22 @@ export default function ConversationScreen() {
8627 const { user } = useAuth ( ) ;
8728 const [ inputText , setInputText ] = useState ( "" ) ;
8829 const flatListRef = useRef < FlatList > ( null ) ;
89-
90- const conversation = useQuery (
91- conversationGetQueryRef ,
92- id ? { id : id as Id < "conversations" > } : "skip"
93- ) as ConversationRecord | null | undefined ;
94-
95- const visitor = useQuery (
96- visitorGetQueryRef ,
97- conversation ?. visitorId ? { id : conversation . visitorId } : "skip"
98- ) as VisitorRecord | null | undefined ;
99-
100- const messages = useQuery (
101- messagesListQueryRef ,
102- id ? { conversationId : id as Id < "conversations" > } : "skip"
103- ) as Message [ ] | undefined ;
104-
105- const sendMessage = useMutation ( sendMessageMutationRef ) ;
106- const updateStatus = useMutation ( updateConversationStatusMutationRef ) ;
107- const markAsRead = useMutation ( markConversationReadMutationRef ) ;
30+ const {
31+ resolvedConversationId,
32+ conversation,
33+ visitor,
34+ messages,
35+ sendMessage,
36+ updateConversationStatus : updateStatus ,
37+ markConversationRead : markAsRead ,
38+ } = useConversationConvex ( id ) ;
10839
10940 // Mark conversation as read when viewing
11041 useEffect ( ( ) => {
111- if ( id && conversation ) {
112- markAsRead ( { id : id as Id < "conversations" > , readerType : "agent" } ) . catch ( console . error ) ;
42+ if ( resolvedConversationId && conversation ) {
43+ markAsRead ( { id : resolvedConversationId , readerType : "agent" } ) . catch ( console . error ) ;
11344 }
114- } , [ id , conversation , markAsRead ] ) ;
45+ } , [ conversation , markAsRead , resolvedConversationId ] ) ;
11546
11647 useEffect ( ( ) => {
11748 if ( messages && messages . length > 0 ) {
@@ -122,14 +53,14 @@ export default function ConversationScreen() {
12253 } , [ messages ] ) ;
12354
12455 const handleSend = async ( ) => {
125- if ( ! inputText . trim ( ) || ! id || ! user ) return ;
56+ if ( ! inputText . trim ( ) || ! resolvedConversationId || ! user ) return ;
12657
12758 const content = inputText . trim ( ) ;
12859 setInputText ( "" ) ;
12960
13061 try {
13162 await sendMessage ( {
132- conversationId : id as Id < "conversations" > ,
63+ conversationId : resolvedConversationId ,
13364 senderId : user . _id ,
13465 senderType : "agent" ,
13566 content,
@@ -141,10 +72,10 @@ export default function ConversationScreen() {
14172 } ;
14273
14374 const handleStatusChange = async ( status : "open" | "closed" | "snoozed" ) => {
144- if ( ! id ) return ;
75+ if ( ! resolvedConversationId ) return ;
14576 try {
14677 await updateStatus ( {
147- id : id as Id < "conversations" > ,
78+ id : resolvedConversationId ,
14879 status,
14980 } ) ;
15081 } catch ( error ) {
0 commit comments