import analytics from '@components/analytics' import Button from '@components/Button' import Icon from '@components/Icon' import { MenuContainer, MenuRow } from '@components/Menu' import CustomText from '@components/Text' import { useAppDispatch } from '@root/store' import { isDevelopment, isRelease } from '@utils/checkEnvironment' import { updateInstancePush } from '@utils/slices/instances/updatePush' import { updateInstancePushAlert } from '@utils/slices/instances/updatePushAlert' import { updateInstancePushDecode } from '@utils/slices/instances/updatePushDecode' import { clearPushLoading, getInstanceAccount, getInstancePush, getInstanceUri } from '@utils/slices/instancesSlice' import { StyleConstants } from '@utils/styles/constants' import layoutAnimation from '@utils/styles/layoutAnimation' import { useTheme } from '@utils/styles/ThemeManager' import * as Notifications from 'expo-notifications' import * as WebBrowser from 'expo-web-browser' import React, { useState, useEffect, useMemo } from 'react' import { useTranslation } from 'react-i18next' import { AppState, Linking, ScrollView, View } from 'react-native' import { useSelector } from 'react-redux' const TabMePush: React.FC = () => { const { colors } = useTheme() const { t } = useTranslation('screenTabs') const instanceAccount = useSelector( getInstanceAccount, (prev, next) => prev?.acct === next?.acct ) const instanceUri = useSelector(getInstanceUri) const dispatch = useAppDispatch() const instancePush = useSelector(getInstancePush) const [pushAvailable, setPushAvailable] = useState() const [pushEnabled, setPushEnabled] = useState() const [pushCanAskAgain, setPushCanAskAgain] = useState() const checkPush = async () => { const settings = await Notifications.getPermissionsAsync() layoutAnimation() setPushEnabled(settings.granted) setPushCanAskAgain(settings.canAskAgain) } useEffect(() => { if (isDevelopment) { setPushAvailable({ data: '', type: 'expo' }) } else { Notifications.getExpoPushTokenAsync({ experienceId: '@xmflsct/tooot' }) .then(data => setPushAvailable(data)) .catch(() => setPushAvailable(undefined)) } checkPush() const subscription = AppState.addEventListener('change', checkPush) return () => { subscription.remove() } }, []) useEffect(() => { dispatch(clearPushLoading()) }, []) const isLoading = instancePush?.global.loading || instancePush?.decode.loading const alerts = useMemo(() => { return instancePush?.alerts ? ( ['follow', 'favourite', 'reblog', 'mention', 'poll'] as [ 'follow', 'favourite', 'reblog', 'mention', 'poll' ] ).map(alert => ( { analytics(`me_push_${alert}`, { current: instancePush?.alerts[alert].value, new: !instancePush?.alerts[alert].value }) dispatch( updateInstancePushAlert({ changed: alert, alerts: { ...instancePush?.alerts, [alert]: { ...instancePush?.alerts[alert], value: !instancePush?.alerts[alert].value } } }) ) }} /> )) : null }, [pushEnabled, instancePush?.global, instancePush?.alerts, isLoading]) return ( {!!pushAvailable ? ( <> {pushEnabled === false ? (