@thecb/components 11.3.2-beta.0 → 11.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +52 -58
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +9 -5
- package/dist/index.esm.js +52 -58
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/molecules/turnstile-widget/TurnstileWidget.js +45 -58
- package/src/hooks/use-turnstile-script/index.js +9 -5
package/package.json
CHANGED
|
@@ -15,10 +15,9 @@ import { ERROR_COLOR } from "../../../constants/colors";
|
|
|
15
15
|
const TurnstileWidgetContainer = styled(Box)`
|
|
16
16
|
display: flex;
|
|
17
17
|
flex-direction: column;
|
|
18
|
-
align-items: flex-end;
|
|
19
18
|
padding: ${({ padding }) => padding};
|
|
20
19
|
justify-content: ${({ justify }) => justify};
|
|
21
|
-
align-items: ${({
|
|
20
|
+
align-items: ${({ align }) => align};
|
|
22
21
|
width: 100%;
|
|
23
22
|
`;
|
|
24
23
|
|
|
@@ -53,12 +52,12 @@ const TurnstileWidget = forwardRef(
|
|
|
53
52
|
onExpired = noop,
|
|
54
53
|
padding = "1rem",
|
|
55
54
|
justify = "flex-end",
|
|
55
|
+
align = "flex-end",
|
|
56
56
|
size = "normal",
|
|
57
57
|
tabindex = 0,
|
|
58
58
|
retry = "never",
|
|
59
59
|
theme = "light",
|
|
60
|
-
extraStyles = ""
|
|
61
|
-
maxErrorRetries = 3
|
|
60
|
+
extraStyles = ""
|
|
62
61
|
},
|
|
63
62
|
ref
|
|
64
63
|
) => {
|
|
@@ -66,43 +65,28 @@ const TurnstileWidget = forwardRef(
|
|
|
66
65
|
|
|
67
66
|
const widgetRef = useRef(null);
|
|
68
67
|
const widgetIdRef = useRef(null);
|
|
69
|
-
const retryCountRef = useRef(0);
|
|
70
68
|
const [error, setError] = useState(null);
|
|
71
69
|
|
|
72
70
|
const { scriptLoaded, scriptError } = useTurnstileScript(verifyURL);
|
|
73
71
|
|
|
74
|
-
// Clear the error state
|
|
72
|
+
// Clear the error state
|
|
75
73
|
const clearError = () => {
|
|
76
74
|
setError(null);
|
|
77
|
-
retryCountRef.current = 0;
|
|
78
75
|
};
|
|
79
76
|
|
|
80
77
|
// Handle errors based on the retry strategy
|
|
81
78
|
const handleError = errorCode => {
|
|
82
|
-
|
|
83
|
-
const currentRetryCount = retryCountRef.current;
|
|
84
|
-
|
|
85
|
-
// If we haven't exceeded max retries, reset and try again
|
|
86
|
-
if (currentRetryCount <= maxErrorRetries) {
|
|
87
|
-
window.turnstile.reset(widgetIdRef.current);
|
|
88
|
-
setError(null);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Max retries exceeded - show appropriate error message
|
|
93
|
-
if (errorCode) {
|
|
94
|
-
const errorMessage = getErrorMessage(errorCode);
|
|
95
|
-
setError(errorMessage);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const getErrorMessage = errorCode => {
|
|
79
|
+
if (!errorCode) return;
|
|
100
80
|
const isFatalError = FATAL_ERROR_CODES.some(pattern =>
|
|
101
81
|
pattern.test(errorCode)
|
|
102
82
|
);
|
|
83
|
+
const errorMessage = getErrorMessage(isFatalError);
|
|
84
|
+
setError(errorMessage);
|
|
85
|
+
};
|
|
103
86
|
|
|
87
|
+
const getErrorMessage = isFatalError => {
|
|
104
88
|
if (isFatalError) {
|
|
105
|
-
return "Browser or system error. Please refresh the page or
|
|
89
|
+
return "Browser or system error. Please refresh the page or try a different browser.";
|
|
106
90
|
}
|
|
107
91
|
return "Something went wrong. Please refresh and try again.";
|
|
108
92
|
};
|
|
@@ -122,29 +106,42 @@ const TurnstileWidget = forwardRef(
|
|
|
122
106
|
);
|
|
123
107
|
|
|
124
108
|
useEffect(() => {
|
|
109
|
+
if (scriptError) {
|
|
110
|
+
setError(
|
|
111
|
+
"Unable to load security verification. Please refresh the page."
|
|
112
|
+
);
|
|
113
|
+
onError?.("script_load_failed");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
125
117
|
if (widgetIdRef.current || !window.turnstile || !scriptLoaded) return;
|
|
126
118
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
119
|
+
try {
|
|
120
|
+
widgetIdRef.current = window.turnstile.render(widgetRef.current, {
|
|
121
|
+
sitekey: siteKey,
|
|
122
|
+
size: size,
|
|
123
|
+
retry: retry,
|
|
124
|
+
tabindex: tabindex,
|
|
125
|
+
theme: theme,
|
|
126
|
+
callback: token => {
|
|
127
|
+
clearError();
|
|
128
|
+
onSuccess?.(token);
|
|
129
|
+
},
|
|
130
|
+
"error-callback": errorCode => {
|
|
139
131
|
handleError(errorCode);
|
|
132
|
+
onError?.(errorCode);
|
|
133
|
+
},
|
|
134
|
+
"expired-callback": () => {
|
|
135
|
+
clearError();
|
|
136
|
+
onExpired?.();
|
|
140
137
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
138
|
+
});
|
|
139
|
+
} catch (error) {
|
|
140
|
+
setError(
|
|
141
|
+
"Unable to load security verification. Please refresh the page."
|
|
142
|
+
);
|
|
143
|
+
onError?.("render_failed");
|
|
144
|
+
}
|
|
148
145
|
|
|
149
146
|
return () => {
|
|
150
147
|
if (widgetIdRef.current && window.turnstile) {
|
|
@@ -153,13 +150,14 @@ const TurnstileWidget = forwardRef(
|
|
|
153
150
|
clearError();
|
|
154
151
|
}
|
|
155
152
|
};
|
|
156
|
-
}, [scriptLoaded, siteKey]);
|
|
153
|
+
}, [scriptLoaded, scriptError, siteKey]);
|
|
157
154
|
|
|
158
155
|
return (
|
|
159
156
|
<>
|
|
160
157
|
<TurnstileWidgetContainer
|
|
161
158
|
padding={padding}
|
|
162
159
|
justify={justify}
|
|
160
|
+
align={align}
|
|
163
161
|
extraStyles={extraStyles}
|
|
164
162
|
>
|
|
165
163
|
<div ref={widgetRef} />
|
|
@@ -170,23 +168,12 @@ const TurnstileWidget = forwardRef(
|
|
|
170
168
|
extraStyles={`
|
|
171
169
|
word-break: break-word;
|
|
172
170
|
text-align: end;
|
|
171
|
+
padding-top: 0.5rem;
|
|
173
172
|
`}
|
|
174
173
|
>
|
|
175
174
|
{error}
|
|
176
175
|
</Text>
|
|
177
176
|
)}
|
|
178
|
-
{scriptError && (
|
|
179
|
-
<Text
|
|
180
|
-
color={ERROR_COLOR}
|
|
181
|
-
variant="pXS"
|
|
182
|
-
extraStyles={`
|
|
183
|
-
word-break: break-word;
|
|
184
|
-
text-align: end;
|
|
185
|
-
`}
|
|
186
|
-
>
|
|
187
|
-
Unable to load security verification. Please refresh the page.
|
|
188
|
-
</Text>
|
|
189
|
-
)}
|
|
190
177
|
</TurnstileWidgetContainer>
|
|
191
178
|
</>
|
|
192
179
|
);
|
|
@@ -7,7 +7,12 @@ import { useEffect, useState } from "react";
|
|
|
7
7
|
*/
|
|
8
8
|
const useTurnstileScript = verifyURL => {
|
|
9
9
|
const [scriptLoaded, setScriptLoaded] = useState(false);
|
|
10
|
-
const [scriptError, setScriptError] = useState(
|
|
10
|
+
const [scriptError, setScriptError] = useState(false);
|
|
11
|
+
|
|
12
|
+
const handleScriptError = () => {
|
|
13
|
+
setScriptError(true);
|
|
14
|
+
setScriptLoaded(false);
|
|
15
|
+
};
|
|
11
16
|
|
|
12
17
|
useEffect(() => {
|
|
13
18
|
if (typeof window === "undefined") {
|
|
@@ -25,18 +30,17 @@ const useTurnstileScript = verifyURL => {
|
|
|
25
30
|
setScriptLoaded(true);
|
|
26
31
|
};
|
|
27
32
|
script.onerror = () => {
|
|
28
|
-
|
|
29
|
-
setScriptLoaded(false);
|
|
33
|
+
handleScriptError();
|
|
30
34
|
};
|
|
31
35
|
script.onabort = () => {
|
|
32
|
-
|
|
33
|
-
setScriptLoaded(false);
|
|
36
|
+
handleScriptError();
|
|
34
37
|
};
|
|
35
38
|
script.defer = true;
|
|
36
39
|
document.getElementsByTagName("head")[0].appendChild(script);
|
|
37
40
|
|
|
38
41
|
return () => {
|
|
39
42
|
setScriptLoaded(false);
|
|
43
|
+
setScriptError(false);
|
|
40
44
|
};
|
|
41
45
|
}, [verifyURL]);
|
|
42
46
|
|