-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpage.tsx
121 lines (113 loc) · 3.57 KB
/
page.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
"use client";
import { useState, useEffect } from "react";
import { Footer } from "./components/Footer";
import { ResponseSection } from "./components/ResponseSection";
import { SettleSection } from "./components/SettleSection";
import Image from "next/image";
import { VerifySection } from "./components/VerifySection";
import { Header } from "./components/Header";
import { Chain } from "./constants";
export default function Home() {
const [selectedTab, setSelectedTab] = useState<Chain>(Chain.AVAIL);
const [response, setResponse] = useState<string>("");
const [verifyResponse, setVerifyResponse] = useState<string>("");
const [pending, setPending] = useState<boolean>(true);
const [data, setData] = useState<string>("");
const [jobId, setJobId] = useState<string>("");
const [autoRefresh, setAutoRefresh] = useState<boolean>(false);
const [explorerLink, setExplorerLink] = useState<string>("");
useEffect(() => {
setVerifyResponse("");
setPending(true);
setExplorerLink("");
}, [jobId]);
const onSettle = async (data: string, chain: string): Promise<void> => {
return new Promise(async (resolve, reject) => {
try {
const res = await fetch("/api/settle", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ data, chain }),
});
const responseJson = await res.json();
setResponse(responseJson);
setVerifyResponse("");
setPending(true);
setJobId(responseJson.jobID);
resolve();
} catch (error) {
reject(error);
}
});
};
const onVerify = async (jobId: string): Promise<void> => {
return new Promise(async (resolve, reject) => {
try {
const res = await fetch("/api/verify", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ jobId }),
});
const responseJson = await res.json();
setPending(responseJson.status === "pending");
setVerifyResponse(responseJson);
if (responseJson.link) {
setAutoRefresh(false);
setExplorerLink(responseJson.link);
}
resolve();
} catch (error) {
reject(error);
}
});
};
useEffect(() => {
const loopCall = async () => {
await onVerify(jobId);
};
const intervalId = setInterval(() => {
if (jobId != "" && autoRefresh && pending) {
loopCall();
}
}, 5000);
return () => clearInterval(intervalId);
}, [jobId, autoRefresh, pending]);
return (
<main className="w-screen h-screen flex flex-col px-20 pt-12 pb-6 gap-10">
<Header />
<div className="flex gap-10 h-full overflow-hidden">
<SettleSection
data={data}
setData={setData}
selectedTab={selectedTab}
setSelectedTab={setSelectedTab}
onSettle={onSettle}
/>
<ResponseSection response={response} />
<VerifySection
jobId={jobId}
setJobId={setJobId}
verifyResponse={verifyResponse}
onVerify={onVerify}
autoRefresh={autoRefresh}
setAutoRefresh={setAutoRefresh}
pending={pending}
explorerLink={explorerLink}
/>
</div>
<Footer>
<span className="text-fg-primary text-lg">Built with ❤️ by</span>
<Image
src="https://assets.stackrlabs.xyz/light.svg"
alt="Stackr Logo"
width={150}
height={150}
/>
</Footer>
</main>
);
}