Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 116x 7084x 7084x 7084x 7084x 7084x | import { QKEY_CHALLENGE_THREAD } from "@constants/queryKeys";
import { getGlobalTopic } from "@features/Minibuilders/Conversations/hooks/useConversationsModal";
import { TGlobalTopic } from "@features/Minibuilders/Conversations/types";
import { customInstance } from "@services/api";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useQueryClient } from "react-query";
export const useConversationsInformations = ({
merchantID,
}: {
merchantID: number;
}) => {
const queryClient = useQueryClient();
const { isNewApprovalFlowEnabled } = useGetFeatureFlagValues();
const fetchGlobalTopic = async (topicName?: string, topicType?: string) => {
try {
const topicDataResult = await queryClient.fetchQuery<{
total: number;
data: TGlobalTopic[];
}>(
["topic", merchantID, topicName],
() =>
getGlobalTopic({
topicName,
merchantId: merchantID,
}),
{
staleTime: Infinity,
},
);
const topicID =
topicDataResult?.total &&
topicDataResult?.data?.find(
(item: any) =>
item.Name === topicName &&
(topicType ? item.Type === topicType : true),
)?.ID;
if (!topicID) {
console.error("Could not fetch or determine topic ID");
return;
}
return { topicDataResult, topicID };
} catch {
console.error("Error fetching global topic");
}
};
const fetchExistingThreads = async (
challengeID?: number,
topicName?: string,
topicType?: string,
) => {
let fetchedTopicID: number | undefined;
let fetchedThreadsData: { data: any[] } | undefined;
try {
const result = await fetchGlobalTopic(topicName, topicType);
if (!result) return;
const { topicID } = result;
if (!topicID) {
return;
}
fetchedTopicID = topicID;
const threadsFetcher = async () => {
const response = await customInstance({
url: `/merchants/${merchantID}/topics/${fetchedTopicID}/threads?sort=createdAt`,
method: "GET",
});
return { data: response?.data };
};
fetchedThreadsData = await queryClient.fetchQuery(
[QKEY_CHALLENGE_THREAD, merchantID, fetchedTopicID],
threadsFetcher,
{
staleTime: Infinity,
},
);
} catch (error) {
console.error("Error fetching notification data:", error);
return;
}
const existingThread = fetchedThreadsData?.data?.find((thread: any) => {
if (isNewApprovalFlowEnabled)
return thread.task?.id && thread.task?.id === challengeID;
return thread.challenge?.id && thread.challenge?.id === challengeID;
});
return { existingThread, fetchedTopicID, fetchedThreadsData };
};
return { fetchExistingThreads, fetchGlobalTopic };
};
|