import { ServerError , ServerParseError } from "@apollo/client" ; import { useApolloNetworkStatus } from "../../app" ; import { ComponentType } from "../../types/jsx" ; import { toastShow } from "../../utils/toast" ; import { NetworkErrorCode } from "./network-error-code" ; import useLogout from "../../hooks/use-logout" ; import { translate } from "../../i18n" ; import Toast from "react-native-root-toast" ; export const GlobalErrorToast = ( ) => { const status = useApolloNetworkStatus ( ) ; const { logout } = useLogout ( ) ; if ( status . queryError ?. operation ?. operationName === "prices" ) { return null ; } const networkError = ( status . queryError || status . mutationError ) ?. networkError as ServerError | ServerParseError ; if ( ! networkError ) { return null ; } if ( networkError . statusCode >= 500 ) { toastShow ( translate ( "errors.network.server" ) ) ; } if ( networkError . statusCode >= 400 && networkError . statusCode < 500 ) { let errorCode = ( networkError as ServerError ) . result ?. errors ?. [ 0 ] ?. code ; if ( ! errorCode ) { switch ( networkError . statusCode ) { case 401 : errorCode = "INVALID_AUTHENTICATION" ; break ; } } switch ( errorCode ) { case NetworkErrorCode . InvalidAuthentication : toastShow ( translate ( "common.reauth" ) , { duration : Toast . durations . SHORT , onHidden : ( ) => logout ( ) , } ) ; break ; default : toastShow ( translate ( "errors.network.request" ) ) ; break ; } } if ( networkError . message === "Network request failed" ) { toastShow ( translate ( "errors.network.connection" ) ) ; } if ( status . mutationError ) { status . mutationError . networkError = null ; } if ( status . queryError ) { status . queryError . networkError = null ; } return null ; } ;