2020-08-28 12:09:46 +00:00
|
|
|
import { WOOT_PREFIX } from './constants';
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
export const isEmptyObject = obj =>
|
|
|
|
Object.keys(obj).length === 0 && obj.constructor === Object;
|
|
|
|
|
2022-06-23 10:18:56 +00:00
|
|
|
export const sendMessage = msg => {
|
|
|
|
window.parent.postMessage(
|
|
|
|
`chatwoot-widget:${JSON.stringify({ ...msg })}`,
|
|
|
|
'*'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-01-17 08:06:05 +00:00
|
|
|
export const IFrameHelper = {
|
|
|
|
isIFrame: () => window.self !== window.top,
|
2022-06-23 10:18:56 +00:00
|
|
|
sendMessage,
|
2020-08-28 12:09:46 +00:00
|
|
|
isAValidEvent: e => {
|
|
|
|
const isDataAString = typeof e.data === 'string';
|
2022-10-14 03:43:11 +00:00
|
|
|
return isDataAString && e.data.indexOf(WOOT_PREFIX) === 0;
|
2020-08-28 12:09:46 +00:00
|
|
|
},
|
|
|
|
getMessage: e => JSON.parse(e.data.replace(WOOT_PREFIX, '')),
|
2020-01-17 08:06:05 +00:00
|
|
|
};
|
2021-03-15 17:10:26 +00:00
|
|
|
export const RNHelper = {
|
|
|
|
isRNWebView: () => window.ReactNativeWebView,
|
|
|
|
sendMessage: msg => {
|
|
|
|
window.ReactNativeWebView.postMessage(
|
|
|
|
`chatwoot-widget:${JSON.stringify({ ...msg })}`
|
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
2022-08-03 11:38:21 +00:00
|
|
|
|
|
|
|
export const groupBy = (array, predicate) => {
|
|
|
|
return array.reduce((acc, value) => {
|
|
|
|
(acc[predicate(value)] ||= []).push(value);
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|