@thecb/components 11.3.2-beta.1 → 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/package.json
CHANGED
|
@@ -3,8 +3,7 @@ import React, {
|
|
|
3
3
|
useEffect,
|
|
4
4
|
useRef,
|
|
5
5
|
forwardRef,
|
|
6
|
-
useImperativeHandle
|
|
7
|
-
useCallback
|
|
6
|
+
useImperativeHandle
|
|
8
7
|
} from "react";
|
|
9
8
|
import { Box } from "../../atoms/layouts";
|
|
10
9
|
import Text from "../../atoms/text";
|
|
@@ -58,8 +57,7 @@ const TurnstileWidget = forwardRef(
|
|
|
58
57
|
tabindex = 0,
|
|
59
58
|
retry = "never",
|
|
60
59
|
theme = "light",
|
|
61
|
-
extraStyles = ""
|
|
62
|
-
maxErrorRetries = 3
|
|
60
|
+
extraStyles = ""
|
|
63
61
|
},
|
|
64
62
|
ref
|
|
65
63
|
) => {
|
|
@@ -67,62 +65,44 @@ const TurnstileWidget = forwardRef(
|
|
|
67
65
|
|
|
68
66
|
const widgetRef = useRef(null);
|
|
69
67
|
const widgetIdRef = useRef(null);
|
|
70
|
-
const retryCountRef = useRef(0);
|
|
71
68
|
const [error, setError] = useState(null);
|
|
72
69
|
|
|
73
70
|
const { scriptLoaded, scriptError } = useTurnstileScript(verifyURL);
|
|
74
71
|
|
|
75
|
-
// Clear the error state
|
|
72
|
+
// Clear the error state
|
|
76
73
|
const clearError = () => {
|
|
77
74
|
setError(null);
|
|
78
|
-
retryCountRef.current = 0;
|
|
79
75
|
};
|
|
80
76
|
|
|
81
77
|
// Handle errors based on the retry strategy
|
|
82
78
|
const handleError = errorCode => {
|
|
83
|
-
|
|
84
|
-
const currentRetryCount = retryCountRef.current;
|
|
85
|
-
|
|
86
|
-
// If we haven't exceeded max retries, reset and try again
|
|
87
|
-
if (currentRetryCount <= maxErrorRetries) {
|
|
88
|
-
window.turnstile.reset(widgetIdRef.current);
|
|
89
|
-
setError(null);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Max retries exceeded - show appropriate error message
|
|
94
|
-
if (errorCode) {
|
|
95
|
-
const errorMessage = getErrorMessage(errorCode);
|
|
96
|
-
setError(errorMessage);
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const getErrorMessage = errorCode => {
|
|
79
|
+
if (!errorCode) return;
|
|
101
80
|
const isFatalError = FATAL_ERROR_CODES.some(pattern =>
|
|
102
81
|
pattern.test(errorCode)
|
|
103
82
|
);
|
|
83
|
+
const errorMessage = getErrorMessage(isFatalError);
|
|
84
|
+
setError(errorMessage);
|
|
85
|
+
};
|
|
104
86
|
|
|
87
|
+
const getErrorMessage = isFatalError => {
|
|
105
88
|
if (isFatalError) {
|
|
106
|
-
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.";
|
|
107
90
|
}
|
|
108
91
|
return "Something went wrong. Please refresh and try again.";
|
|
109
92
|
};
|
|
110
93
|
|
|
111
|
-
// Reset the Turnstile widget when requested
|
|
112
|
-
const resetWidget = useCallback(() => {
|
|
113
|
-
if (widgetIdRef.current && window.turnstile) {
|
|
114
|
-
window.turnstile.reset(widgetIdRef.current);
|
|
115
|
-
clearError();
|
|
116
|
-
}
|
|
117
|
-
}, [clearError]);
|
|
118
|
-
|
|
119
94
|
// Allow the parent component to reset the Turnstile widget
|
|
120
95
|
useImperativeHandle(
|
|
121
96
|
ref,
|
|
122
97
|
() => ({
|
|
123
|
-
reset:
|
|
98
|
+
reset: () => {
|
|
99
|
+
if (widgetIdRef.current && window.turnstile) {
|
|
100
|
+
window.turnstile.reset(widgetIdRef.current);
|
|
101
|
+
clearError();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
124
104
|
}),
|
|
125
|
-
[
|
|
105
|
+
[]
|
|
126
106
|
);
|
|
127
107
|
|
|
128
108
|
useEffect(() => {
|
|
@@ -130,7 +110,7 @@ const TurnstileWidget = forwardRef(
|
|
|
130
110
|
setError(
|
|
131
111
|
"Unable to load security verification. Please refresh the page."
|
|
132
112
|
);
|
|
133
|
-
onError?.(
|
|
113
|
+
onError?.("script_load_failed");
|
|
134
114
|
return;
|
|
135
115
|
}
|
|
136
116
|
|
|
@@ -145,12 +125,10 @@ const TurnstileWidget = forwardRef(
|
|
|
145
125
|
theme: theme,
|
|
146
126
|
callback: token => {
|
|
147
127
|
clearError();
|
|
148
|
-
onSuccess(token);
|
|
128
|
+
onSuccess?.(token);
|
|
149
129
|
},
|
|
150
130
|
"error-callback": errorCode => {
|
|
151
|
-
|
|
152
|
-
handleError(errorCode);
|
|
153
|
-
}
|
|
131
|
+
handleError(errorCode);
|
|
154
132
|
onError?.(errorCode);
|
|
155
133
|
},
|
|
156
134
|
"expired-callback": () => {
|
|
@@ -162,7 +140,7 @@ const TurnstileWidget = forwardRef(
|
|
|
162
140
|
setError(
|
|
163
141
|
"Unable to load security verification. Please refresh the page."
|
|
164
142
|
);
|
|
165
|
-
onError?.(
|
|
143
|
+
onError?.("render_failed");
|
|
166
144
|
}
|
|
167
145
|
|
|
168
146
|
return () => {
|
|
@@ -172,7 +150,7 @@ const TurnstileWidget = forwardRef(
|
|
|
172
150
|
clearError();
|
|
173
151
|
}
|
|
174
152
|
};
|
|
175
|
-
}, [scriptLoaded, siteKey]);
|
|
153
|
+
}, [scriptLoaded, scriptError, siteKey]);
|
|
176
154
|
|
|
177
155
|
return (
|
|
178
156
|
<>
|
|
@@ -190,6 +168,7 @@ const TurnstileWidget = forwardRef(
|
|
|
190
168
|
extraStyles={`
|
|
191
169
|
word-break: break-word;
|
|
192
170
|
text-align: end;
|
|
171
|
+
padding-top: 0.5rem;
|
|
193
172
|
`}
|
|
194
173
|
>
|
|
195
174
|
{error}
|