diff --git a/app/components/amount-input-screen/amount-input-screen.tsx b/app/components/amount-input-screen/amount-input-screen.tsx index 0b8af92e12..86afcd74da 100644 --- a/app/components/amount-input-screen/amount-input-screen.tsx +++ b/app/components/amount-input-screen/amount-input-screen.tsx @@ -1,5 +1,5 @@ import * as React from "react" -import { WalletCurrency } from "@app/graphql/generated" +import { WalletCurrency, useSwapDefaultUnitOfAccountQuery } from "@app/graphql/generated" import { CurrencyInfo, useDisplayCurrency } from "@app/hooks/use-display-currency" import { useI18nContext } from "@app/i18n/i18n-react" import { ConvertMoneyAmount } from "@app/screens/send-bitcoin-screen/payment-details" @@ -19,6 +19,8 @@ import { NumberPadReducerActionType, NumberPadReducerState, } from "./number-pad-reducer" +import { setSwapDefaultUnitOfAccount } from "@app/graphql/client-only-query" +import { useApolloClient } from "@apollo/client" export type AmountInputScreenProps = { goBack: () => void @@ -198,11 +200,32 @@ export const AmountInputScreen: React.FC = ({ [currencyInfo], ) - const onToggleCurrency = - secondaryNewAmount && - (() => { + const client = useApolloClient() + const swapDefaultUnitOfAccount = + useSwapDefaultUnitOfAccountQuery().data?.swapDefaultUnitOfAccount + const [needSwapCurrencyInit, setNeedSwapCurrencyInit] = React.useState( + swapDefaultUnitOfAccount, + ) + + const onToggleCurrency = React.useCallback(() => { + if (secondaryNewAmount) { + setSwapDefaultUnitOfAccount(client, !swapDefaultUnitOfAccount) setNumberPadAmount(secondaryNewAmount) - }) + } + }, [secondaryNewAmount, setNumberPadAmount, client, swapDefaultUnitOfAccount]) + + useEffect(() => { + if (needSwapCurrencyInit && secondaryNewAmount) { + setNeedSwapCurrencyInit(false) + setNumberPadAmount(secondaryNewAmount) + } + }, [ + swapDefaultUnitOfAccount, + onToggleCurrency, + secondaryNewAmount, + needSwapCurrencyInit, + setNumberPadAmount, + ]) useEffect(() => { if (initialAmount) { diff --git a/app/graphql/cache.ts b/app/graphql/cache.ts index 65bec3ad6f..91c6e5e55f 100644 --- a/app/graphql/cache.ts +++ b/app/graphql/cache.ts @@ -88,6 +88,9 @@ export const createCache = () => introducingCirclesModalShown: { read: (value) => value ?? false, }, + swapDefaultUnitOfAccount: { + read: (value) => value ?? false, + }, innerCircleValue: { read: (value) => value ?? -1, }, diff --git a/app/graphql/client-only-query.ts b/app/graphql/client-only-query.ts index 1d6fae2f6e..dc12b73333 100644 --- a/app/graphql/client-only-query.ts +++ b/app/graphql/client-only-query.ts @@ -15,6 +15,8 @@ import { InnerCircleValueQuery, IntroducingCirclesModalShownDocument, IntroducingCirclesModalShownQuery, + SwapDefaultUnitOfAccountDocument, + SwapDefaultUnitOfAccountQuery, } from "./generated" export default gql` @@ -49,6 +51,10 @@ export default gql` query innerCircleValue { innerCircleValue @client } + + query swapDefaultUnitOfAccount { + swapDefaultUnitOfAccount @client + } ` export const saveHideBalance = ( @@ -173,3 +179,20 @@ export const setInnerCircleCachedValue = ( console.warn("unable to update InnerCircleValueDocument") } } + +export const setSwapDefaultUnitOfAccount = ( + client: ApolloClient, + swapDefaultUnitOfAccount: boolean, +) => { + try { + client.writeQuery({ + query: SwapDefaultUnitOfAccountDocument, + data: { + __typename: "Query", + swapDefaultUnitOfAccount, + }, + }) + } catch { + console.warn("unable to update SwapDefaultUnitOfAccountDocument") + } +} diff --git a/app/graphql/generated.gql b/app/graphql/generated.gql index 99aa5e87ac..2b0d0b12bb 100644 --- a/app/graphql/generated.gql +++ b/app/graphql/generated.gql @@ -1454,6 +1454,10 @@ query supportedCountries { } } +query swapDefaultUnitOfAccount { + swapDefaultUnitOfAccount @client +} + query totpRegistrationScreen { me { username diff --git a/app/graphql/generated.ts b/app/graphql/generated.ts index e40f4ae159..224c4f1c95 100644 --- a/app/graphql/generated.ts +++ b/app/graphql/generated.ts @@ -1496,6 +1496,7 @@ export type Query = { readonly quizQuestions?: Maybe>>; /** Returns 1 Sat and 1 Usd Cent price for the given currency */ readonly realtimePrice: RealtimePrice; + readonly swapDefaultUnitOfAccount: Scalars['Boolean']['output']; /** @deprecated will be migrated to AccountDefaultWalletId */ readonly userDefaultWalletId: Scalars['WalletId']['output']; readonly usernameAvailable?: Maybe; @@ -2239,6 +2240,11 @@ export type InnerCircleValueQueryVariables = Exact<{ [key: string]: never; }>; export type InnerCircleValueQuery = { readonly __typename: 'Query', readonly innerCircleValue: number }; +export type SwapDefaultUnitOfAccountQueryVariables = Exact<{ [key: string]: never; }>; + + +export type SwapDefaultUnitOfAccountQuery = { readonly __typename: 'Query', readonly swapDefaultUnitOfAccount: boolean }; + export type TransactionFragment = { readonly __typename: 'Transaction', readonly id: string, readonly status: TxStatus, readonly direction: TxDirection, readonly memo?: string | null, readonly createdAt: number, readonly settlementAmount: number, readonly settlementFee: number, readonly settlementDisplayFee: string, readonly settlementCurrency: WalletCurrency, readonly settlementDisplayAmount: string, readonly settlementDisplayCurrency: string, readonly settlementPrice: { readonly __typename: 'PriceOfOneSettlementMinorUnitInDisplayMinorUnit', readonly base: number, readonly offset: number, readonly currencyUnit: string, readonly formattedAmount: string }, readonly initiationVia: { readonly __typename: 'InitiationViaIntraLedger', readonly counterPartyWalletId?: string | null, readonly counterPartyUsername?: string | null } | { readonly __typename: 'InitiationViaLn', readonly paymentHash: string, readonly paymentRequest: string } | { readonly __typename: 'InitiationViaOnChain', readonly address: string }, readonly settlementVia: { readonly __typename: 'SettlementViaIntraLedger', readonly counterPartyWalletId?: string | null, readonly counterPartyUsername?: string | null } | { readonly __typename: 'SettlementViaLn', readonly preImage?: string | null } | { readonly __typename: 'SettlementViaOnChain', readonly transactionHash?: string | null, readonly arrivalInMempoolEstimatedAt?: number | null } }; export type TransactionListFragment = { readonly __typename: 'TransactionConnection', readonly pageInfo: { readonly __typename: 'PageInfo', readonly hasNextPage: boolean, readonly hasPreviousPage: boolean, readonly startCursor?: string | null, readonly endCursor?: string | null }, readonly edges?: ReadonlyArray<{ readonly __typename: 'TransactionEdge', readonly cursor: string, readonly node: { readonly __typename: 'Transaction', readonly id: string, readonly status: TxStatus, readonly direction: TxDirection, readonly memo?: string | null, readonly createdAt: number, readonly settlementAmount: number, readonly settlementFee: number, readonly settlementDisplayFee: string, readonly settlementCurrency: WalletCurrency, readonly settlementDisplayAmount: string, readonly settlementDisplayCurrency: string, readonly settlementPrice: { readonly __typename: 'PriceOfOneSettlementMinorUnitInDisplayMinorUnit', readonly base: number, readonly offset: number, readonly currencyUnit: string, readonly formattedAmount: string }, readonly initiationVia: { readonly __typename: 'InitiationViaIntraLedger', readonly counterPartyWalletId?: string | null, readonly counterPartyUsername?: string | null } | { readonly __typename: 'InitiationViaLn', readonly paymentHash: string, readonly paymentRequest: string } | { readonly __typename: 'InitiationViaOnChain', readonly address: string }, readonly settlementVia: { readonly __typename: 'SettlementViaIntraLedger', readonly counterPartyWalletId?: string | null, readonly counterPartyUsername?: string | null } | { readonly __typename: 'SettlementViaLn', readonly preImage?: string | null } | { readonly __typename: 'SettlementViaOnChain', readonly transactionHash?: string | null, readonly arrivalInMempoolEstimatedAt?: number | null } } }> | null }; @@ -3498,6 +3504,38 @@ export function useInnerCircleValueLazyQuery(baseOptions?: Apollo.LazyQueryHookO export type InnerCircleValueQueryHookResult = ReturnType; export type InnerCircleValueLazyQueryHookResult = ReturnType; export type InnerCircleValueQueryResult = Apollo.QueryResult; +export const SwapDefaultUnitOfAccountDocument = gql` + query swapDefaultUnitOfAccount { + swapDefaultUnitOfAccount @client +} + `; + +/** + * __useSwapDefaultUnitOfAccountQuery__ + * + * To run a query within a React component, call `useSwapDefaultUnitOfAccountQuery` and pass it any options that fit your needs. + * When your component renders, `useSwapDefaultUnitOfAccountQuery` returns an object from Apollo Client that contains loading, error, and data properties + * you can use to render your UI. + * + * @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options; + * + * @example + * const { data, loading, error } = useSwapDefaultUnitOfAccountQuery({ + * variables: { + * }, + * }); + */ +export function useSwapDefaultUnitOfAccountQuery(baseOptions?: Apollo.QueryHookOptions) { + const options = {...defaultOptions, ...baseOptions} + return Apollo.useQuery(SwapDefaultUnitOfAccountDocument, options); + } +export function useSwapDefaultUnitOfAccountLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions) { + const options = {...defaultOptions, ...baseOptions} + return Apollo.useLazyQuery(SwapDefaultUnitOfAccountDocument, options); + } +export type SwapDefaultUnitOfAccountQueryHookResult = ReturnType; +export type SwapDefaultUnitOfAccountLazyQueryHookResult = ReturnType; +export type SwapDefaultUnitOfAccountQueryResult = Apollo.QueryResult; export const NetworkDocument = gql` query network { globals { @@ -8143,6 +8181,7 @@ export type QueryResolvers, ParentType, ContextType>; quizQuestions?: Resolver>>, ParentType, ContextType>; realtimePrice?: Resolver>; + swapDefaultUnitOfAccount?: Resolver; userDefaultWalletId?: Resolver>; usernameAvailable?: Resolver, ParentType, ContextType, RequireFields>; welcomeLeaderboard?: Resolver>; diff --git a/app/graphql/local-schema.gql b/app/graphql/local-schema.gql index 4398ab9f97..ad479254d9 100644 --- a/app/graphql/local-schema.gql +++ b/app/graphql/local-schema.gql @@ -20,6 +20,7 @@ extend type Query { hasPromptedSetDefaultAccount: Boolean! introducingCirclesModalShown: Boolean! innerCircleValue: Int! + swapDefaultUnitOfAccount: Boolean! } extend type Account {