@yandex/smart-captcha
Advanced tools
Comparing version 2.2.0 to 2.4.8
@@ -12,2 +12,4 @@ import { FC } from 'react'; | ||
} | { | ||
event: 'token-expired'; | ||
} | { | ||
event: 'success'; | ||
@@ -59,8 +61,4 @@ data: string; | ||
/** | ||
* @internal | ||
*/ | ||
declare const callbacks: Array<() => void>; | ||
declare function useSmartCaptchaLoader(): SmartCaptchaApi | undefined; | ||
export { InvisibleSmartCaptcha, InvisibleSmartCaptchaProps, SmartCaptcha, SmartCaptchaProps, callbacks, useSmartCaptchaLoader }; | ||
export { InvisibleSmartCaptcha, InvisibleSmartCaptchaProps, SmartCaptcha, SmartCaptchaProps, useSmartCaptchaLoader }; |
137
index.js
@@ -114,9 +114,59 @@ 'use strict'; | ||
function useSubscriptions(props) { | ||
var propsRef = React.useRef({}); | ||
var widgetId = props.widgetId, smartCaptcha = props.smartCaptcha, onChallengeHidden = props.onChallengeHidden, onChallengeVisible = props.onChallengeVisible, onSuccess = props.onSuccess, onNetworkError = props.onNetworkError, onTokenExpired = props.onTokenExpired; | ||
propsRef.current = { | ||
onChallengeHidden: onChallengeHidden, | ||
onChallengeVisible: onChallengeVisible, | ||
onSuccess: onSuccess, | ||
onNetworkError: onNetworkError, | ||
onTokenExpired: onTokenExpired, | ||
}; | ||
React.useEffect(function () { | ||
if (!smartCaptcha || typeof widgetId !== 'number') { | ||
return; | ||
} | ||
var unsubscribeFns = [ | ||
smartCaptcha.subscribe(widgetId, 'challenge-visible', function () { | ||
var _a, _b; | ||
(_b = (_a = propsRef.current).onChallengeVisible) === null || _b === void 0 ? void 0 : _b.call(_a); | ||
}), | ||
smartCaptcha.subscribe(widgetId, 'challenge-hidden', function () { | ||
var _a, _b; | ||
(_b = (_a = propsRef.current).onChallengeHidden) === null || _b === void 0 ? void 0 : _b.call(_a); | ||
}), | ||
smartCaptcha.subscribe(widgetId, 'network-error', function () { | ||
var _a, _b; | ||
(_b = (_a = propsRef.current).onNetworkError) === null || _b === void 0 ? void 0 : _b.call(_a); | ||
}), | ||
// @ts-expect-error Subscribe types has no success method signature | ||
smartCaptcha.subscribe(widgetId, 'success', function (token) { | ||
var _a, _b; | ||
(_b = (_a = propsRef.current).onSuccess) === null || _b === void 0 ? void 0 : _b.call(_a, token); | ||
}), | ||
smartCaptcha.subscribe(widgetId, 'token-expired', function () { | ||
var _a, _b; | ||
(_b = (_a = propsRef.current).onTokenExpired) === null || _b === void 0 ? void 0 : _b.call(_a); | ||
}), | ||
]; | ||
return function () { return unsubscribeFns.forEach(function (fn) { return fn === null || fn === void 0 ? void 0 : fn(); }); }; | ||
}, [widgetId, smartCaptcha]); | ||
} | ||
var style = { height: 102 }; | ||
var SmartCaptcha = function (props) { | ||
var containerRef = React.useRef(null); | ||
var onChallengeHidden = props.onChallengeHidden, onChallengeVisible = props.onChallengeVisible, onSuccess = props.onSuccess, onNetworkError = props.onNetworkError, language = props.language, restProps = __rest(props, ["onChallengeHidden", "onChallengeVisible", "onSuccess", "onNetworkError", "language"]); | ||
var onChallengeHidden = props.onChallengeHidden, onChallengeVisible = props.onChallengeVisible, onSuccess = props.onSuccess, onNetworkError = props.onNetworkError, onTokenExpired = props.onTokenExpired, language = props.language, restProps = __rest(props, ["onChallengeHidden", "onChallengeVisible", "onSuccess", "onNetworkError", "onTokenExpired", "language"]); | ||
var smartCaptcha = useSmartCaptchaLoader(); | ||
var _a = __read(React.useState(), 2), widgetId = _a[0], setWidgetId = _a[1]; | ||
var destroyedRef = React.useRef({}); | ||
useSubscriptions({ | ||
widgetId: widgetId, | ||
smartCaptcha: smartCaptcha, | ||
onChallengeHidden: onChallengeHidden, | ||
onChallengeVisible: onChallengeVisible, | ||
onSuccess: onSuccess, | ||
onNetworkError: onNetworkError, | ||
onTokenExpired: onTokenExpired, | ||
}); | ||
React.useLayoutEffect(function () { | ||
@@ -136,39 +186,2 @@ if (!containerRef.current || !smartCaptcha) { | ||
}, [smartCaptcha, language, restProps.sitekey, restProps.test, restProps.webview]); | ||
React.useEffect(function () { | ||
if (!onChallengeVisible || | ||
!smartCaptcha || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
return smartCaptcha.subscribe(widgetId, 'challenge-visible', onChallengeVisible); | ||
}, [onChallengeVisible, widgetId, smartCaptcha]); | ||
React.useEffect(function () { | ||
if (!onChallengeHidden || | ||
!smartCaptcha || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
return smartCaptcha.subscribe(widgetId, 'challenge-hidden', onChallengeHidden); | ||
}, [widgetId, smartCaptcha, onChallengeHidden]); | ||
React.useEffect(function () { | ||
if (!onNetworkError || | ||
!smartCaptcha || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
return smartCaptcha.subscribe(widgetId, 'network-error', onNetworkError); | ||
}, [onNetworkError, widgetId, smartCaptcha]); | ||
React.useEffect(function () { | ||
if (!onSuccess || | ||
!smartCaptcha || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
// @ts-expect-error subscribe types has no success method signature | ||
return smartCaptcha.subscribe(widgetId, 'success', onSuccess); | ||
}, [onSuccess, widgetId, smartCaptcha]); | ||
return React.createElement("div", { ref: containerRef, className: "smart-captcha", style: style }); | ||
@@ -179,6 +192,15 @@ }; | ||
var containerRef = React.useRef(null); | ||
var onChallengeHidden = props.onChallengeHidden, onChallengeVisible = props.onChallengeVisible, onSuccess = props.onSuccess, onNetworkError = props.onNetworkError, language = props.language, restProps = __rest(props, ["onChallengeHidden", "onChallengeVisible", "onSuccess", "onNetworkError", "language"]); | ||
var onChallengeHidden = props.onChallengeHidden, onChallengeVisible = props.onChallengeVisible, onSuccess = props.onSuccess, onNetworkError = props.onNetworkError, onTokenExpired = props.onTokenExpired, language = props.language, restProps = __rest(props, ["onChallengeHidden", "onChallengeVisible", "onSuccess", "onNetworkError", "onTokenExpired", "language"]); | ||
var smartCaptcha = useSmartCaptchaLoader(); | ||
var _a = __read(React.useState(), 2), widgetId = _a[0], setWidgetId = _a[1]; | ||
var destroyedRef = React.useRef({}); | ||
useSubscriptions({ | ||
widgetId: widgetId, | ||
smartCaptcha: smartCaptcha, | ||
onChallengeHidden: onChallengeHidden, | ||
onChallengeVisible: onChallengeVisible, | ||
onSuccess: onSuccess, | ||
onNetworkError: onNetworkError, | ||
onTokenExpired: onTokenExpired, | ||
}); | ||
React.useLayoutEffect(function () { | ||
@@ -214,39 +236,2 @@ if (!containerRef.current || !smartCaptcha) { | ||
}, [smartCaptcha, restProps.visible, widgetId]); | ||
React.useEffect(function () { | ||
if (!smartCaptcha || | ||
!onChallengeVisible || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
return smartCaptcha.subscribe(widgetId, 'challenge-visible', onChallengeVisible); | ||
}, [onChallengeVisible, widgetId, smartCaptcha]); | ||
React.useEffect(function () { | ||
if (!smartCaptcha || | ||
!onChallengeHidden || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
return smartCaptcha.subscribe(widgetId, 'challenge-hidden', onChallengeHidden); | ||
}, [widgetId, smartCaptcha, onChallengeHidden]); | ||
React.useEffect(function () { | ||
if (!smartCaptcha || | ||
!onNetworkError || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
return smartCaptcha.subscribe(widgetId, 'network-error', onNetworkError); | ||
}, [onNetworkError, widgetId, smartCaptcha]); | ||
React.useEffect(function () { | ||
if (!smartCaptcha || | ||
!onSuccess || | ||
typeof widgetId !== 'number' || | ||
destroyedRef.current[widgetId]) { | ||
return; | ||
} | ||
// @ts-expect-error Subscribe types has no success method signature | ||
return smartCaptcha.subscribe(widgetId, 'success', onSuccess); | ||
}, [onSuccess, widgetId, smartCaptcha]); | ||
return React.createElement("div", { ref: containerRef, className: "smart-captcha" }); | ||
@@ -253,0 +238,0 @@ }; |
{ | ||
"name": "@yandex/smart-captcha", | ||
"version": "2.2.0", | ||
"version": "2.4.8", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "module": "module.mjs", |
Sorry, the diff of this file is not supported yet
23822
506