import { View, Text, DeviceEventEmitter, EmitterSubscription } from "react-native"; import React, { useEffect, useRef, useState } from "react"; import { GuideProps, GuideUpdate } from "../components/guides/GuideManager"; import { useSelector } from "react-redux"; import { RootState } from "../redux/store"; import Icon from "react-native-vector-icons/MaterialCommunityIcons"; const IconGuide = (props: GuideProps) => { const cameraViewDimensions = useSelector((state: RootState) => state.Util.cameraViewDimensions); const [iconname, setIconname] = useState(""); const [guideText, setGuideText] = useState(props.textInput || ""); const orientation = useSelector((state: RootState) => state.Util.orientation); const [eventSubscription, setEventSubscription] = useState([]); const [guideStyles, setGuideStyles] = useState(props.stylesheet?.guideStyles || {}); const [textStyles, setTextStyles] = useState(props.stylesheet?.textStyles || {}); const updater = () => { const subscribedEvent = DeviceEventEmitter.addListener( `@s20/sequence-event/action-event-${props.guide._id}`, (data: GuideUpdate) => { handleUpdate(data); } ); setEventSubscription((subscribedEvents) => [...subscribedEvents, subscribedEvent]); }; const handleUpdate = (update: GuideUpdate) => { if (update.action.property == "GUIDE_IMAGE") { if (update.action.type === "SHOW") { if (update.action.applyUpdate) setIconname(update.action.applyUpdate); if (update.action.styleUpdate) setGuideStyles(update.action.styleUpdate); } else if (update.action.type === "DONT_SHOW") { setIconname(""); } } if (update.action.property == "GUIDE_TEXT") { if (update.action.type === "SHOW") { if (update.action.applyUpdate) setGuideText(update.action.applyUpdate); if (update.action.styleUpdate) setTextStyles(update.action.styleUpdate); } else if (update.action.type === "DONT_SHOW") { setGuideText(""); } } }; useEffect(() => { setIconname((props.targets?.[0] as string) || ""); updater(); return () => { eventSubscription.forEach((subscription) => subscription.remove()); }; }, []); return ( {iconname.length ? ( ) : ( <> )} {guideText !== "" && ( {guideText} )} ); }; export default IconGuide;