import type { ExchangeRateUpdateParams } from '@lifi/sdk' import WarningRounded from '@mui/icons-material/WarningRounded' import { Box, Button, Typography } from '@mui/material' import type { RefObject } from 'react' import { forwardRef, useCallback, useImperativeHandle, useRef, useState, } from 'react' import { useTranslation } from 'react-i18next' import { BottomSheet } from '../../components/BottomSheet/BottomSheet.js' import type { BottomSheetBase } from '../../components/BottomSheet/types.js' import { useSetContentHeight } from '../../hooks/useSetContentHeight.js' import { formatTokenAmount } from '../../utils/format.js' import { CenterContainer, IconCircle } from './StatusBottomSheet.style.js' 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>(null) const handleContinue = () => { ;(ref as RefObject).current?.close(true) onContinue?.() } const handleCancel = useCallback(() => { ;(ref as RefObject).current?.close(false) onCancel?.() }, [onCancel, ref]) const handleClose = useCallback(() => { ;(ref as RefObject).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(null) 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.tokenAmount', { value: formatTokenAmount( BigInt(data.oldToAmount), data.toToken.decimals ), })}{' '} {data?.toToken.symbol} {t('main.currentAmount')} {t('format.tokenAmount', { value: formatTokenAmount( BigInt(data?.newToAmount), data?.toToken.decimals ), })}{' '} {data?.toToken.symbol} {t('main.rateChange')} {rateChange}% ) }