-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathSignInWithGoogle.tsx
More file actions
85 lines (78 loc) · 2.55 KB
/
SignInWithGoogle.tsx
File metadata and controls
85 lines (78 loc) · 2.55 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
import React, { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { Key } from "ts-key-enum";
import { AuthApi } from "@web/common/apis/auth.api";
import { SyncApi } from "@web/common/apis/sync.api";
import { STORAGE_KEYS } from "@web/common/constants/storage.constants";
import { AbsoluteOverflowLoader } from "@web/components/AbsoluteOverflowLoader";
import { GoogleButton } from "@web/components/oauth/google/GoogleButton";
import { useGoogleLogin } from "@web/components/oauth/google/useGoogleLogin";
import { OnboardingCardLayout } from "../../components";
import { OnboardingStepProps } from "../../components/Onboarding";
export const SignInWithGoogle: React.FC<OnboardingStepProps> = ({
currentStep,
totalSteps,
onNext,
onPrevious,
onSkip,
}) => {
const navigate = useNavigate();
const { login, loading } = useGoogleLogin({
onSuccess: async (code) => {
const result = await AuthApi.loginOrSignup(code);
// Set flag to track that user has completed signup
localStorage.setItem(STORAGE_KEYS.HAS_COMPLETED_SIGNUP, "true");
if (result.isNewUser) {
// Start Google Calendar import in the background
// This allows the import to begin while the user continues through onboarding
SyncApi.importGCal().catch((error) => {
// Log the error but don't block the onboarding flow
console.error("Background Google Calendar import failed:", error);
});
} else {
navigate("/");
}
onNext();
},
onError: (error) => {
console.error(error);
},
});
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (
(event.key === Key.Enter || event.key === Key.ArrowRight) &&
!loading
) {
event.preventDefault();
event.stopPropagation(); // Prevent the centralized handler from also triggering
login();
}
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [login, loading]);
return (
<OnboardingCardLayout
currentStep={currentStep}
totalSteps={totalSteps}
hideSkip
onSkip={onSkip}
onPrevious={onPrevious}
onNext={onNext}
nextBtnDisabled
prevBtnDisabled
showFooter={false}
>
<GoogleButton
disabled={loading}
onClick={login}
style={{
marginTop: "120px",
marginBottom: "60px",
}}
/>
{loading && <AbsoluteOverflowLoader />}
</OnboardingCardLayout>
);
};