import { useQuery } from "@apollo/client" ; import { MAIN_QUERY } from "@app/graphql/query" ; import useToken from "@app/utils/use-token" ; import NetInfo from "@react-native-community/netinfo" ; import { translate } from "../i18n" ; import crashlytics from "@react-native-firebase/crashlytics" ; const useMainQuery = ( ) : useMainQueryOutput => { const { hasToken } = useToken ( ) ; const { data , previousData , error , loading , refetch } = useQuery ( MAIN_QUERY , { variables : { hasToken } , fetchPolicy : "cache-and-network" , } ) ; let errors = [ ] ; if ( error ) { if ( error . graphQLErrors && previousData ) { errors = [ ... error . graphQLErrors ] ; } if ( error . graphQLErrors && ! previousData ) { error . graphQLErrors . forEach ( ( e ) => { crashlytics ( ) . recordError ( e ) ; console . log ( e ) ; } ) ; throw new Error ( translate ( "errors.fatalError" ) ) ; } if ( error . networkError && previousData ) { NetInfo . fetch ( ) . then ( ( state ) => { if ( ! state . isConnected ) { errors . push ( { message : translate ( "errors.network.connection" ) } ) ; } else { errors . push ( { message : translate ( "errors.network.request" ) } ) ; } } ) ; } if ( error . networkError && ! previousData ) { crashlytics ( ) . recordError ( error . networkError ) ; throw new Error ( translate ( "errors.fatalError" ) ) ; } } const userPreferredLanguage = data ?. me ?. language ; const btcWallet = data ?. me ?. defaultAccount ?. wallets ?. find ( ( wallet ) => wallet ?. __typename === "BTCWallet" ) ; if ( hasToken && ! btcWallet && ! loading ) { throw new Error ( translate ( "errors.fatalError" ) ) ; } const btcWalletBalance = btcWallet ?. balance ; const btcWalletId = btcWallet ?. id ; const transactionsEdges = btcWallet ?. transactions ?. edges ; const csvEncoded = data ?. me ?. defaultAccount ?. csvTransactions ; const me = data ?. me || { } ; const myPubKey = data ?. globals ?. nodesIds ?. [ 0 ] ?? "" ; const username = data ?. me ?. username ; const phoneNumber = data ?. me ?. phone ; const mobileVersions = data ?. mobileVersions ; return { userPreferredLanguage , btcWalletBalance , btcWalletId , transactionsEdges , csvEncoded , me , myPubKey , username , phoneNumber , mobileVersions , loading , refetch , errors , } ; } ; export default useMainQuery ;