-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaccount_widget.tsx
More file actions
104 lines (85 loc) · 2.94 KB
/
Copy pathaccount_widget.tsx
File metadata and controls
104 lines (85 loc) · 2.94 KB
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
"use client";
import { type FC, useEffect, useState } from "react";
import { useSDKState } from "@oko-wallet-user-dashboard/state/sdk";
import { useUserInfoState } from "@oko-wallet-user-dashboard/state/user_info";
import { AuthProgressWidget } from "./auth_progress_widget";
import { LoginWidget } from "../login_widget/login_widget";
import { Spinner } from "../../spinner/spinner";
import { paths } from "@oko-wallet-user-dashboard/paths";
import { useRouter } from "next/navigation";
import type { AuthType } from "@oko-wallet/oko-types/auth";
import styles from "./account_widget.module.scss";
type SigningInState =
| { status: "ready" }
| { status: "signing-in" }
| { status: "failed"; error: string };
export const AccountWidget: FC<AccountWidgetProps> = () => {
const okoWallet = useSDKState((state) => state.oko_cosmos)?.okoWallet;
const [signingInState, setSigningInState] = useState<SigningInState>({
status: "ready",
});
const router = useRouter();
const isSignedIn = useUserInfoState((state) => state.isSignedIn);
const setAuthType = useUserInfoState((state) => state.setAuthType);
// TODO: add other login methods, and update the type accordingly
const [loginMethod, setLoginMethod] = useState<AuthType>("google");
async function handleSignIn(method: AuthType) {
setLoginMethod(method);
if (!okoWallet) {
console.error("okoWallet is not initialized");
return;
}
if (
method !== "google" &&
method !== "auth0" &&
method !== "telegram" &&
method !== "x" &&
method !== "discord"
) {
console.error("Unsupported login method atm: %s", method);
return;
}
try {
setSigningInState({ status: "signing-in" });
await okoWallet.signIn(method === "auth0" ? "email" : method);
setAuthType(method);
setSigningInState({ status: "ready" });
} catch (error: any) {
console.error("sign in fail, err: %s", error);
setAuthType(null);
const errorMessage =
error instanceof Error ? error.message : "Login failed";
setSigningInState({ status: "failed", error: errorMessage });
}
}
async function handleRetry() {
setSigningInState({ status: "ready" });
}
useEffect(() => {
if (isSignedIn) {
router.push(paths.home);
}
}, [isSignedIn]);
if (!okoWallet) {
return (
<div className={styles.spinnerWrapper}>
<Spinner size={30} />
</div>
);
}
// The email login loading progress is shown in the Attached popup, so we don't need to show that here
if (signingInState.status === "signing-in" && loginMethod !== "auth0") {
return <AuthProgressWidget method={loginMethod} status="loading" />;
}
if (signingInState.status === "failed") {
return (
<AuthProgressWidget
method={loginMethod}
status="failed"
onRetry={handleRetry}
/>
);
}
return <LoginWidget onSignIn={handleSignIn} />;
};
export interface AccountWidgetProps {}