import React, { useCallback, useState, useMemo } from "react"; import { View, ImageBackground, ScrollView } from "react-native"; import { TouchableRipple, Button, Title } from "react-native-paper"; import { useIsFocused, useNavigation } from "@react-navigation/native"; import { MaterialCommunityIcons } from "@expo/vector-icons"; import { deepEqual } from "fast-equals"; import withConnectivity from "~/hoc/withConnectivity"; import { useAlertState, useSessionState, alertActions, useAggregatedMessagesState, } from "~/stores"; import { getCurrentLocation } from "~/location"; import alertBigButtonBgMap from "~/assets/img/alert-big-button-bg-map.png"; import alertBigButtonBgMapGrey from "~/assets/img/alert-big-button-bg-map-grey.png"; import alertBigButtonBgMessages from "~/assets/img/alert-big-button-bg-messages.png"; import alertBigButtonBgMessagesGrey from "~/assets/img/alert-big-button-bg-messages-grey.png"; import Text from "~/components/Text"; import AlertInfoLineLevel from "~/containers/AlertInfoLines/Level"; import LocationInfoSection from "~/containers/LocationInfoSection"; import AlertInfoLineCode from "~/containers/AlertInfoLines/Code"; import AlertInfoLineDistance from "~/containers/AlertInfoLines/Distance"; import AlertInfoLineCreatedTime from "~/containers/AlertInfoLines/CreatedTime"; import AlertInfoLineClosedTime from "~/containers/AlertInfoLines/ClosedTime"; import AlertInfoLineSubject from "~/containers/AlertInfoLines/Subject"; import AlertInfoLineAddress from "~/containers/AlertInfoLines/Address"; import AlertInfoLineNear from "~/containers/AlertInfoLines/Near"; import AlertInfoLineW3w from "~/containers/AlertInfoLines/W3w"; import AlertInfoLineNotifiedCount from "~/containers/AlertInfoLines/NotifiedCount"; import AlertInfoLineRadius from "~/containers/AlertInfoLines/Radius"; import AlertInfoLineSentBy from "~/containers/AlertInfoLines/SentBy"; import { COMING_HELP_MUTATION, NOTIFY_AROUND_MUTATION, NOTIFY_RELATIVES_MUTATION, CLOSE_ALERT, REOPEN_ALERT, KEEP_OPEN_ALERT, } from "./gql"; import useStyles from "./styles"; import SendSms from "./SendSms"; import PhoneCallEmergency from "./PhoneCallEmergency"; import { useMutation } from "@apollo/client"; import FieldLevel from "./FieldLevel"; import FieldSubject from "./FieldSubject"; import FieldFollowLocation from "./FieldFollowLocation"; import MapLinksButton from "~/containers/MapLinksButton"; import { useTheme } from "~/theme"; const COMING_HELP_MESSAGE = "Je viens vous aider"; export default withConnectivity( React.memo(function AlertCurOverview() { const styles = useStyles(); const { colors } = useTheme(); const { navAlertCur } = useAlertState(["navAlertCur"]); const { alert } = navAlertCur; // const isFocused = useIsFocused(); // console.debug("Render AlertCurOverwiew", new Date(), { isFocused, alertId }); const { messagesList } = useAggregatedMessagesState(["messagesList"]); const unreadMessagesForCurrentAlert = alert && alert.id && messagesList.filter((msg) => !msg.isRead && msg.alertId === alert.id) .length; const { id: alertId, userId } = alert; const { userId: sessionUserId } = useSessionState(["userId"]); const isSent = userId === sessionUserId; const navigation = useNavigation(); const [notifyAroundMutation] = useMutation(NOTIFY_AROUND_MUTATION); const notifyAround = useCallback(async () => { await notifyAroundMutation({ variables: { alertId, }, }); }, [alertId, notifyAroundMutation]); const [notifyRelativesMutation] = useMutation(NOTIFY_RELATIVES_MUTATION); const notifyRelatives = useCallback(async () => { await notifyRelativesMutation({ variables: { alertId, }, }); }, [alertId, notifyRelativesMutation]); const [closeAlertMutation] = useMutation(CLOSE_ALERT); const closeAlert = useCallback(async () => { await closeAlertMutation({ variables: { alertId }, optimisticResponse: { doAlertClose: { ok: true, __typename: "AlertCloseOutput", }, }, update: (cache) => { // Find the alert in the cache and update its state cache.modify({ id: cache.identify({ __typename: "alert", id: alertId }), fields: { state: () => "closed", closedAt: () => new Date().toISOString(), }, }); }, }); }, [alertId, closeAlertMutation]); const [reopenAlertMutation] = useMutation(REOPEN_ALERT); const reopenAlert = useCallback(async () => { await reopenAlertMutation({ variables: { alertId }, }); }, [alertId, reopenAlertMutation]); const [keepOpenAlertMutation] = useMutation(KEEP_OPEN_ALERT); const keepOpenAlert = useCallback(async () => { await keepOpenAlertMutation({ variables: { alertId }, }); }, [alertId, keepOpenAlertMutation]); const shouldDisplayKeepOpen = useCallback(() => { const createdAt = new Date(alert.createdAt); const now = new Date(); const diff = now - createdAt; const hours = diff / (1000 * 60 * 60); // Check if alert is older than 23 hours if (hours < 23) { return false; } // Check if keep_open_at is null or less than 1 hour from now if (!alert.keepOpenAt) { return true; } const keepOpenAt = new Date(alert.keepOpenAt); const diffFromKeepOpen = keepOpenAt - now; // Future - now to get time until keepOpenAt const hoursUntilKeepOpen = diffFromKeepOpen / (1000 * 60 * 60); return hoursUntilKeepOpen <= 1; // Show button if less than 1 hour until keepOpenAt }, [alert.createdAt, alert.keepOpenAt]); const [comingHelpMutation] = useMutation(COMING_HELP_MUTATION); const comingHelp = useCallback(async () => { const coords = await getCurrentLocation(); const { latitude, longitude } = coords; const location = { type: "Point", coordinates: [longitude, latitude], }; await comingHelpMutation({ variables: { id: navAlertCur.id, alertId: alert.id, text: COMING_HELP_MESSAGE, location, }, }); navigation.navigate({ name: "AlertCur", params: { screen: "AlertCurTab", params: { screen: "AlertCurMessage", }, }, }); }, [comingHelpMutation, alert.id, navAlertCur.id, navigation]); const quitAlert = useCallback(() => { alertActions.setNavAlertCur(null); navigation.navigate("AlertAggList"); }, [navigation]); const { state } = alert; const isOpen = state === "open"; const isClosed = state === "closed"; const isArchived = state === "archived"; const { level } = alert; const alertLevelEmergency = level === "red" || level === "yellow"; const [parentScrollEnabled, setParentScrollEnabled] = useState(true); const isLocationsDifferent = useMemo(() => { return ( alert.initialLocation && alert.location && !deepEqual(alert.initialLocation, alert.location) ); }, [alert.initialLocation, alert.location]); // if (!isFocused) { // return null; // } return ( {isClosed && Alerte terminée} {isArchived && Alerte archivée} { navigation.navigate("Main", { screen: "AlertCur", params: { screen: "AlertCurTab", params: { screen: "AlertCurMessage", }, }, }); }} > {/* */} Messages {unreadMessagesForCurrentAlert > 0 && ( {` (${ unreadMessagesForCurrentAlert > 99 ? "+99" : unreadMessagesForCurrentAlert })`} )} { navigation.navigate("Main", { screen: "AlertCur", params: { screen: "AlertCurTab", params: { screen: "AlertCurMap", }, }, }); }} style={styles.linkBoxButton} > {/* */} Carte {/* {isSent && alert.notifyRelatives && ( Vos proches ont été notifiés )} */} {isOpen && isSent && !alert.notifyRelatives && ( )} {isOpen && isSent && !alert.notifyAround && ( )} {isOpen && !isSent && !navAlertCur.comingHelp && ( )} {!isSent && alert.location?.coordinates && ( )} {isOpen && isSent && } {isOpen && isSent && alertLevelEmergency && } {isOpen && isSent && shouldDisplayKeepOpen() && ( )} {isOpen && isSent && ( )} {isClosed && isSent && ( )} {isSent && alert.location?.coordinates && ( )} {isSent && isOpen && ( )} {(!isSent || !isOpen) && ( )} {!isSent && } {(!isSent || !isOpen) && } {useMemo(() => { if (isLocationsDifferent) { return ( <> ); } else { return ( <> ); } }, [alert, styles, isLocationsDifferent])} ); }), );