This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
generated from moonshotcollective/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
297 lines (286 loc) · 11 KB
/
index.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import React, { useCallback, useContext, useEffect, useState } from "react";
import { Core } from "@self.id/core";
import axios from "axios";
import {
Button,
Code,
HStack,
InputGroup,
InputLeftElement,
Box,
Heading,
SimpleGrid,
VStack,
AlertDialog,
AlertDialogBody,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogContent,
AlertDialogOverlay,
} from "@chakra-ui/react";
import { Web3Context } from "../helpers/Web3Context";
import { CeramicClient } from "@ceramicnetwork/http-client";
import { ModelManager } from "@glazed/devtools";
import { DID } from "dids";
import { Ed25519Provider } from "key-did-provider-ed25519";
import { getResolver } from "key-did-resolver";
import { fromString, toString } from "uint8arrays";
import { DataModel } from "@glazed/datamodel";
import { DIDDataStore } from "@glazed/did-datastore";
import { EthereumAuthProvider, SelfID, WebClient } from "@self.id/web";
import { Table, Modal, Form, Input, Divider, InputNumber, Select, Typography, Tag, Space, PageHeader } from "antd";
import { randomBytes } from "@stablelib/random";
import { ethers } from "ethers";
import modelAliases from "../model.json";
import { ceramicCoreFactory, CERAMIC_TESTNET } from "../ceramic";
import { getDidFromTokenURI, loadDRecruitV1Contract, loadTokenContract } from "../helpers";
import MediaCard from "../components/cards/MediaCard";
import { Layout } from "../components/layout/Layout";
import { HomeActions } from "../components/layout/HomeActions";
import { FiSearch } from "react-icons/fi";
import { useDebounce } from "../hooks";
import { IPFS_GATEWAY } from "../constants";
function Home() {
const context = useContext(Web3Context);
const [isAlertOpen, setIsAlertOpen] = React.useState(false);
const onAlertClose = async () => {
const data = [
{
chainId: "0x" + context.targetNetwork.chainId.toString(16),
chainName: context.targetNetwork.name,
nativeCurrency: context.targetNetwork.nativeCurrency,
rpcUrls: [context.targetNetwork.rpcUrl],
blockExplorerUrls: [context.targetNetwork.blockExplorer],
},
];
try {
await ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: data[0].chainId }],
});
setIsAlertOpen(false);
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) {
try {
await ethereum.request({
method: "wallet_addEthereumChain",
params: data,
});
setIsAlertOpen(false);
} catch (addError) {
console.log(addError);
}
}
// handle other "switch" errors
}
};
const cancelRef = React.useRef();
const [inputEmail, setInputEmail] = useState("");
const [recipients, setRecipients] = useState([]);
const [developerProfiles, setDeveloperProfiles] = useState([]);
// State and setters for ...
// Search term
const [searchTerm, setSearchTerm] = useState("");
// API search results
const [results, setResults] = useState([]);
// Searching status (whether there is pending API request)
const [isMatch, setIsMatch] = useState(false);
const [isSearching, setIsSearching] = useState(false);
// Debounce search term so that it only gives us latest value ...
// ... if searchTerm has not been updated within last 500ms.
// The goal is to only have the API call fire when user stops typing ...
// ... so that we aren't hitting our API rapidly.
const debouncedSearchTerm = useDebounce(searchTerm, 500);
const [dRecruitContract, setDRecruitContract] = useState();
const [tokenContract, setTokenContract] = useState();
const [tokenMetadata, setTokenMetadata] = useState({ name: null, symbol: null });
const [store, setStore] = useState();
const [prevNote, setPrevNote] = useState("");
// Effect for API call
useEffect(
() => {
if (debouncedSearchTerm) {
setIsSearching(true);
handleSearch(debouncedSearchTerm).then(devs => {
setIsSearching(false);
setIsMatch(true);
setResults(devs);
});
} else {
setResults([]);
setIsMatch(false);
setIsSearching(false);
}
},
[debouncedSearchTerm], // Only call effect if debounced search term changes
);
const init = async () => {
if (context.injectedProvider && context.injectedProvider.getSigner()) {
try {
const signer = context.injectedProvider.getSigner();
const contract = await loadDRecruitV1Contract(context.targetNetwork, signer);
const tokenAddress = await contract.token();
const tokenContract = await loadTokenContract(tokenAddress, signer);
setDRecruitContract(contract);
setTokenContract(tokenContract);
const tokenName = await tokenContract.name();
const tokenSymbol = await tokenContract.symbol();
setTokenMetadata({ name: tokenName, symbol: tokenSymbol });
const lastTokenId = await contract.tokenId();
const tokenIds = [...Array(parseInt(lastTokenId, 10)).keys()];
const tokenURIs = await Promise.all(tokenIds.map(async id => contract.uri(id)));
const developersDID = [...new Set(tokenURIs.map(uri => getDidFromTokenURI(uri).did))];
const core = ceramicCoreFactory();
const devProfiles = await Promise.all(
developersDID.map(async did => ({
did,
basicProfile: await core.get("basicProfile", did),
cryptoAccounts: await core.get("cryptoAccounts", did),
webAccounts: await core.get("alsoKnownAs", did),
publicProfile: await core.get("publicProfile", did),
privateProfile: await core.get("privateProfile", did),
})),
);
setDeveloperProfiles(devProfiles);
} catch (error) {
console.log({ error });
setIsAlertOpen(true);
}
}
};
const handleSearch = async value => {
const foundDevs = developerProfiles.filter(({ publicProfile }) => {
if (publicProfile) {
const skills = publicProfile.skillTags.map(s => s.toLowerCase());
console.log({ skills });
const isMatch = skills.some(s => s.startsWith(value)); // skills.includes(value);
console.log({ isMatch });
return isMatch;
}
return false;
});
console.log({ foundDevs });
if (!foundDevs || foundDevs.length === 0) {
return null;
}
return foundDevs;
};
useEffect(() => {
init();
}, [context.injectedProvider]);
return (
<Layout>
<AlertDialog isOpen={isAlertOpen} leastDestructiveRef={cancelRef}>
<AlertDialogOverlay>
<AlertDialogContent>
<AlertDialogHeader fontSize="lg" fontWeight="bold">
Switch network
</AlertDialogHeader>
<AlertDialogBody>
To use this app you must switch to the {context.targetNetwork.name} network.
</AlertDialogBody>
<AlertDialogFooter>
<Button ref={cancelRef} onClick={onAlertClose} colorScheme="blue">
Switch
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
<HomeActions contract={dRecruitContract} mySelf={context.self} />
<Input type="search" placeholder="Search" onChange={e => setSearchTerm(e.target.value)} />
{isSearching && <div>Searching ...</div>}
<Heading>Found developers:</Heading>
<SimpleGrid columns={4} spacing={10}>
{isMatch &&
results &&
results.map(({ did, basicProfile, cryptoAccounts, webAccounts, privateProfile, publicProfile }) => {
const formattedAvatar = basicProfile.image
? IPFS_GATEWAY + basicProfile.image.original.src.split("//")[1]
: undefined;
const formattedBg = basicProfile.background
? IPFS_GATEWAY + basicProfile.background.original.src.split("//")[1]
: undefined;
const account = Object.keys(cryptoAccounts)[0]?.split("@")[0];
return (
publicProfile &&
privateProfile && (
<MediaCard
key={did}
did={did}
account={account}
avatarSrc={formattedAvatar}
coverSrc={formattedBg}
publicProfile={publicProfile}
heading={basicProfile.emoji + " " + basicProfile.name}
subheading={`${
basicProfile.residenceCountry || basicProfile.homeLocation
? "Location: " +
basicProfile.residenceCountry +
(basicProfile.homeLocation ? ", " + basicProfile.homeLocation : "")
: ""
}`}
description={basicProfile.description}
date={`Birthdate: ${basicProfile.birthDate}`}
dRecruitContract={dRecruitContract}
hasWebAccount={!!webAccounts}
privateProfile={privateProfile}
tokenContract={tokenContract}
tokenMetadata={tokenMetadata}
/>
)
);
})}
</SimpleGrid>
<Heading>All developers:</Heading>
<SimpleGrid
columns={{
sm: 1,
md: 3,
lg: 3,
}}
spacing={5}
>
{developerProfiles.map(({ did, basicProfile, cryptoAccounts, webAccounts, privateProfile, publicProfile }) => {
const formattedAvatar = basicProfile.image
? IPFS_GATEWAY + basicProfile.image.original.src.split("//")[1]
: undefined;
const formattedBg = basicProfile.background
? IPFS_GATEWAY + basicProfile.background.original.src.split("//")[1]
: undefined;
const account = Object.keys(cryptoAccounts)[0]?.split("@")[0];
return (
publicProfile &&
privateProfile && (
<MediaCard
key={did}
did={did}
account={account}
avatarSrc={formattedAvatar}
coverSrc={formattedBg}
publicProfile={publicProfile}
heading={basicProfile.emoji + " " + basicProfile.name}
subheading={`${
basicProfile.residenceCountry || basicProfile.homeLocation
? "Location: " +
basicProfile.residenceCountry +
(basicProfile.homeLocation ? ", " + basicProfile.homeLocation : "")
: ""
}`}
description={basicProfile.description}
date={`Birthdate: ${basicProfile.birthDate}`}
dRecruitContract={dRecruitContract}
hasWebAccount={!!webAccounts}
privateProfile={privateProfile}
tokenContract={tokenContract}
tokenMetadata={tokenMetadata}
/>
)
);
})}
</SimpleGrid>
</Layout>
);
}
export default Home;