@livelike/react-native
Version:
LiveLike React Native package
94 lines • 2.55 kB
JavaScript
import React, { useEffect } from 'react';
import { StyleSheet, View, ActivityIndicator } from 'react-native';
import { useStyles, useLoadWidgetEffect, useWidgetDismiss, useAnalytics } from '../../hooks';
import { LLText } from '../LLText';
export function LLCoreWidget(_ref) {
let {
programId,
widgetId,
widgetKind,
LoadingComponent,
ErrorComponent,
children,
onDismiss,
styles: stylesProp
} = _ref;
const {
dismiss,
onDismissHandler
} = useWidgetDismiss({
widgetId,
onDismiss
});
const {
isLoading,
error,
data
} = useLoadWidgetEffect({
widgetId,
widgetKind,
programId
});
const styles = useStyles({
componentStylesFn: getWidgetStyles,
stylesProp
});
const widget = data === null || data === void 0 ? void 0 : data[0];
const {
trackEvent
} = useAnalytics();
useEffect(() => {
if (!isLoading && !error && widget !== null && widget !== void 0 && widget.id && !dismiss) {
trackEvent('Widget Displayed', {
programId,
widgetId: widget.id,
widgetKind: widget.kind
});
}
}, [isLoading, error, widget === null || widget === void 0 ? void 0 : widget.id, widget === null || widget === void 0 ? void 0 : widget.kind, dismiss, trackEvent]);
if (isLoading) {
return LoadingComponent ? /*#__PURE__*/React.createElement(LoadingComponent, null) : /*#__PURE__*/React.createElement(ActivityIndicator, {
size: 'small',
color: "blue"
});
}
if (error) {
return ErrorComponent ? /*#__PURE__*/React.createElement(ErrorComponent, null) : /*#__PURE__*/React.createElement(View, {
style: styles.widgetContainer
}, /*#__PURE__*/React.createElement(LLText, {
style: styles.errorText
}, 'Unable to load widget'));
}
if (!widget || dismiss) {
return undefined;
}
return /*#__PURE__*/React.createElement(View, {
style: styles.widgetContainer
}, children === null || children === void 0 ? void 0 : children({
widget,
onDismiss: onDismissHandler
}));
}
const getWidgetStyles = _ref2 => {
let {
theme
} = _ref2;
return StyleSheet.create({
widgetContainer: {
display: 'flex',
flexDirection: 'column',
backgroundColor: theme.widgetBackground,
borderRadius: 4,
overflow: 'hidden'
},
errorText: {
display: 'flex',
flex: 1,
height: 100,
textAlign: 'center',
textAlignVertical: 'center',
color: theme.error
}
});
};
//# sourceMappingURL=LLCoreWidget.js.map