Skip to content

Commit b4c84c8

Browse files
committed
wip
1 parent e9567ae commit b4c84c8

File tree

4 files changed

+77
-114
lines changed

4 files changed

+77
-114
lines changed

features/conversation-list/conversation-list.screen.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export function ConversationListScreen(props: IConversationListProps) {
5555
}
5656
}, [refetchConversations]);
5757

58+
console.log("conversations:", conversations);
59+
5860
return (
5961
<Screen contentContainerStyle={$globalStyles.flex1}>
6062
<AnimatedVStack layout={theme.animation.reanimatedLayoutSpringTransition}>

features/conversation-list/use-conversation-list-conversations.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import { useEffect, useMemo } from "react";
1212
export const useConversationListConversations = () => {
1313
const currentAccount = useCurrentAccount();
1414

15-
const {
16-
data: conversations,
17-
isLoading,
18-
refetch,
19-
} = useQuery(
15+
const { data: conversations, refetch } = useQuery(
2016
getConversationsQueryOptions({
2117
account: currentAccount!,
2218
})
@@ -96,5 +92,10 @@ export const useConversationListConversations = () => {
9692
*/
9793
}, [conversations, conversationsMetadataQueries]);
9894

99-
return { data: filteredAndSortedConversations, isLoading, refetch };
95+
console.log(
96+
"filteredAndSortedConversations:",
97+
filteredAndSortedConversations
98+
);
99+
100+
return { data: filteredAndSortedConversations, refetch };
100101
};

features/conversation-requests-list/use-conversation-requests-list-items.tsx

Lines changed: 62 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,71 @@ import { getV3SpamScore } from "@/data/helpers/conversations/spamScore";
22
import { useCurrentAccount } from "@/data/store/accountsStore";
33
import { getMessageStringContent } from "@/features/conversation/conversation-message/conversation-message.utils";
44
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";
76
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";
108

11-
// TODO: Put in react query
12-
13-
export const useConversationRequestsListItem = () => {
9+
export function useConversationRequestsListItem() {
1410
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-
);
2411

25-
const { data: conversations, isLoading: isLoadingConversations } = useQuery(
26-
getUnknownConsentConversationsQueryOptions({
12+
const {
13+
data: unkownConsentConversations,
14+
isLoading: unkownConsentConversationsLoading,
15+
} = useQuery({
16+
...getUnknownConsentConversationsQueryOptions({
2717
account: currentAccount!,
2818
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+
}

queries/unknown-consent-conversations-query.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import { UseQueryOptions } from "@tanstack/react-query";
77

88
function selectUnknownConsentConversations(conversations: IConversationsQuery) {
99
return (
10-
conversations?.filter((conversation) =>
11-
// Only the unknown conversations
12-
isConversationConsentUnknown(conversation)
10+
conversations?.filter(
11+
(conversation) =>
12+
// Only the unknown conversations
13+
isConversationConsentUnknown(conversation) &&
14+
// Only the conversations with last message
15+
conversation.lastMessage
1316
) ?? []
1417
);
1518
}

0 commit comments

Comments
 (0)