91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
import React, { useEffect, useRef, useState } from "react";
|
|
import { View, findNodeHandle } from "react-native";
|
|
import Modal from "react-native-modal";
|
|
import { sendAsAnalyticsEvent } from "../../App/common/hooks/useAnalyticsEvent";
|
|
import { CtaData, CtaType } from "../../App/common/interface";
|
|
import { ModalView } from "../../App/common/interface/modals/ModalView";
|
|
import { GetModalView } from "../../App/common/modals/modalViewResolver";
|
|
import { NativeDeeplinkNavigatorModule } from "../../App/common/native-module/NativeModules";
|
|
import styles from "../../App/common/styles/BaseBottomSheetComponentStyles";
|
|
|
|
const BaseBottomSheetComponent = ({
|
|
onBottomSheetAnimationEnd,
|
|
showModal,
|
|
onClose,
|
|
modalView,
|
|
}: {
|
|
onBottomSheetAnimationEnd?: (id: number | null) => void;
|
|
showModal: boolean;
|
|
onClose: () => void;
|
|
modalView: ModalView | undefined;
|
|
}) => {
|
|
const [isModalVisible, setModalVisible] = useState(showModal);
|
|
const modalRef = useRef(null);
|
|
|
|
const closeModal = () => {
|
|
setModalVisible(false);
|
|
setTimeout(() => {
|
|
onClose();
|
|
}, 400);
|
|
};
|
|
|
|
const handleModalClick = (cta: CtaData) => {
|
|
switch (cta?.type) {
|
|
case CtaType.DISMISS_MODAL:
|
|
if (!!cta?.analyticsEventProperties) {
|
|
sendAsAnalyticsEvent(cta?.analyticsEventProperties);
|
|
}
|
|
closeModal();
|
|
break;
|
|
default:
|
|
try {
|
|
NativeDeeplinkNavigatorModule.navigateToNaviDeeplinkNavigator(
|
|
JSON.stringify(cta)
|
|
);
|
|
} catch (error) {
|
|
// #TODO: Handle the error gracefully using Sentry.
|
|
console.error("Error while navigating to deep link:", error);
|
|
}
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
setModalVisible(showModal);
|
|
if (typeof onBottomSheetAnimationEnd === "function") {
|
|
const nodeId = findNodeHandle(modalRef?.current);
|
|
onBottomSheetAnimationEnd(nodeId);
|
|
}
|
|
}, [showModal]);
|
|
|
|
return (
|
|
<Modal
|
|
onBackdropPress={() => {
|
|
closeModal();
|
|
}}
|
|
onBackButtonPress={() => {
|
|
closeModal();
|
|
}}
|
|
hasBackdrop={true}
|
|
backdropOpacity={0.65}
|
|
isVisible={isModalVisible}
|
|
swipeDirection="down"
|
|
onSwipeComplete={() => closeModal()}
|
|
onModalHide={() => onClose()}
|
|
animationIn="slideInUp"
|
|
animationOut="slideOutDown"
|
|
animationInTiming={400}
|
|
animationOutTiming={400}
|
|
useNativeDriverForBackdrop={true}
|
|
style={styles.modal}
|
|
>
|
|
<View ref={modalRef} style={styles.modalContent}>
|
|
{/* <View style={styles.barContainer}>
|
|
<Text style={styles.barIcon} />
|
|
</View> */}
|
|
{GetModalView.getModal(modalView!!, handleModalClick)}
|
|
</View>
|
|
</Modal>
|
|
);
|
|
};
|
|
|
|
export default BaseBottomSheetComponent; |