Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/frontend/app/routes/affiliates/hooks/useAffiliateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export interface AffiliateStats {
isRegistered: boolean;
}

const INVITEE_COUNT_POLL_MS = 30_000;
const INVITEE_COUNT_POLL_MS = 120_000;

// Hook for affiliate audience check
export function useAffiliateAudience(userIdentifier: string, enabled = true) {
Expand Down Expand Up @@ -232,6 +232,10 @@ export function useAffiliateInviteeCount(
const inFlightRef = useRef(false);
const hasLoadedOnceRef = useRef(false);

// Import and use useAffiliateCode hook to fetch the affiliate's actual referral code
const { data: affiliateCodeData, isLoading: isAffiliateCodeLoading } =
useAffiliateCode(walletAddress, enabled);

const fetchData = useCallback(async () => {
if (!enabled || !walletAddress) return;
if (inFlightRef.current) return;
Expand All @@ -243,17 +247,17 @@ export function useAffiliateInviteeCount(
setError(null);

try {
const referralCodes =
await fetchAffiliateReferralCodes(walletAddress);
// Use the affiliate code (affiliateCodeData?.code) instead of fetching from separate referral codes endpoint
const affiliateCode = affiliateCodeData?.code;

if (referralCodes.length === 0) {
if (!affiliateCode) {
setData(0);
return;
}

const count = await fetchAttributedReferralCount({
referralKind: 1,
referralIdTexts: referralCodes,
referralIdTexts: [affiliateCode],
});

setData(count ?? 0);
Expand All @@ -268,10 +272,10 @@ export function useAffiliateInviteeCount(
hasLoadedOnceRef.current = true;
setIsLoading(false);
}
}, [enabled, walletAddress]);
}, [enabled, walletAddress, affiliateCodeData]);

useEffect(() => {
if (!enabled || !walletAddress) return;
if (!enabled || !walletAddress || isAffiliateCodeLoading) return;

void fetchData();
const intervalId = window.setInterval(() => {
Expand All @@ -281,7 +285,7 @@ export function useAffiliateInviteeCount(
return () => {
window.clearInterval(intervalId);
};
}, [enabled, fetchData, walletAddress]);
}, [enabled, fetchData, walletAddress, isAffiliateCodeLoading]);

useEffect(() => {
window.addEventListener('affiliateDataUpdate', fetchData);
Expand Down
Loading