import type { ExchangeRateUpdateParams } from '@lifi/sdk'; import WarningRoundedIcon from '@mui/icons-material/WarningRounded'; import { Box, Button, Typography } from '@mui/material'; import type { MutableRefObject } from 'react'; import { forwardRef, useCallback, useImperativeHandle, useRef, useState, } from 'react'; import { useTranslation } from 'react-i18next'; import type { BottomSheetBase } from '../../components/BottomSheet'; import { BottomSheet } from '../../components/BottomSheet'; import { useSetContentHeight } from '../../hooks'; import { formatTokenAmount } from '../../utils'; import { CenterContainer, IconCircle } from './StatusBottomSheet.style'; export interface ExchangeRateBottomSheetBase { isOpen(): void; open( resolver: (value: boolean) => void, data: ExchangeRateUpdateParams, ): void; close(value?: boolean, bottomSheetClose?: boolean): void; } interface ExchangeRateBottomSheetProps { data?: ExchangeRateUpdateParams; onContinue?(): void; onCancel?(): void; } export const ExchangeRateBottomSheet = forwardRef< ExchangeRateBottomSheetBase, ExchangeRateBottomSheetProps >(({ onContinue, onCancel }, ref) => { const [data, setData] = useState(); const bottomSheetRef = useRef(null); const resolverRef = useRef<(value: boolean) => void>(); const handleContinue = () => { (ref as MutableRefObject).current?.close(true); onContinue?.(); }; const handleCancel = useCallback(() => { (ref as MutableRefObject).current?.close( false, ); onCancel?.(); }, [onCancel, ref]); const handleClose = useCallback(() => { (ref as MutableRefObject).current?.close( false, false, ); onCancel?.(); }, [onCancel, ref]); useImperativeHandle( ref, () => ({ isOpen: () => bottomSheetRef.current?.isOpen(), open: (resolver, data) => { setData(data); resolverRef.current = resolver; bottomSheetRef.current?.open(); }, close: (value = false, bottomSheetClose = true) => { resolverRef.current?.(value); if (bottomSheetClose) { bottomSheetRef.current?.close(); } }, }), [], ); return ( ); }); const ExchangeRateBottomSheetContent: React.FC< ExchangeRateBottomSheetProps > = ({ data, onCancel, onContinue }) => { const { t } = useTranslation(); const ref = useRef(); useSetContentHeight(ref); if (!data) { return; } const oldAmount = BigInt(data.oldToAmount || 1); const rateChange = ( (Number((BigInt(data.newToAmount || 0) * 1_000_000n) / oldAmount) / 1_000_000) * 100 - 100 ).toFixed(2); return ( {t('warning.title.rateChanged')} {t('warning.message.rateChanged')} {t('main.quotedAmount')} {t('format.number', { value: formatTokenAmount( BigInt(data.oldToAmount), data.toToken.decimals, 5, ), })}{' '} {data?.toToken.symbol} {t('main.currentAmount')} {t('format.number', { value: formatTokenAmount( BigInt(data?.newToAmount), data?.toToken.decimals, 5, ), })}{' '} {data?.toToken.symbol} {t('main.rateChange')} {rateChange}% ); };