@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
116 lines (94 loc) • 3.25 kB
text/typescript
import * as R from "ramda";
import {
ANALYTICS_COMPONENT_EVENTS,
ANALYTICS_CORE_EVENTS,
ANALYTICS_ENTRY_EVENTS,
DOWNLOADS_EVENTS,
} from "../events";
import { isEmptyOrNil } from "../../cellUtils";
export enum OfflineItemState {
notExist = "NOT_EXISTS",
preparing = "PREPARING",
inProgress = "IN_PROGRESS",
error = "ERROR",
completed = "COMPLETED",
expired = "EXPIRED",
}
function assertUnreachable(): never {
throw new Error("Didn't expect to get here");
}
export const downloadsEventForState = (state: OfflineItemState): string => {
switch (state) {
case OfflineItemState.preparing:
return DOWNLOADS_EVENTS.download_start;
case OfflineItemState.inProgress:
case OfflineItemState.expired:
return null;
case OfflineItemState.completed:
return DOWNLOADS_EVENTS.download_success;
case OfflineItemState.error:
return DOWNLOADS_EVENTS.download_error;
case OfflineItemState.notExist:
return DOWNLOADS_EVENTS.download_delete;
}
return assertUnreachable();
};
export const extensionsEvents = (extensions) => {
const customProperties = extensions?.analyticsCustomProperties;
if (R.isNil(customProperties) || R.isEmpty(customProperties)) {
return null;
}
return JSON.stringify(customProperties);
};
export const replaceAnalyticsPropsNils = (analyticsProps): any => {
return Object.keys(analyticsProps).reduce((acc, key) => {
if (isEmptyOrNil(analyticsProps[key])) {
acc[key] = "N/A";
} else {
acc[key] = analyticsProps[key];
}
return acc;
}, {});
};
export function eventForEntry(item, itemIndex = null) {
const { title, id, position } = item;
const valueType = item?.type?.value;
let analyticsProps = {
[ANALYTICS_ENTRY_EVENTS.ITEM_TYPE]: valueType || item?.type,
[ANALYTICS_ENTRY_EVENTS.ITEM_TITLE]: title,
[ANALYTICS_ENTRY_EVENTS.ITEM_ID]: id,
[ANALYTICS_ENTRY_EVENTS.ITEM_INDEX]: itemIndex || position || null,
};
analyticsProps = replaceAnalyticsPropsNils(analyticsProps);
return analyticsProps;
}
export function eventForComponent(
component,
headerTitle,
// Zapp Pipes data passed for group components
zappPipesData: any = null
) {
const { id, component_type, styles, data } = component;
const { cell_plugin_configuration_id = null } = styles;
const source = data?.source || zappPipesData?.data?.url || null;
let analyticsProps = {
[ANALYTICS_COMPONENT_EVENTS.COMPONENT_ID]: id,
[ANALYTICS_COMPONENT_EVENTS.COMPONENT_TYPE]: component_type,
[ANALYTICS_COMPONENT_EVENTS.CELL_STYLE]:
cell_plugin_configuration_id || ANALYTICS_CORE_EVENTS.ITEM_NOT_AVAILABLE,
[ANALYTICS_COMPONENT_EVENTS.HEADER_TITLE]: headerTitle,
[ANALYTICS_COMPONENT_EVENTS.COMPONENT_SOURCE]: source,
};
analyticsProps = replaceAnalyticsPropsNils(analyticsProps);
return analyticsProps;
}
export function playEventForType(item) {
const itemType = item?.type && item.type?.value;
if (itemType === "program") {
return ANALYTICS_CORE_EVENTS.PROGRAM_ITEM_PLAY_WAS_TRIGGERED;
} else if (itemType === "channel") {
return ANALYTICS_CORE_EVENTS.CHANNEL_ITEM_PLAY_WAS_TRIGGERED;
} else {
return ANALYTICS_CORE_EVENTS.VOD_ITEM_PLAY_WAS_TRIGGERED;
}
}