organism-react-i13n
Version:
168 lines (143 loc) • 4.67 kB
JavaScript
import _objectWithoutPropertiesLoose from "reshow-runtime/es/helpers/objectWithoutPropertiesLoose";
var _MonitorPvidContainer, _MonitorBrowserBFCont;
var _excluded = ["I18N"];
import React, { useRef, useMemo, useState, useEffect } from "react";
import { Return } from "reshow";
import { SemanticUI, Unsafe } from "react-atomic-molecule";
import Iframe from "organism-react-iframe";
import { ajaxStore } from "organism-react-ajax";
import get from "get-object-value";
import { win } from "win-doc";
import windowOnload from "window-onload";
import { getTimestamp } from "get-random-id";
import { usePrevious } from "reshow-hooks";
import i13nStore from "../../src/stores/i13nStore.mjs";
import { i13nDispatch } from "../../src/index.mjs";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var _windowOnload = windowOnload(),
process = _windowOnload[0],
close = _windowOnload[1];
var urlDecode = decodeURIComponent;
var MonitorPvidContainer = function MonitorPvidContainer(props) {
var pvid = useRef();
return /*#__PURE__*/_jsx(Return, {
initStates: ["data", "pvid"],
children: function children(_ref) {
var nextPvid = _ref.pvid,
I13N = _ref.I13N;
if (pvid.current !== nextPvid) {
pvid.current = nextPvid;
i13nDispatch("config/set", {
pvid: nextPvid,
I13N: I13N
});
}
return null;
}
});
};
var MonitorBrowserBFContainer = function MonitorBrowserBFContainer(_ref2) {
var nextBfApplyUrl = _ref2.bfApplyUrl,
nextToggleBfChange = _ref2.toggleBfChange;
var prevToggleBfChange = usePrevious(nextToggleBfChange);
var bfApplyUrl = useRef();
if (prevToggleBfChange !== nextToggleBfChange) {
i13nDispatch("action", {
I13N: {
action: "bfChange",
before: urlDecode(get(i13nStore.getState().get("lastUrl"), null, "")),
after: urlDecode(nextBfApplyUrl),
last: urlDecode(get(bfApplyUrl.current, null, ""))
}
});
bfApplyUrl.current = nextBfApplyUrl;
}
return null;
};
var handleIframe = function handleIframe(iframe) {
i13nDispatch("config/set", {
iframe: iframe
});
};
var I13nElement = function I13nElement(props) {
var _Unsafe;
var _useState = useState(),
iframe = _useState[0],
setIframe = _useState[1];
var _useState2 = useState(),
isLoad = _useState2[0],
setIsLoad = _useState2[1];
var pvid = useRef(true);
var oWin = win();
useEffect(function () {
if (!oWin.__null) {
var I18N = props.I18N,
otherProps = _objectWithoutPropertiesLoose(props, _excluded);
oWin.i13nDispatch = i13nDispatch;
i13nDispatch("config/set", otherProps);
setIsLoad(true);
}
return function () {
close();
};
}, []);
return useMemo(function () {
if (!isLoad) {
return null;
}
var dIframe;
if (iframe) {
dIframe = /*#__PURE__*/_jsx(Iframe, {
style: Styles.iframe,
ref: handleIframe,
children: _Unsafe || (_Unsafe = /*#__PURE__*/_jsx(Unsafe, {
children: iframe
}))
});
}
return /*#__PURE__*/_jsxs(SemanticUI, {
children: [/*#__PURE__*/_jsx(Return, {
initStates: ["pvid", "I13N"],
store: i13nStore,
children: function children(_ref3) {
var nextPvid = _ref3.pvid,
I13N = _ref3.I13N;
if (pvid.current !== nextPvid) {
pvid.current = nextPvid;
var query = {};
if (oWin.startUpTime) {
query.sp = Math.round(getTimestamp() - oWin.startUpTime);
oWin.startUpTime = false; //only log in page refresh
}
setTimeout(function () {
return process(function () {
return i13nDispatch("impression", {
query: query,
I13N: get(I13N),
callback: function callback(json, text) {
return setIframe(text);
}
});
});
});
}
return null;
}
}), _MonitorPvidContainer || (_MonitorPvidContainer = /*#__PURE__*/_jsx(MonitorPvidContainer, {})), /*#__PURE__*/_jsx(Return, {
initStates: ["bfApplyUrl", "toggleBfChange"],
store: ajaxStore,
children: _MonitorBrowserBFCont || (_MonitorBrowserBFCont = /*#__PURE__*/_jsx(MonitorBrowserBFContainer, {}))
}), dIframe]
});
}, [iframe, isLoad]);
};
export default I13nElement;
var Styles = {
iframe: {
width: 1,
height: 1,
position: "absolute",
top: -99999
}
};