-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCreateController.tsx
262 lines (238 loc) · 6.87 KB
/
CreateController.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { useRef, useState, useEffect } from "react";
import { Container, Footer, Content } from "@/components/layout";
import { Button, cn, Input } from "@cartridge/ui-next";
import { useControllerTheme } from "@/hooks/theme";
import { useDebounce } from "@/hooks/debounce";
import { useUsernameValidation } from "./useUsernameValidation";
import { LoginMode } from "../types";
import { Legal, StatusTray } from ".";
import { useCreateController } from "./useCreateController";
import { ErrorAlert } from "@/components/ErrorAlert";
import { VerifiableControllerTheme } from "@/context/theme";
import InAppSpy from "inapp-spy";
import { usePostHog } from "@/hooks/posthog";
interface CreateControllerViewProps {
theme: VerifiableControllerTheme;
usernameField: {
value: string;
error?: string;
};
validation: ReturnType<typeof useUsernameValidation>;
isLoading: boolean;
error?: Error;
onUsernameChange: (value: string) => void;
onUsernameFocus: () => void;
onUsernameClear: () => void;
onSubmit: () => void;
onKeyDown: (e: React.KeyboardEvent) => void;
isInAppBrowser?: boolean;
}
export function CreateControllerView({
theme,
usernameField,
validation,
isLoading,
error,
isInAppBrowser,
onUsernameChange,
onUsernameFocus,
onUsernameClear,
onSubmit,
onKeyDown,
}: CreateControllerViewProps) {
return (
<Container
variant="expanded"
title={
theme.name === "cartridge"
? "Play with Controller"
: `Play ${theme.name}`
}
description="Connect your Controller"
hideNetwork
>
<form
className="flex flex-col flex-1"
onSubmit={(e) => {
e.preventDefault();
onSubmit();
}}
>
<Content className="gap-0">
<div
className={cn(
"border-destructive-100 rounded",
validation.status === "invalid" || error ? "border" : undefined,
)}
>
<Input
{...usernameField}
autoFocus
placeholder="shinobi"
onFocus={onUsernameFocus}
onChange={(e) => {
onUsernameChange(e.target.value.toLowerCase());
}}
onKeyDown={onKeyDown}
isLoading={validation.status === "validating"}
disabled={isLoading}
onClear={onUsernameClear}
style={{ position: "relative", zIndex: 1 }}
/>
</div>
<StatusTray
username={usernameField.value}
validation={validation}
error={error}
/>
</Content>
<Footer showCatridgeLogo>
{isInAppBrowser && (
<div className="mb-5">
<ErrorAlert
title="Browser not supported"
description="Please open this page in your device's native browser (Safari/Chrome) to continue."
variant="warning"
isExpanded
/>
</div>
)}
{!theme.verified && (
<div className="mb-5">
<ErrorAlert
title="Please proceed with caution"
description="Application domain does not match the configured domain."
variant="warning"
isExpanded
/>
</div>
)}
<Legal />
<Button
type="submit"
isLoading={isLoading}
disabled={validation.status !== "valid"}
>
{validation.exists ? "login" : "sign up"}
</Button>
</Footer>
</form>
</Container>
);
}
function getNativeBrowserUrl() {
// iOS: Open in Safari
if (/iPhone|iPad|iPod/.test(navigator.userAgent)) {
return `x-safari-${window.location.href}`;
}
// Android: Open in Chrome
if (/Android/.test(navigator.userAgent)) {
let currentUrl = window.location.href;
currentUrl = currentUrl.replace(/^https?:\/\//, "");
return `intent://${currentUrl}#Intent;scheme=https;package=com.android.chrome;end`;
}
return null;
}
export function CreateController({
isSlot,
loginMode = LoginMode.Webauthn,
onCreated,
}: {
isSlot?: boolean;
loginMode?: LoginMode;
onCreated?: () => void;
error?: Error;
}) {
const posthog = usePostHog();
const hasLoggedFocus = useRef(false);
const hasLoggedChange = useRef(false);
const theme = useControllerTheme();
const pendingSubmitRef = useRef(false);
const [usernameField, setUsernameField] = useState({
value: "",
error: undefined,
});
// Debounce validation quickly to reduce latency
const { debouncedValue: validationUsername } = useDebounce(
usernameField.value,
25,
);
const validation = useUsernameValidation(validationUsername);
const { debouncedValue: debouncedValidation } = useDebounce(validation, 200);
const { isLoading, error, setError, handleSubmit } = useCreateController({
onCreated,
isSlot,
loginMode,
});
const handleFormSubmit = () => {
if (!usernameField.value) {
return;
}
if (validation.status === "validating") {
pendingSubmitRef.current = true;
return;
}
if (validation.status === "valid") {
handleSubmit(usernameField.value, !!validation.exists);
}
};
useEffect(() => {
if (pendingSubmitRef.current && debouncedValidation.status === "valid") {
pendingSubmitRef.current = false;
handleFormSubmit();
}
}, [debouncedValidation.status, handleFormSubmit]);
const [{ isInApp }] = useState(() => InAppSpy());
useEffect(() => {
if (isInApp) {
const nativeBrowserUrl = getNativeBrowserUrl();
if (nativeBrowserUrl) {
// Try to open in native browser
window.location.href = nativeBrowserUrl;
}
}
}, [isInApp]);
const handleUsernameChange = (value: string) => {
if (!hasLoggedChange.current) {
posthog?.capture("Change Username");
hasLoggedChange.current = true;
}
setError(undefined);
setUsernameField((u) => ({
...u,
value,
error: undefined,
}));
};
const handleUsernameFocus = () => {
if (!hasLoggedFocus.current) {
posthog?.capture("Focus Username");
hasLoggedFocus.current = true;
}
};
const handleUsernameClear = () => {
setError(undefined);
setUsernameField((u) => ({ ...u, value: "" }));
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "Enter") {
e.preventDefault();
handleFormSubmit();
}
};
return (
<CreateControllerView
theme={theme}
usernameField={usernameField}
validation={debouncedValidation}
isLoading={isLoading}
error={error}
isInAppBrowser={isInApp}
onUsernameChange={handleUsernameChange}
onUsernameFocus={handleUsernameFocus}
onUsernameClear={handleUsernameClear}
onSubmit={handleFormSubmit}
onKeyDown={handleKeyDown}
/>
);
}