Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/components/AccountDropdown/AccountChildOption.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { MenuItemOptionProps } from '@chakra-ui/react'
import { forwardRef, MenuItemOption, Stack, useColorModeValue } from '@chakra-ui/react'
import {
forwardRef,
MenuItemOption,
Stack,
useBreakpointValue,
useColorModeValue,
} from '@chakra-ui/react'
import type { AccountId } from '@shapeshiftoss/caip'
import { useCallback } from 'react'
import { useCallback, useMemo } from 'react'

import { Amount } from '@/components/Amount/Amount'
import { RawText } from '@/components/Text'
import { trimWithEndEllipsis } from '@/lib/utils'

type AccountChildRowProps = {
accountId: AccountId
Expand All @@ -18,12 +25,14 @@ export const AccountChildOption = forwardRef<AccountChildRowProps, 'button'>(
({ accountId, title, cryptoBalance, symbol, children, onOptionClick, ...props }, ref) => {
const color = useColorModeValue('black', 'white')
const handleClick = useCallback(() => onOptionClick(accountId), [accountId, onOptionClick])
const maxLength = useBreakpointValue({ base: 25, md: 50 })
const truncatedTitle = useMemo(() => trimWithEndEllipsis(title, maxLength), [title, maxLength])

return (
<MenuItemOption ref={ref} color={color} onClick={handleClick} {...props}>
<Stack direction='row' fontSize='sm' spacing={4} width='full'>
<RawText fontWeight='bold' whiteSpace='nowrap' flex={1}>
{title}
{truncatedTitle}
</RawText>
<Amount.Crypto
whiteSpace='nowrap'
Expand Down
46 changes: 28 additions & 18 deletions src/components/AccountDropdown/AccountSegement.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import { Stack } from '@chakra-ui/react'
import { Stack, useBreakpointValue } from '@chakra-ui/react'
import type { FC } from 'react'
import { useMemo } from 'react'

import { RawText } from '@/components/Text'
import { trimWithEndEllipsis } from '@/lib/utils'

type AccountGroupProps = {
title: string
subtitle?: string
}

export const AccountSegment: FC<AccountGroupProps> = ({ title, subtitle }) => (
<Stack
direction='row'
color='text.subtle'
fontSize='sm'
alignItems='center'
justifyContent='space-between'
width='full'
>
<RawText>{title}</RawText>
{subtitle && (
<RawText fontFamily='monospace' fontWeight='bold'>
{subtitle}
</RawText>
)}
</Stack>
)
export const AccountSegment: FC<AccountGroupProps> = ({ title, subtitle }) => {
const maxLength = useBreakpointValue({ base: 25, md: 50 })
const truncatedSubtitle = useMemo(
() => trimWithEndEllipsis(subtitle, maxLength),
[subtitle, maxLength],
)

return (
<Stack
direction='row'
color='text.subtle'
fontSize='sm'
alignItems='center'
justifyContent='space-between'
width='full'
>
<RawText>{title}</RawText>
{subtitle && (
<RawText fontFamily='monospace' fontWeight='bold'>
{truncatedSubtitle}
</RawText>
)}
</Stack>
)
}
2 changes: 1 addition & 1 deletion src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const trimWithEndEllipsis = (content?: string, trimmedContentLength?: num

if (!trimmedContentLength) return content

if (content.length < trimmedContentLength) return content
if (content.length <= trimmedContentLength) return content

return content.slice(0, trimmedContentLength).concat('...')
}
Expand Down
6 changes: 6 additions & 0 deletions src/state/slices/portfolioSlice/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,10 @@ describe('trimWithEndEllipsis', () => {
expect(trimWithEndEllipsis('abcdef', 191)).toEqual('abcdef')
expect(trimWithEndEllipsis(LongFoxDescription, 191)).toEqual(ExpectedTrimmedFoxDescription)
})

it('should not add ellipsis when content length equals max length', () => {
const exactly50Chars = 'BlackRock USD Institutional Digital Liquidity Fund'
expect(exactly50Chars.length).toEqual(50)
expect(trimWithEndEllipsis(exactly50Chars, 50)).toEqual(exactly50Chars)
})
})