forked from passportxyz/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Welcome.tsx
180 lines (161 loc) · 5.58 KB
/
Welcome.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
/* eslint-disable react-hooks/exhaustive-deps */
// --- React Methods
import React, { useContext, useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
// --- Types
import { Status, Step } from "../components/Progress";
import { PLATFORM_ID } from "@gitcoin/passport-types";
import { PlatformProps } from "../components/GenericPlatform";
// --Components
import MinimalHeader from "../components/MinimalHeader";
import { PAGE_PADDING } from "../components/PageWidthGrid";
import HeaderContentFooterGrid from "../components/HeaderContentFooterGrid";
import PageRoot from "../components/PageRoot";
import { WelcomeBack } from "../components/WelcomeBack";
import { RefreshMyStampsModal } from "../components/RefreshMyStampsModal";
// --Chakra UI Elements
import { useDisclosure } from "@chakra-ui/react";
// --- Contexts
import { CeramicContext, IsLoadingPassportState } from "../context/ceramicContext";
import { UserContext } from "../context/userContext";
import { InitialWelcome } from "../components/InitialWelcome";
import LoadingScreen from "../components/LoadingScreen";
// --- Utils
import { fetchPossibleEVMStamps, ValidatedPlatform } from "../signer/utils";
import BodyWrapper from "../components/BodyWrapper";
const MIN_DELAY = 50;
const MAX_DELAY = 800;
const getStepDelay = () => Math.floor(Math.random() * (MAX_DELAY - MIN_DELAY + 1) + MIN_DELAY);
export default function Welcome() {
const { isOpen, onOpen, onClose } = useDisclosure();
const { passport, allPlatforms, isLoadingPassport } = useContext(CeramicContext);
const { wallet, address } = useContext(UserContext);
const navigate = useNavigate();
// Route user to home page when wallet is disconnected
useEffect(() => {
if (!wallet) {
navigate("/");
}
}, [wallet]);
const initialSteps = [
{
name: "Scanning",
status: Status.SUCCESS,
},
{
name: "Double Checking",
status: Status.NOT_STARTED,
},
{
name: "Validating",
status: Status.NOT_STARTED,
},
{
name: "Brewing Coffee",
status: Status.NOT_STARTED,
},
{
name: "Almost there",
status: Status.NOT_STARTED,
},
{
name: "Ready for review",
status: Status.NOT_STARTED,
},
];
const [validPlatforms, setValidPlatforms] = useState<ValidatedPlatform[]>();
const [currentSteps, setCurrentSteps] = useState<Step[]>(initialSteps);
const resetStampsAndProgressState = () => {
setValidPlatforms([]);
setCurrentSteps(initialSteps);
};
const updateSteps = (activeStepIndex: number, error?: boolean) => {
// if error mark ActiveStep as ERROR, and previous steps as SUCCESS
const steps = [...currentSteps];
if (error) {
steps.slice(0, activeStepIndex).forEach((step) => (step.status = Status.SUCCESS));
steps[activeStepIndex - 1].status = Status.ERROR;
setCurrentSteps(steps);
return;
}
// if there is no error mark previous steps as SUCCESS, mark step after activeStepIndex as IS_STARTED
steps.slice(0, activeStepIndex).forEach((step) => (step.status = Status.SUCCESS));
if (steps[activeStepIndex]) {
steps[activeStepIndex].status = Status.IN_PROGRESS;
}
setCurrentSteps(steps);
};
const fetchValidPlatforms = async (
address: string,
allPlatforms: Map<PLATFORM_ID, PlatformProps>
): Promise<ValidatedPlatform[]> => {
try {
let step = 0;
const incrementStep = () => {
if (step < 4) {
updateSteps(++step);
setTimeout(incrementStep, getStepDelay());
}
};
incrementStep();
const validPlatforms = await fetchPossibleEVMStamps(address, allPlatforms, passport);
step = 5;
updateSteps(6);
await new Promise((resolve) => setTimeout(resolve, 300));
return validPlatforms;
} catch (error) {
console.log(error);
throw new Error("Error: ");
}
};
const handleFetchPossibleEVMStamps = async (addr: string, allPlats: Map<PLATFORM_ID, PlatformProps>) => {
try {
const platforms = await fetchValidPlatforms(addr, allPlats);
setValidPlatforms(platforms);
} catch (error) {
console.log(error);
throw new Error();
}
};
return (
<PageRoot className="text-color-2">
<HeaderContentFooterGrid>
<div className={`${PAGE_PADDING} bg-background`}>
<MinimalHeader className={`border-b border-accent-2`} />
</div>
<BodyWrapper className="flex justify-center">
<div className="max-w-[464px]">
{isLoadingPassport === IsLoadingPassportState.Idle ||
isLoadingPassport === IsLoadingPassportState.FailedToConnect ? (
passport && passport.stamps.length > 0 ? (
<WelcomeBack
handleFetchPossibleEVMStamps={handleFetchPossibleEVMStamps}
onOpen={onOpen}
resetStampsAndProgressState={resetStampsAndProgressState}
/>
) : (
<InitialWelcome
onBoardFinished={async () => {
if (address) {
handleFetchPossibleEVMStamps(address, allPlatforms);
onOpen();
}
}}
/>
)
) : (
<LoadingScreen />
)}
</div>
</BodyWrapper>
</HeaderContentFooterGrid>
<RefreshMyStampsModal
steps={currentSteps}
isOpen={isOpen}
onClose={onClose}
validPlatforms={validPlatforms}
resetStampsAndProgressState={resetStampsAndProgressState}
/>
</PageRoot>
);
}