-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathuseRewardsActivity.tsx
More file actions
113 lines (97 loc) · 3.36 KB
/
Copy pathuseRewardsActivity.tsx
File metadata and controls
113 lines (97 loc) · 3.36 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { useCallback, useRef, useEffect } from 'react';
import type { PreSubmitTransactionResponse } from '@blockscout/points-types';
import config from 'configs/app';
import useApiFetch from 'lib/api/useApiFetch';
import useApiQuery from 'lib/api/useApiQuery';
import { useRewardsContext } from 'lib/contexts/rewards';
import { MINUTE } from 'toolkit/utils/consts';
import useProfileQuery from 'ui/snippets/auth/useProfileQuery';
const feature = config.features.rewards;
const LAST_EXPLORE_TIME_KEY = 'rewards_activity_last_explore_time';
type RewardsActivityEndpoint =
'rewards:user_activity_track_tx' |
'rewards:user_activity_track_tx_confirm' |
'rewards:user_activity_track_contract' |
'rewards:user_activity_track_contract_confirm' |
'rewards:user_activity_track_usage';
export default function useRewardsActivity() {
const { isAuth } = useRewardsContext();
const apiFetch = useApiFetch();
const lastExploreTime = useRef<number>(0);
const profileQuery = useProfileQuery();
const checkActivityPassQuery = useApiQuery('rewards:user_check_activity_pass', {
queryOptions: {
enabled: feature.isEnabled && isAuth && Boolean(profileQuery.data?.address_hash),
},
queryParams: {
address: profileQuery.data?.address_hash ?? '',
},
});
useEffect(() => {
try {
const storedTime = window.localStorage.getItem(LAST_EXPLORE_TIME_KEY);
if (storedTime) {
lastExploreTime.current = Number(storedTime);
}
} catch {}
}, []);
const makeRequest = useCallback(async(endpoint: RewardsActivityEndpoint, params: Record<string, string>) => {
if (!isAuth || !checkActivityPassQuery.data?.is_valid) {
return;
}
try {
return await apiFetch(endpoint, {
fetchParams: {
method: 'POST',
body: params,
},
});
} catch {}
}, [ apiFetch, checkActivityPassQuery.data, isAuth ]);
const trackTransaction = useCallback(async(from: string, to: string, chainId?: string) => {
return (
await makeRequest('rewards:user_activity_track_tx', {
from_address: from,
to_address: to,
chain_id: chainId || config.chain.id || '',
})
) as PreSubmitTransactionResponse | undefined;
}, [ makeRequest ]);
const trackTransactionConfirm = useCallback((hash: string, token: string) =>
makeRequest('rewards:user_activity_track_tx_confirm', { tx_hash: hash, token }),
[ makeRequest ],
);
const trackContract = useCallback(async(address: string) =>
makeRequest('rewards:user_activity_track_contract', {
address,
chain_id: config.chain.id ?? '',
}),
[ makeRequest ],
);
const trackUsage = useCallback((action: string) => {
// check here because this function is called on page load
if (!isAuth || !checkActivityPassQuery.data?.is_valid) {
return;
}
if (action === 'explore') {
const now = Date.now();
if (now - lastExploreTime.current < 5 * MINUTE) {
return;
}
lastExploreTime.current = now;
try {
window.localStorage.setItem(LAST_EXPLORE_TIME_KEY, String(now));
} catch {}
}
return makeRequest('rewards:user_activity_track_usage', {
action,
chain_id: config.chain.id ?? '',
});
}, [ makeRequest, isAuth, checkActivityPassQuery.data ]);
return {
trackTransaction,
trackTransactionConfirm,
trackContract,
trackUsage,
};
}