-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGoogleSignInSnackbar.tsx
More file actions
113 lines (96 loc) · 4.51 KB
/
Copy pathGoogleSignInSnackbar.tsx
File metadata and controls
113 lines (96 loc) · 4.51 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
105
106
107
108
109
110
111
112
113
import * as React from "react";
import { useSnackbar, SnackbarConfig } from "./SnackbarProvider";
import { LocalStorageInt } from "./localStorage";
// Assume gtag is available globally, or provide it via context/props
declare global {
interface Window {
gtag: (...args: any[]) => void;
// Define other global properties if needed, e.g., for googleSignIn function
triggerGoogleSignIn?: () => void; // Example, replace with actual
}
}
const { useEffect, useCallback } = React;
const GOOGLE_SIGN_IN_SHOW_COUNTER_LS_KEY = "googleSignInShowCounter"; // From original logic
const GOOGLE_SIGN_IN_SNACKBAR_SHOWN_COUNT_LS_KEY = "googleSignInSnackbarShownCount";
const GOOGLE_SIGN_IN_SNACKBAR_DISMISSED_LS_KEY = "googleSignInSnackbarShownDismissed";
const MAX_SHOW_COUNT = 9999999; // Effectively infinite as per original
const SNACKBAR_ID = "google-sign-in-snackbar";
// Helper functions mirroring 'Kind' class logic for this specific snackbar
const getShownCount = () => new LocalStorageInt(GOOGLE_SIGN_IN_SNACKBAR_SHOWN_COUNT_LS_KEY).get() || 0;
const incrementShownCount = () => {
new LocalStorageInt(GOOGLE_SIGN_IN_SNACKBAR_SHOWN_COUNT_LS_KEY).set(getShownCount() + 1);
};
const getDismissed = () => localStorage.getItem(GOOGLE_SIGN_IN_SNACKBAR_DISMISSED_LS_KEY) === "true";
const setDismissed = () => localStorage.setItem(GOOGLE_SIGN_IN_SNACKBAR_DISMISSED_LS_KEY, "true");
const customShowLogic = () => {
// This counter is different from the general "shownCount" for the snackbar instance itself.
// This seems to be a global counter for how many times the app has considered showing this.
const showCountConsidered = new LocalStorageInt(GOOGLE_SIGN_IN_SHOW_COUNTER_LS_KEY).getAndIncrement();
let modulo = 50;
if (showCountConsidered < 5) modulo = 1;
else if (showCountConsidered < 20) modulo = 2;
else if (showCountConsidered < 50) modulo = 4;
else if (showCountConsidered < 100) modulo = 10;
return showCountConsidered % modulo === 0;
};
// Placeholder for actual Google Sign-In trigger function
// This should be replaced with the actual implementation used in the project.
const triggerGoogleSignIn = () => {
console.log("Attempting to trigger Google Sign-In");
if (window.triggerGoogleSignIn) {
window.triggerGoogleSignIn();
} else {
alert("Google Sign-In function not implemented yet.");
}
};
export function GoogleSignInSnackbarTrigger() {
const { showSnackbar, hideSnackbar } = useSnackbar();
const handleSignInClick = useCallback(() => {
if (window.gtag) {
window.gtag("event", "snackbar.googleSignIn.signInClicked");
}
triggerGoogleSignIn(); // Call the actual sign-in function
hideSnackbar(SNACKBAR_ID); // Dismiss after clicking
}, [hideSnackbar]);
const handleDismissClick = useCallback(() => {
if (window.gtag) {
window.gtag("event", "snackbar.googleSignIn.dismissed");
}
setDismissed();
hideSnackbar(SNACKBAR_ID);
}, [hideSnackbar]);
useEffect(() => {
// Logic from SnackbarManager constructor and GOOGLE_SIGN_IN Kind
// No shouldResetShowCount for this kind in original code.
const isDismissed = getDismissed();
const currentInstanceShowCount = getShownCount();
if (!isDismissed && customShowLogic() && currentInstanceShowCount < MAX_SHOW_COUNT) {
// Delay incrementing the shown count for this specific snackbar instance
setTimeout(() => incrementShownCount(), 5 * 1000);
const useHebrew = localStorage.languageOption === "hebrew";
// Message and buttons need to be defined. The original code doesn't specify
// the message for this snackbar directly in the `snackbars.googleSignIn.show(...)` call
// as it was a "startup" snackbar. We need to define a default message/buttons.
// Assuming a generic message for now. This should be reviewed for actual content.
const message = useHebrew ? "התחבר עם גוגל לחוויה טובה יותר" : "Sign in with Google for a better experience";
const snackbarConfig: SnackbarConfig = {
id: SNACKBAR_ID,
message: message,
buttons: [
{
text: useHebrew ? "התחבר" : "Sign In",
onClick: handleSignInClick,
},
{
text: useHebrew ? "סגור" : "Dismiss",
onClick: handleDismissClick,
},
],
kind: "google-sign-in",
autoHideDuration: 15000, // Example: 15 seconds
};
showSnackbar(snackbarConfig);
}
}, [showSnackbar, handleSignInClick, handleDismissClick]);
return null; // This component is a trigger
}