@@ -2,114 +2,71 @@ import { getV3SpamScore } from "@/data/helpers/conversations/spamScore";
2
2
import { useCurrentAccount } from "@/data/store/accountsStore" ;
3
3
import { getMessageStringContent } from "@/features/conversation/conversation-message/conversation-message.utils" ;
4
4
import { getUnknownConsentConversationsQueryOptions } from "@/queries/unknown-consent-conversations-query" ;
5
- import { captureError , captureErrorWithToast } from "@/utils/capture-error" ;
6
- import { ConversationWithCodecsType } from "@/utils/xmtpRN/client.types" ;
5
+ import { captureError } from "@/utils/capture-error" ;
7
6
import { getMessageContentType } from "@/utils/xmtpRN/content-types/content-types" ;
8
- import { useQuery } from "@tanstack/react-query" ;
9
- import { useEffect , useState } from "react" ;
7
+ import { useQueries , useQuery } from "@tanstack/react-query" ;
10
8
11
- // TODO: Put in react query
12
-
13
- export const useConversationRequestsListItem = ( ) => {
9
+ export function useConversationRequestsListItem ( ) {
14
10
const currentAccount = useCurrentAccount ( ) ;
15
- const [ isProcessingConversations , setIsProcessingConversations ] =
16
- useState ( false ) ;
17
-
18
- const [ likelyNotSpam , setLikelyNotSpam ] = useState <
19
- ConversationWithCodecsType [ ]
20
- > ( [ ] ) ;
21
- const [ likelySpam , setLikelySpam ] = useState < ConversationWithCodecsType [ ] > (
22
- [ ]
23
- ) ;
24
11
25
- const { data : conversations , isLoading : isLoadingConversations } = useQuery (
26
- getUnknownConsentConversationsQueryOptions ( {
12
+ const {
13
+ data : unkownConsentConversations ,
14
+ isLoading : unkownConsentConversationsLoading ,
15
+ } = useQuery ( {
16
+ ...getUnknownConsentConversationsQueryOptions ( {
27
17
account : currentAccount ! ,
28
18
context : "useConversationRequestsListItem" ,
29
- } )
30
- ) ;
31
-
32
- const numberOfConversations = conversations ?. length ;
33
-
34
- useEffect ( ( ) => {
35
- async function processConversations (
36
- conversations : ConversationWithCodecsType [ ]
37
- ) {
38
- try {
39
- setIsProcessingConversations ( true ) ;
40
- const isSpamResults = await Promise . allSettled (
41
- conversations . map ( async ( conversation ) => {
42
- try {
43
- const lastMessage = conversation . lastMessage ;
44
-
45
- if ( ! lastMessage ) {
46
- return true ;
47
- }
48
-
49
- const messageText = getMessageStringContent ( lastMessage ) ;
50
-
51
- if ( ! messageText ) {
52
- return true ;
53
- }
54
-
55
- // Get spam score
56
- const contentType = getMessageContentType (
57
- lastMessage . contentTypeId !
58
- ) ;
59
-
60
- if ( ! contentType ) {
61
- return true ;
62
- }
63
-
64
- const spamScore = await getV3SpamScore ( {
65
- messageText : messageText ,
66
- contentType,
67
- } ) ;
68
-
69
- if ( spamScore === 0 ) {
70
- return false ;
71
- }
72
-
73
- return true ;
74
- } catch ( error ) {
75
- captureError ( error ) ;
76
- return true ;
77
- }
78
- } )
79
- ) ;
80
-
81
- const notSpams : ConversationWithCodecsType [ ] = [ ] ;
82
- const spams : ConversationWithCodecsType [ ] = [ ] ;
83
-
84
- isSpamResults . forEach ( ( result , index ) => {
85
- if ( result . status !== "fulfilled" ) {
86
- return ;
87
- }
88
- const conversation = conversations [ index ] ;
89
- const isSpam = result . value ;
90
- if ( isSpam ) {
91
- spams . push ( conversation ) ;
92
- } else {
93
- notSpams . push ( conversation ) ;
94
- }
95
- } ) ;
96
-
97
- setLikelyNotSpam ( notSpams ) ;
98
- setLikelySpam ( spams ) ;
99
- } catch ( error ) {
100
- captureErrorWithToast ( error ) ;
101
- } finally {
102
- // Default to putting all conversations in likelySpam if we have an error
103
- setLikelySpam ( conversations ) ;
104
- setIsProcessingConversations ( false ) ;
105
- }
106
- }
107
-
108
- processConversations ( conversations ?? [ ] ) ;
109
- // eslint-disable-next-line react-hooks/exhaustive-deps
110
- } , [ numberOfConversations ] ) ;
111
-
112
- const isLoading = isLoadingConversations || isProcessingConversations ;
113
-
114
- return { likelyNotSpam, likelySpam, isLoading } ;
115
- } ;
19
+ } ) ,
20
+ } ) ;
21
+
22
+ const spamQueries = useQueries ( {
23
+ queries : ( unkownConsentConversations ?? [ ] ) . map ( ( conversation ) => ( {
24
+ queryKey : [ "is-spam" , conversation . topic ] ,
25
+ queryFn : async ( ) => {
26
+ const lastMessage = conversation . lastMessage ;
27
+
28
+ if ( ! lastMessage ) {
29
+ return true ;
30
+ }
31
+
32
+ const messageText = getMessageStringContent ( lastMessage ) ;
33
+
34
+ if ( ! messageText ) {
35
+ return true ;
36
+ }
37
+
38
+ const contentType = getMessageContentType ( lastMessage . contentTypeId ! ) ;
39
+ if ( ! contentType ) {
40
+ return true ;
41
+ }
42
+
43
+ try {
44
+ const spamScore = await getV3SpamScore ( {
45
+ messageText,
46
+ contentType,
47
+ } ) ;
48
+ return spamScore !== 0 ;
49
+ } catch ( error ) {
50
+ captureError ( error ) ;
51
+ return true ;
52
+ }
53
+ } ,
54
+ enabled : ! ! conversation . lastMessage ,
55
+ } ) ) ,
56
+ } ) ;
57
+
58
+ const isLoading =
59
+ unkownConsentConversationsLoading || spamQueries . some ( ( q ) => q . isLoading ) ;
60
+ const spamResults = spamQueries . map ( ( q , i ) => ( {
61
+ conversation : unkownConsentConversations ?. [ i ] ,
62
+ isSpam : q . data ?? true ,
63
+ } ) ) ;
64
+
65
+ return {
66
+ likelyNotSpam :
67
+ spamResults . filter ( ( r ) => ! r . isSpam ) . map ( ( r ) => r . conversation ) ?? [ ] ,
68
+ likelySpam :
69
+ spamResults . filter ( ( r ) => r . isSpam ) . map ( ( r ) => r . conversation ) ?? [ ] ,
70
+ isLoading,
71
+ } ;
72
+ }
0 commit comments