forked from passportxyz/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefreshMyStampsModalContent.tsx
238 lines (217 loc) · 9.09 KB
/
RefreshMyStampsModalContent.tsx
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
// --- React & ReactDOM hooks
import { useState, useEffect, useContext } from "react";
import { useNavigate } from "react-router-dom";
// --- Types
import { PlatformGroupSpec } from "@gitcoin/passport-platforms";
// --- Identity tools
import { Stamp, PROVIDER_ID, PLATFORM_ID } from "@gitcoin/passport-types";
import { fetchVerifiableCredential } from "@gitcoin/passport-identity";
// --- Contexts
import { useWalletStore } from "../context/walletStore";
import { CeramicContext } from "../context/ceramicContext";
// --- Datadog
import { datadogLogs } from "@datadog/browser-logs";
// --- App components
import { RefreshMyStampsModalContentCardList } from "../components/RefreshMyStampsModalContentCardList";
import { createSignedPayload, reduceStampResponse } from "../utils/helpers";
import { ValidatedPlatform } from "../signer/utils";
import Checkbox from "./Checkbox";
import { Button } from "./Button";
import { LoadButton } from "./LoadButton";
// -- Utils
import { IAM_SIGNATURE_TYPE, iamUrl } from "../config/stamp_config";
import { useDatastoreConnectionContext } from "../context/datastoreConnectionContext";
export type RefreshMyStampsModalContentProps = {
resetStampsAndProgressState: () => void;
onClose: () => void;
validPlatforms: ValidatedPlatform[];
dashboardCustomizationKey: string | null;
};
export type evmPlatformProvider = {
checked: boolean;
platformId: PLATFORM_ID;
platformGroup: PlatformGroupSpec[];
};
export const RefreshMyStampsModalContent = ({
onClose,
validPlatforms,
resetStampsAndProgressState,
dashboardCustomizationKey,
}: RefreshMyStampsModalContentProps): JSX.Element => {
const address = useWalletStore((state) => state.address);
const { handleAddStamps, handleDeleteStamps } = useContext(CeramicContext);
const [isLoading, setLoading] = useState(false);
const [canSubmit, setCanSubmit] = useState(false);
const [showDataInfo, setShowDataInfo] = useState(false);
const [disableOnboard, setDisplayOnboard] = useState(false);
const { did } = useDatastoreConnectionContext();
const navigate = useNavigate();
// TODO: update comments
// SelectedProviders will be passed in to the sidebar to be filled there...
const [verifiedProviders, setVerifiedProviders] = useState<PROVIDER_ID[]>([]);
// TODO: update comments
// SelectedProviders will be passed in to the sidebar to be filled there...
const [selectedProviders, setSelectedProviders] = useState<PROVIDER_ID[]>([...verifiedProviders]);
useEffect(() => {
if (selectedProviders.length !== 0) {
setCanSubmit(true);
} else {
setCanSubmit(false);
}
}, [selectedProviders.length]);
const handleRefreshSelectedStamps = async () => {
try {
setLoading(true);
await handleFetchCredential(selectedProviders);
datadogLogs.logger.info("Successfully saved Stamp, onboard one step verification", {
providers: selectedProviders,
});
localStorage.setItem("successfulRefresh", "true");
} catch (e) {
datadogLogs.logger.error("Verification Error, onboard one step verification", { error: e });
localStorage.setItem("successfulRefresh", "false");
}
setLoading(false);
navigate(`/dashboard${dashboardCustomizationKey ? `/${dashboardCustomizationKey}` : ""}`);
resetStampsAndProgressState();
};
const handleFetchCredential = async (providerIDs: PROVIDER_ID[]): Promise<void> => {
try {
if (!did) throw new Error("No DID found");
if (selectedProviders.length > 0) {
const verified = await fetchVerifiableCredential(
iamUrl,
{
type: "EVMBulkVerify",
types: selectedProviders,
version: "0.0.0",
address: address || "",
proofs: {},
signatureType: IAM_SIGNATURE_TYPE,
},
(data: any) => createSignedPayload(did, data)
);
const vcs = reduceStampResponse(selectedProviders, verified.credentials);
// Delete all stamps ...
await handleDeleteStamps(providerIDs as PROVIDER_ID[]);
// .. and now add all newly validate stamps
if (vcs.length > 0) {
await handleAddStamps(vcs);
}
// grab all providers who are verified from the verify response
const actualVerifiedProviders = providerIDs.filter(
(providerId) =>
!!vcs.find((vc: Stamp | undefined) => vc?.credential?.credentialSubject?.provider === providerId)
);
// both verified and selected should look the same after save
setVerifiedProviders([...actualVerifiedProviders]);
setSelectedProviders([...actualVerifiedProviders]);
}
} catch (e: unknown) {
// TODO: update datadog logger
// datadogLogs.logger.error("Verification Error", { error: e, platform: platform.platformId });
console.log(e);
throw new Error();
}
};
useEffect(() => {
const providerNames: string[] = validPlatforms
.map(({ groups }) => groups.map(({ providers }) => providers.map(({ name }) => name)))
.flat(3);
setSelectedProviders(providerNames as PROVIDER_ID[]);
}, [validPlatforms]);
return (
<div className="flex grow flex-col items-center pb-6">
<div className="w-full grow px-4 md:px-8">
{validPlatforms.length > 0 ? (
<div className="flex flex-col text-color-1">
<div className="my-4 font-heading text-2xl md:my-6">Stamps Found</div>
<div>
{" "}
{/* TODO: update comments */}
{/* container for platforms so user can scroll if they have a lot */}
<RefreshMyStampsModalContentCardList
selectedProviders={selectedProviders}
validPlatforms={validPlatforms}
setSelectedProviders={setSelectedProviders}
/>
</div>
<div className="mt-8 cursor-pointer text-center text-color-2 underline">
<a onClick={() => setShowDataInfo(!showDataInfo)}>How is my data stored?</a>
</div>
{showDataInfo && (
<div className="pt-3 text-justify">
<p>
The only information in your passport is the Decentralized Identifier (DID) associated with your
Ethereum address and the Verifiable Credentials (VCs) issued for each service you connect to your
passport. No identifiable details are stored in your passport as we encrypt the account details when
creating your VCs. You can inspect the data yourself in the Gitcoin Passport by clicking the </>
Passport JSON button in the upper right of the Passport dashboard.
</p>
</div>
)}
<div className="mt-8 grid grid-cols-2 items-center justify-center gap-6">
<Button
variant="secondary"
onClick={() =>
navigate(`/dashboard${dashboardCustomizationKey ? `/${dashboardCustomizationKey}` : ""}`)
}
>
Cancel
</Button>
<LoadButton
onClick={() => {
handleRefreshSelectedStamps();
}}
disabled={!canSubmit || isLoading}
isLoading={isLoading}
>
Confirm Stamps
</LoadButton>
</div>
</div>
) : (
<div className="flex flex-col items-center text-center text-color-1">
<div className="mb-6 mt-4 flex h-10 w-10"></div>
<div className="w-3/4 text-3xl">No New Web3 Stamps Detected</div>
<div className="my-20 text-xl text-color-2">
We did not find any new Web3 stamps to add to your passport. Completing the actions for a web3 stamp and
resubmitting will ensure that stamp is added (for example: Obtain an ENS name, NFT, etc.). Please return
to the dashboard and select additional stamps to verify your unique humanity by connecting to external
accounts (for example: Gmail, Discord, etc).
</div>
<Button
className="w-full"
onClick={() => {
navigate(`/dashboard${dashboardCustomizationKey ? `/${dashboardCustomizationKey}` : ""}`);
resetStampsAndProgressState();
}}
>
Explore Stamps
</Button>
</div>
)}
</div>
<div className="mb-2 mt-8 flex text-color-1">
<Checkbox
data-testid="checkbox-onboard-hide"
id="checkbox-onboard-hide"
checked={disableOnboard}
onChange={(checked: boolean) => {
if (checked) {
const now = Math.floor(Date.now() / 1000);
localStorage.setItem("onboardTS", now.toString());
setDisplayOnboard(true);
} else {
localStorage.removeItem("onboardTS");
setDisplayOnboard(false);
}
}}
/>
<label className="pl-2" htmlFor="checkbox-onboard-hide">
Skip welcome onboarding until stamps expire
</label>
</div>
</div>
);
};