import type { TokenAmount } from '@lifi/sdk' import ExpandLess from '@mui/icons-material/ExpandLess' import ExpandMore from '@mui/icons-material/ExpandMore' import { Box, Collapse, Tooltip } from '@mui/material' import type { MouseEventHandler } from 'react' import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useWidgetConfig } from '../../providers/WidgetProvider/WidgetProvider.js' import { HiddenUI, type RouteLabel } from '../../types/widget.js' import { getAccumulatedFeeCostsBreakdown } from '../../utils/fees.js' import type { CardProps } from '../Card/Card.js' import { Card } from '../Card/Card.js' import { CardIconButton } from '../Card/CardIconButton.js' import { CardLabel, CardLabelTypography } from '../Card/CardLabel.js' import { StepActions } from '../StepActions/StepActions.js' import { Token } from '../Token/Token.js' import { getMatchingLabels } from './getMatchingLabels.js' import { TokenContainer } from './RouteCard.style.js' import { RouteCardEssentials } from './RouteCardEssentials.js' import { RouteCardEssentialsExpanded } from './RouteCardEssentialsExpanded.js' import type { RouteCardProps } from './types.js' export const RouteCard: React.FC< RouteCardProps & Omit > = ({ route, active, variant = 'default', expanded: defaultExpanded, ...other }) => { const { t } = useTranslation() const { subvariant, subvariantOptions, routeLabels, hiddenUI } = useWidgetConfig() const [cardExpanded, setCardExpanded] = useState(defaultExpanded) const handleExpand: MouseEventHandler = (e) => { e.stopPropagation() setCardExpanded((expanded) => !expanded) } const token: TokenAmount = subvariant === 'custom' && subvariantOptions?.custom !== 'deposit' && subvariantOptions?.custom !== 'fund' ? { ...route.fromToken, amount: BigInt(route.fromAmount) } : { ...route.toToken, amount: BigInt(route.toAmount) } const impactToken: TokenAmount | undefined = subvariant !== 'custom' && !hiddenUI?.includes(HiddenUI.RouteCardPriceImpact) ? { ...route.fromToken, amount: BigInt(route.fromAmount) } : undefined const [tags, customLabels]: [string[], RouteLabel[]] = useMemo(() => { const mainTag = route.tags?.find( (tag) => tag === 'CHEAPEST' || tag === 'FASTEST' ) const tags: string[] = mainTag ? [mainTag] : [] const { combinedFeesUSD } = getAccumulatedFeeCostsBreakdown(route) if (!combinedFeesUSD) { tags.push('gasless') } if (route.steps.length > 1) { tags.push('multistep') } const customLabels = getMatchingLabels(route, routeLabels) return [tags, customLabels] }, [route.tags, route, routeLabels]) const cardContent = ( {subvariant !== 'refuel' && (tags.length || customLabels.length) ? ( {tags?.map((tag) => { const formattedTag = tag.toLowerCase() const tooltipKey = `tooltip.${formattedTag}` as any const tooltipText = t(tooltipKey) const hasTooltip = tooltipText !== tooltipKey const cardLabel = ( {t(`main.tags.${formattedTag}` as any)} ) return hasTooltip ? ( {cardLabel} ) : ( cardLabel ) })} {customLabels.map((label, index) => ( {label.text} ))} ) : null} {!defaultExpanded ? ( {cardExpanded ? ( ) : ( )} ) : null} {route.steps.map((step) => ( ))} ) return subvariant === 'refuel' || variant === 'cardless' ? ( cardContent ) : ( {cardContent} ) }