|
1 | | -import { useEffect, useMemo, useState } from 'react'; |
| 1 | +import { useMemo } from 'react'; |
2 | 2 |
|
| 3 | +import { RSK_CHAIN_ID } from '../../../../../../config/chains'; |
| 4 | + |
| 5 | +import { useCacheCall } from '../../../../../../hooks'; |
3 | 6 | import { useAccount } from '../../../../../../hooks/useAccount'; |
4 | 7 | import { areAddressesEqual } from '../../../../../../utils/helpers'; |
5 | 8 | import { UserPoints } from '../../../LeaderboardPointsPage.types'; |
6 | | -import { DATA_ENDPOINT_URL } from '../LeaderboardPoints.constants'; |
| 9 | +import { |
| 10 | + DATA_ENDPOINT_URL, |
| 11 | + S3_DATA_ENDPOINT_URL, |
| 12 | +} from '../LeaderboardPoints.constants'; |
7 | 13 |
|
8 | 14 | export const useGetPoints = () => { |
9 | 15 | const { account } = useAccount(); |
10 | 16 |
|
11 | | - const [data, setData] = useState<UserPoints[]>([]); |
| 17 | + const { value: data } = useCacheCall( |
| 18 | + 'bob-lp-points', |
| 19 | + RSK_CHAIN_ID, |
| 20 | + async () => { |
| 21 | + const [data, s3Data] = await Promise.all([ |
| 22 | + fetch(DATA_ENDPOINT_URL).then(response => response?.json()), |
| 23 | + fetch(S3_DATA_ENDPOINT_URL).then(response => response?.json()), |
| 24 | + ]); |
| 25 | + |
| 26 | + const result: { [key: string]: UserPoints } = {}; |
| 27 | + |
| 28 | + data.forEach(user => { |
| 29 | + const points = parseFloat(user.points.replace(/,/g, '')); |
| 30 | + |
| 31 | + result[user.toAddress.toLowerCase()] = { |
| 32 | + wallet: user.toAddress, |
| 33 | + points, |
| 34 | + s3Points: 0, |
| 35 | + total: points, |
| 36 | + }; |
| 37 | + }); |
12 | 38 |
|
13 | | - useEffect(() => { |
14 | | - if (!data || data.length === 0) { |
15 | | - fetch(DATA_ENDPOINT_URL) |
16 | | - .then(response => response?.json()) |
17 | | - .then(data => { |
18 | | - if (!data) { |
19 | | - return; |
20 | | - } |
| 39 | + s3Data.forEach(user => { |
| 40 | + const s3Points = parseFloat(user.points.replace(/,/g, '')); |
| 41 | + const key = user.toAddress.toLowerCase(); |
21 | 42 |
|
22 | | - const result: UserPoints[] = data.map(user => ({ |
| 43 | + if (!(key in result)) { |
| 44 | + result[key] = { |
23 | 45 | wallet: user.toAddress, |
24 | | - points: parseFloat(user.points.replace(/,/g, '')), |
25 | | - })); |
| 46 | + points: 0, |
| 47 | + s3Points, |
| 48 | + total: s3Points, |
| 49 | + }; |
| 50 | + } else { |
| 51 | + const points = result[key].points; |
26 | 52 |
|
27 | | - setData(result); |
28 | | - }); |
29 | | - } |
30 | | - }, [data]); |
| 53 | + result[key] = { |
| 54 | + wallet: user.toAddress, |
| 55 | + points, |
| 56 | + s3Points, |
| 57 | + total: points + s3Points, |
| 58 | + }; |
| 59 | + } |
| 60 | + }); |
31 | 61 |
|
32 | | - const sortedUsers = useMemo(() => { |
33 | | - return data.sort((a, b) => b.points - a.points); |
34 | | - }, [data]); |
| 62 | + return Object.keys(result) |
| 63 | + .map(key => ({ ...result[key] })) |
| 64 | + .sort((user1, user2) => (user1.total < user2.total ? 1 : -1)); |
| 65 | + }, |
| 66 | + [], |
| 67 | + [], |
| 68 | + ); |
35 | 69 |
|
36 | 70 | const userIndex = useMemo(() => { |
37 | | - return sortedUsers.findIndex(user => |
38 | | - areAddressesEqual(user.wallet, account), |
39 | | - ); |
40 | | - }, [sortedUsers, account]); |
| 71 | + return data.findIndex(user => areAddressesEqual(user.wallet, account)); |
| 72 | + }, [data, account]); |
41 | 73 |
|
42 | 74 | const connectedWalletPoints = useMemo(() => { |
43 | 75 | if (userIndex !== -1) { |
44 | | - const { wallet, points } = sortedUsers[userIndex]; |
| 76 | + const { wallet, points, s3Points, total } = data[userIndex]; |
45 | 77 | return [ |
46 | 78 | { |
47 | 79 | id: userIndex + 1, |
48 | 80 | wallet, |
49 | 81 | points, |
| 82 | + s3Points, |
| 83 | + total, |
50 | 84 | }, |
51 | 85 | ]; |
52 | 86 | } |
53 | 87 | return []; |
54 | | - }, [sortedUsers, userIndex]); |
| 88 | + }, [data, userIndex]); |
55 | 89 |
|
56 | 90 | const points = useMemo( |
57 | 91 | () => |
58 | | - sortedUsers.map(({ wallet, points }, index) => ({ |
| 92 | + data.map(({ wallet, points, s3Points, total }, index) => ({ |
59 | 93 | id: index + 1, |
60 | 94 | wallet, |
61 | 95 | points, |
| 96 | + s3Points, |
| 97 | + total, |
62 | 98 | })), |
63 | | - [sortedUsers], |
| 99 | + [data], |
64 | 100 | ); |
65 | 101 |
|
66 | 102 | return { |
|
0 commit comments