diff --git a/packages/legacy/core/App/components/network/NetInfo.tsx b/packages/legacy/core/App/components/network/NetInfo.tsx index f782316e1c..eb18b94d7a 100644 --- a/packages/legacy/core/App/components/network/NetInfo.tsx +++ b/packages/legacy/core/App/components/network/NetInfo.tsx @@ -1,38 +1,47 @@ import * as React from 'react' -import { useEffect } from 'react' +import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Toast from 'react-native-toast-message' import { useNetwork } from '../../contexts/network' +import { ToastType } from '../../components/toast/BaseToast' const NetInfo: React.FC = () => { const { silentAssertConnectedNetwork, assertNetworkReachable } = useNetwork() const { t } = useTranslation() + const [hasShown, setHasShown] = useState(false) const isConnected = silentAssertConnectedNetwork() - useEffect(() => { + // Network is connected if (isConnected) { + // Assert that internet is available assertNetworkReachable().then((status) => { + // Connected to a network, reset toast + setHasShown(false) if (status) { return } + // User is connected to a network but has no internet, display toast Toast.show({ - type: 'warn', - autoHide: false, - text1: t('NetInfo.NoInternetConnectionMessage'), + type: ToastType.Error, + autoHide: true, + text1: t('NetInfo.NoInternetConnectionTitle'), }) }) - return } - Toast.show({ - type: 'error', - autoHide: true, - text1: t('NetInfo.NoInternetConnectionTitle'), - }) - }, [isConnected, assertNetworkReachable, t]) + // Only show the toast if the user hasn't seen it already + if (!hasShown) { + setHasShown(true) + Toast.show({ + type: ToastType.Error, + autoHide: true, + text1: t('NetInfo.NoInternetConnectionTitle'), + }) + } + }, [isConnected, assertNetworkReachable, t, hasShown]) return null } diff --git a/packages/legacy/core/App/components/toast/BaseToast.tsx b/packages/legacy/core/App/components/toast/BaseToast.tsx index b851b564fe..086b32a1c1 100644 --- a/packages/legacy/core/App/components/toast/BaseToast.tsx +++ b/packages/legacy/core/App/components/toast/BaseToast.tsx @@ -5,6 +5,7 @@ import Icon from 'react-native-vector-icons/MaterialIcons' import { useTheme } from '../../contexts/theme' import { GenericFn } from '../../types/fn' import { testIdWithKey } from '../../utils/testable' +import Toast from 'react-native-toast-message' interface BaseToastProps { title?: string @@ -96,14 +97,27 @@ const BaseToast: React.FC = ({ title, body, toastType, onPress = return ( onPress()}> - - - - {title} - - - {body} - + + + + + {title} + + {body && ( + + {body} + + )} + + + + { + Toast.hide() + }} + > + + diff --git a/packages/legacy/core/__tests__/components/BaseToast.test.tsx b/packages/legacy/core/__tests__/components/BaseToast.test.tsx index d8e3c59c0e..83ca6ca472 100644 --- a/packages/legacy/core/__tests__/components/BaseToast.test.tsx +++ b/packages/legacy/core/__tests__/components/BaseToast.test.tsx @@ -51,4 +51,10 @@ describe('BaseToast Component', () => { expect(tree).toMatchSnapshot() }) + + test('Toast Renders without body text', () => { + const tree = render() + const bodyText = tree.queryByTestId('ToastBody') + expect(bodyText).toBeNull() + }) }) diff --git a/packages/legacy/core/__tests__/components/__snapshots__/BaseToast.test.tsx.snap b/packages/legacy/core/__tests__/components/__snapshots__/BaseToast.test.tsx.snap index e0e6a397f6..ef14ddf9e0 100644 --- a/packages/legacy/core/__tests__/components/__snapshots__/BaseToast.test.tsx.snap +++ b/packages/legacy/core/__tests__/components/__snapshots__/BaseToast.test.tsx.snap @@ -54,79 +54,149 @@ exports[`BaseToast Component Error renders correctly 1`] = ` ] } > - -  - - Hello World +  - - The quick brown fox jumped over the lazy dog - + + Hello World + + + The quick brown fox jumped over the lazy dog + + + + + + +  + + @@ -186,79 +256,149 @@ exports[`BaseToast Component Info renders correctly 1`] = ` ] } > - -  - - Hello World +  - - The quick brown fox jumped over the lazy dog - + + Hello World + + + The quick brown fox jumped over the lazy dog + + + + + + +  + + @@ -318,79 +458,149 @@ exports[`BaseToast Component Success renders correctly 1`] = ` ] } > - -  - - Hello World +  - - The quick brown fox jumped over the lazy dog - + + Hello World + + + The quick brown fox jumped over the lazy dog + + + + + + +  + + @@ -450,79 +660,149 @@ exports[`BaseToast Component Warn renders correctly 1`] = ` ] } > - -  - - Hello World +  - - The quick brown fox jumped over the lazy dog - + + Hello World + + + The quick brown fox jumped over the lazy dog + + + + + + +  + +