@roderickhsiao/react-i13n
Version:
[Experiment] React I13n provides a performant and scalable solution to application instrumentation.
63 lines (54 loc) • 2.24 kB
JavaScript
/**
* Copyright 2015 - Present, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import { useCallback, useRef, useState } from 'react';
import ReactI13n from '../core/ReactI13n';
import { IS_CLIENT, IS_PROD } from '../utils/variables';
import warnAndPrintTrace from '../utils/warnAndPrintTrace';
/*
* @param {Object} options passed into ReactI13n
* @param {Boolean} options.isViewportEnabled if enable viewport checking
* @param {Object} options.displayName display name of the wrapper component
* @param {Object} options.i13nNodeClass the i13nNode class, you can inherit it with your own functionalities
* @param {Object} options.rootModelData model data of root i13n node
*/
var useReactI13n = options => {
var i13nInstance = useRef();
var [instance, setInstance] = useState();
var setupI13nRoot = () => {
if (!i13nInstance.current) {
var reactI13n = new ReactI13n(options);
reactI13n.i13nInstance = reactI13n;
i13nInstance.current = reactI13n;
setInstance(reactI13n);
if (IS_CLIENT && !IS_PROD) {
window._reactI13nInstance = reactI13n;
}
}
};
var executeI13nEvent = useCallback(function (eventName) {
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var callback = arguments.length > 2 ? arguments[2] : undefined;
var errorMessage = ''; // payload.i13nNode = payload.i13nNode || this.getI13nNode();
if (instance) {
instance.execute(eventName, payload, callback);
} else {
/* istanbul ignore next */
if (!IS_PROD) {
errorMessage = 'ReactI13n instance is not found, please make sure you have setupI13n on the root component. ';
if (!IS_CLIENT) {
errorMessage += 'On server side, ' + 'you can only execute the i13n event on the components under setupI13n, ' + 'please make sure you are calling executeI13nEvent correctly';
}
warnAndPrintTrace(errorMessage);
}
callback === null || callback === void 0 ? void 0 : callback();
}
}, [instance]);
setupI13nRoot();
return {
i13nInstance: i13nInstance.current,
executeI13nEvent
};
};
export default useReactI13n;