-
Notifications
You must be signed in to change notification settings - Fork 30
/
ens-cache.js
227 lines (206 loc) · 6.25 KB
/
ens-cache.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// @flow
import invariant from 'invariant';
import * as React from 'react';
import { ENSCacheContext } from '../components/ens-cache-provider.react.js';
import { getETHAddressForUserInfo } from '../shared/account-utils.js';
import { stringForUser } from '../shared/user-utils.js';
import { getENSNames } from '../utils/ens-helpers.js';
type BaseUserInfo = { +username?: ?string, ... };
type UseENSNamesOptions = {
+allAtOnce?: ?boolean,
};
function useENSNames<T: ?BaseUserInfo>(
users: $ReadOnlyArray<T>,
options?: ?UseENSNamesOptions,
): T[] {
const cacheContext = React.useContext(ENSCacheContext);
const { ensCache } = cacheContext;
const allAtOnce = options?.allAtOnce ?? false;
const cachedInfo = React.useMemo(
() =>
users.map(user => {
if (!user) {
return user;
}
const ethAddress = getETHAddressForUserInfo(user);
const cachedResult =
ethAddress && ensCache
? ensCache.getCachedNameForAddress(ethAddress)
: null;
return {
input: user,
ethAddress,
cachedResult,
};
}),
[users, ensCache],
);
const [fetchedAddresses, setFetchedAddresses] = React.useState<
$ReadOnlySet<string>,
>(new Set());
const [ensNames, setENSNames] = React.useState<$ReadOnlyMap<string, string>>(
new Map(),
);
React.useEffect(() => {
if (!ensCache) {
return;
}
const needFetchUsers: $ReadOnlyArray<{ +username: string }> = cachedInfo
.map(user => {
if (!user) {
return null;
}
const { ethAddress, cachedResult } = user;
if (cachedResult || !ethAddress || fetchedAddresses.has(ethAddress)) {
return null;
}
return { username: ethAddress };
})
.filter(Boolean);
if (needFetchUsers.length === 0) {
return;
}
const needFetchAddresses = needFetchUsers.map(({ username }) => username);
setFetchedAddresses(oldFetchedAddresses => {
const newFetchedAddresses = new Set(oldFetchedAddresses);
for (const ethAddress of needFetchAddresses) {
newFetchedAddresses.add(ethAddress);
}
return newFetchedAddresses;
});
if (allAtOnce) {
void (async () => {
const withENSNames = await getENSNames(ensCache, needFetchUsers);
setENSNames(oldENSNames => {
const newENSNames = new Map(oldENSNames);
for (let i = 0; i < withENSNames.length; i++) {
const ethAddress = needFetchAddresses[i];
const result = withENSNames[i].username;
newENSNames.set(ethAddress, result);
}
return newENSNames;
});
})();
return;
}
for (const ethAddress of needFetchAddresses) {
void (async () => {
const result = await ensCache.getNameForAddress(ethAddress);
if (!result) {
return;
}
setENSNames(oldENSNames => {
const newENSNames = new Map(oldENSNames);
newENSNames.set(ethAddress, result);
return newENSNames;
});
})();
}
}, [cachedInfo, fetchedAddresses, ensCache, allAtOnce]);
return React.useMemo(
() =>
cachedInfo.map(user => {
if (!user) {
return user;
}
const { input, ethAddress, cachedResult } = user;
if (cachedResult) {
return { ...input, username: cachedResult };
} else if (!ethAddress) {
return input;
}
const ensName = ensNames.get(ethAddress);
if (ensName) {
return { ...input, username: ensName };
}
return input;
}),
[cachedInfo, ensNames],
);
}
function useENSName(username: string): string {
const usersObjArr = React.useMemo(() => [{ username }], [username]);
const [potentiallyENSUser] = useENSNames<{ +username: string }>(usersObjArr);
return potentiallyENSUser.username;
}
function useStringForUser(
user: ?{ +username?: ?string, +isViewer?: ?boolean, ... },
): ?string {
const toFetch = user?.isViewer ? null : user;
// stringForUser ignores username is isViewer, so we skip the ENS fetch
const [result] = useENSNames([toFetch]);
if (user?.isViewer) {
return stringForUser(user);
} else if (result) {
return stringForUser(result);
} else {
invariant(
!user,
'the only way result can be falsey is if useENSNames is passed a ' +
'falsey input, and that can only happen if useStringForUser input is ' +
'falsey or isViewer is set',
);
return user;
}
}
function useENSAvatar(ethAddress: ?string): ?string {
const cacheContext = React.useContext(ENSCacheContext);
const { ensCache } = cacheContext;
const cachedAvatar = React.useMemo(() => {
if (!ethAddress) {
return ethAddress;
}
if (!ensCache) {
return null;
}
return ensCache.getCachedAvatarURIForAddress(ethAddress);
}, [ensCache, ethAddress]);
const [ensAvatars, setENSAvatars] = React.useState<
$ReadOnlyMap<string, string>,
>(new Map());
React.useEffect(() => {
if (!ensCache || !ethAddress || cachedAvatar !== undefined) {
return;
}
void (async () => {
const result = await ensCache.getAvatarURIForAddress(ethAddress);
if (!result) {
return;
}
setENSAvatars(oldENSAvatars => {
const newENSAvatars = new Map(oldENSAvatars);
newENSAvatars.set(ethAddress, result);
return newENSAvatars;
});
})();
}, [ensCache, cachedAvatar, ethAddress]);
return React.useMemo(() => {
if (!ethAddress) {
return ethAddress;
} else if (cachedAvatar !== undefined) {
return cachedAvatar;
} else {
return ensAvatars.get(ethAddress);
}
}, [ethAddress, cachedAvatar, ensAvatars]);
}
type BaseENSResolvedUser = { +username?: ?string, +isViewer?: ?boolean, ... };
function useSortedENSResolvedUsers<T: BaseENSResolvedUser>(
userInfos: $ReadOnlyArray<T>,
): $ReadOnlyArray<T> {
const ensResolvedUsers = useENSNames(userInfos);
return React.useMemo(
() =>
ensResolvedUsers.sort((userInfo1, userInfo2) =>
stringForUser(userInfo1).localeCompare(stringForUser(userInfo2)),
),
[ensResolvedUsers],
);
}
export {
useENSNames,
useENSName,
useStringForUser,
useENSAvatar,
useSortedENSResolvedUsers,
};