@@ -13,6 +13,12 @@ import {
1313 selectedNetworkSelector ,
1414 selectedWalletSelector ,
1515} from '../store/reselect/wallet' ;
16+ import { useCurrency } from './displayValues' ;
17+ import { useTranslation } from 'react-i18next' ;
18+ import i18n from '../utils/i18n' ;
19+ import { showToast } from '../utils/notifications' ;
20+ import { ignoresSwitchUnitToastSelector } from '../store/reselect/user' ;
21+ import { ignoreSwitchUnitToast } from '../store/slices/user' ;
1622
1723/**
1824 * Retrieves wallet balances for the currently selected wallet and network.
@@ -102,3 +108,36 @@ export const useSwitchUnit = (): [EUnit, () => void] => {
102108
103109 return [ nextUnit , switchUnit ] ;
104110} ;
111+
112+ export function useSwitchUnitAnnounced ( ) : ( ) => void {
113+ const dispatch = useAppDispatch ( ) ;
114+ const [ nextUnit , switchUnit ] = useSwitchUnit ( ) ;
115+ const unit = useAppSelector ( primaryUnitSelector ) ;
116+ const ignoresSwitchUnitToast = useAppSelector ( ignoresSwitchUnitToastSelector ) ;
117+ const { fiatTicker } = useCurrency ( ) ;
118+ const { t } = useTranslation ( 'wallet' ) ;
119+
120+ const toUnitText : ( unit : EUnit ) => string = ( u ) => {
121+ if ( u === EUnit . BTC ) {
122+ return i18n . t ( 'settings:general.unit_bitcoin' ) ;
123+ }
124+ if ( u === EUnit . satoshi ) {
125+ return i18n . t ( 'settings:general.unit_satoshis' ) ;
126+ }
127+ return fiatTicker ;
128+ } ;
129+
130+ return ( ) : void => {
131+ switchUnit ( ) ;
132+ if ( ! ignoresSwitchUnitToast ) {
133+ showToast ( {
134+ type : 'info' ,
135+ title : t ( 'balance_unit_switched_title' , { unit : toUnitText ( nextUnit ) } ) ,
136+ description : t ( 'balance_unit_switched_message' , {
137+ unit : toUnitText ( unit ) ,
138+ } ) ,
139+ } ) ;
140+ dispatch ( ignoreSwitchUnitToast ( ) ) ;
141+ }
142+ } ;
143+ }
0 commit comments