-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
167 lines (150 loc) · 4.65 KB
/
index.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
import { createRoot } from "react-dom/client";
import {
PassportScoreWidget,
usePassportScore,
CollapseMode,
} from "@passportxyz/passport-embed";
import "./index.css";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useState } from "react";
const appQueryClient = new QueryClient({
defaultOptions: {
queries: {
// With this config, the query will be re-fetched when this tab/window
// is refocused and the data has not been fetched for at least 1 minute
refetchOnWindowFocus: true,
refetchOnMount: false,
refetchOnReconnect: false,
staleTime: 1000 * 60 * 1,
gcTime: Infinity,
},
},
});
const passportEmbedParams = {
apiKey: import.meta.env.VITE_API_KEY,
scorerId: import.meta.env.VITE_SCORER_ID,
overrideIamUrl: "https://embed.review.passport.xyz",
challengeSignatureUrl: "https://iam.review.passport.xyz/api/v0.0.0/challenge",
// challengeSignatureUrl: "http://localhost:8003/api/v0.0.0/challenge",
// oAuthPopUpUrl: "http://localhost:3010",
oAuthPopUpUrl:
"http://passport-embed-popup-review.s3-website-us-west-2.amazonaws.com",
};
const connectWallet = async () => {
// Check if MetaMask is installed
if (typeof window.ethereum === "undefined") {
alert("Please install MetaMask!");
return;
}
try {
// Request account access
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
// Get the connected account
return accounts[0];
} catch (error) {
console.error(error);
alert("Failed to connect to wallet");
}
};
const generateSignature = async (message: string) => {
try {
// Check if MetaMask is installed
if (typeof window.ethereum === "undefined") {
alert("Please install MetaMask!");
return;
}
// Request account access if not already connected
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
const signerAddress = accounts[0];
// Sign the message
const signature = await window.ethereum.request({
method: "personal_sign",
params: [message, signerAddress],
});
return signature ? signature : "";
} catch (error) {
console.error("Error signing message:", error);
alert("Failed to sign message");
throw error;
}
};
const DirectPassportDataAccess = ({ address }: { address?: string }) => {
const { data, isError, error } = usePassportScore({
...passportEmbedParams,
address,
});
return (
<div>
<h1>This is an app element!</h1>
<ul>
<li>Passport Score: {data?.score}</li>
<li>Passport Threshold: {data?.threshold}</li>
<li>Is passing threshold: {data?.passingScore ? "True" : "False"}</li>
<li>
What stamps?
<pre>{JSON.stringify(data?.stamps, undefined, 2)}</pre>
</li>
{isError && <li>Error: {error?.message}</li>}
</ul>
</div>
);
};
const Dashboard = () => {
const [address, setAddress] = useState<string | undefined>();
const [collapseMode, setCollapseMode] = useState<CollapseMode>("shift");
return (
<div className="container">
<h1>Passport Widgets Example</h1>
<h3>Check your Passport score</h3>
<div style={{ marginBottom: "1rem" }}>
<label style={{ marginRight: "0.5rem" }}>Collapse Mode:</label>
<select
value={collapseMode}
onChange={(e) =>
setCollapseMode(e.target.value as "shift" | "overlay" | "off")
}
>
<option value="shift">Shift</option>
<option value="overlay">Overlay</option>
<option value="off">Off</option>
</select>
</div>
<PassportScoreWidget
{...passportEmbedParams}
address={address}
collapseMode={collapseMode}
// Generally you would not provide this, the widget has its own QueryClient.
// But this can be used to override query parameters or to share a QueryClient
// with the wider app and/or multiple widgets
queryClient={appQueryClient}
connectWalletCallback={async () => {
const address = await connectWallet();
setAddress(address);
}}
generateSignatureCallback={generateSignature}
/*
theme={{
colors: {
primary: "255, 255, 0",
},
}}
*/
/>
<DirectPassportDataAccess address={address} />
</div>
);
};
const App = () => (
<QueryClientProvider client={appQueryClient}>
<Dashboard />
</QueryClientProvider>
);
const rootElement = document.getElementById("root");
if (rootElement) {
const root = createRoot(rootElement);
root.render(<App />);
}