import React, { useContext, useEffect, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Typography, Grid, Select, Divider, MenuItem, Box } from '@mui/material'; import currencyDict from '../../../static/assets/currencies.json'; import { useTheme } from '@mui/system'; import { AutocompletePayments } from '../MakerForm'; import { fiatMethods, swapMethods, PaymentIcon } from '../PaymentMethods'; import { FlagWithProps, SendReceiveIcon } from '../Icons'; import { AppContext, type UseAppStoreType } from '../../contexts/AppContext'; import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank'; import SwapCalls from '@mui/icons-material/SwapCalls'; import { FederationContext, type UseFederationStoreType } from '../../contexts/FederationContext'; import RobotAvatar from '../RobotAvatar'; import RoboSatsNoText from '../Icons/RoboSatsNoText'; interface BookControlProps { width: number; paymentMethod: string[]; setPaymentMethods: (state: string[]) => void; } const BookControl = ({ width, paymentMethod, setPaymentMethods, }: BookControlProps): JSX.Element => { const { fav, setFav } = useContext(AppContext); const { federation } = useContext(FederationContext); const { t, i18n } = useTranslation(); const theme = useTheme(); const [orderType, setOrderType] = useState('any'); const [small, medium, large] = useMemo(() => { const small = fav.mode === 'fiat' ? 16 : 7.5; const medium = small + 13; const large = medium + (t('and use').length + t('pay with').length) * 0.6 + 5; return [small, medium, large]; }, [i18n.language, fav.mode]); useEffect(() => { if (fav.type === null) { setOrderType('any'); } else if (fav.mode === 'fiat') { setOrderType(fav.type === 1 ? 'buy' : 'sell'); } else { setOrderType(fav.type === 1 ? 'swapin' : 'swapout'); } }, [fav.mode, fav.type]); const handleCurrencyChange = function (e: React.ChangeEvent): void { const currency = Number(e.target.value); setFav({ ...fav, currency, mode: currency === 1000 ? 'swap' : 'fiat' }); }; const handleHostChange = function (e: React.ChangeEvent): void { const coordinator = String(e.target.value); if (coordinator === 'any') { federation.refreshBookHosts(coordinator !== 'any'); } setFav({ ...fav, coordinator }); }; const handleOrderTypeChange = (mouseEvent: React.MouseEvent, select: object): void => { if (select.props.value === 'sell') { const currency = fav.currency === 1000 ? 0 : fav.currency; setFav({ ...fav, mode: 'fiat', type: 0, currency }); } else if (select.props.value === 'buy') { const currency = fav.currency === 1000 ? 0 : fav.currency; setFav({ ...fav, mode: 'fiat', type: 1, currency }); } else if (select.props.value === 'swapin') { setFav({ ...fav, mode: 'swap', type: 1, currency: 1000 }); } else if (select.props.value === 'swapout') { setFav({ ...fav, mode: 'swap', type: 0, currency: 1000 }); } else { const currency = fav.currency === 1000 ? 0 : fav.currency; setFav({ ...fav, mode: 'fiat', type: null, currency }); } setOrderType(select.props.value); }; const orderTypeIcon = (value: string): React.ReactNode => { let component = ; let text = t('ANY'); if (value === 'sell') { component = ; text = t('Sell'); } else if (value === 'buy') { component = ; text = t('Buy'); } else if (value === 'swapin') { component = ; text = t('Swap In'); } else if (value === 'swapout') { component = ; text = t('Swap Out'); } return (
{component} {width > medium ? ( {` ${text}`} ) : ( <> )}
); }; return ( {width > large ? ( {t('I want to')} ) : null} {width > large && fav.mode === 'fiat' ? ( {t('and use')} ) : null} {fav.mode === 'fiat' ? ( ) : null} {width > large ? ( {fav.currency === 1000 ? t(fav.type === 0 ? 'to' : 'from') : t('pay with')} ) : null} {width > medium ? ( ) : null} {width > small && width <= medium ? ( ) : null} {width > large ? ( {fav.currency === 1000 ? t(fav.type === 0 ? 'to' : 'from') : t('hosted by')} ) : null} ); }; export default BookControl;