// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import React, {useCallback, useState} from 'react'; import {Platform} from 'react-native'; import {updateMe} from '@actions/remote/user'; import SettingContainer from '@components/settings/container'; import SettingSeparator from '@components/settings/separator'; import {useServerUrl} from '@context/server'; import useAndroidHardwareBackHandler from '@hooks/android_back_handler'; import useBackNavigation from '@hooks/navigate_back'; import useNotificationProps from '@hooks/notification_props'; import {popTopScreen} from '@screens/navigation'; import MobileSendPush from './push_send'; import MobilePushStatus from './push_status'; import MobilePushThread from './push_thread'; import type UserModel from '@typings/database/models/servers/user'; import type {AvailableScreens} from '@typings/screens/navigation'; type NotificationMobileProps = { componentId: AvailableScreens; currentUser?: UserModel; isCRTEnabled: boolean; sendPushNotifications: boolean; }; const NotificationPush = ({componentId, currentUser, isCRTEnabled, sendPushNotifications}: NotificationMobileProps) => { const serverUrl = useServerUrl(); const notifyProps = useNotificationProps(currentUser); const [pushSend, setPushSend] = useState(notifyProps.push); const [pushStatus, setPushStatus] = useState(notifyProps.push_status); const [pushThread, setPushThreadPref] = useState(notifyProps?.push_threads || 'all'); const onMobilePushThreadChanged = useCallback(() => { setPushThreadPref(pushThread === 'all' ? 'mention' : 'all'); }, [pushThread]); const close = useCallback(() => popTopScreen(componentId), [componentId]); const canSaveSettings = useCallback(() => { const p = pushSend !== notifyProps.push; const pT = pushThread !== notifyProps.push_threads; const pS = pushStatus !== notifyProps.push_status; return p || pT || pS; }, [notifyProps, pushSend, pushStatus, pushThread]); const saveNotificationSettings = useCallback(() => { const canSave = canSaveSettings(); if (canSave) { const notify_props: UserNotifyProps = { ...notifyProps, push: pushSend, push_status: pushStatus, push_threads: pushThread, }; updateMe(serverUrl, {notify_props}); } close(); }, [canSaveSettings, close, notifyProps, pushSend, pushStatus, pushThread, serverUrl]); useBackNavigation(saveNotificationSettings); useAndroidHardwareBackHandler(componentId, saveNotificationSettings); return ( {isCRTEnabled && pushSend === 'mention' && ( <> {Platform.OS === 'android' && ()} )} {sendPushNotifications && pushSend !== 'none' && ( <> {Platform.OS === 'android' && ()} )} ); }; export default NotificationPush;