Skip to content

Commit c19504b

Browse files
fix: set max char width for account dropdown in receive (#11076)
1 parent 1b246ab commit c19504b

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

src/components/AccountDropdown/AccountChildOption.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import type { MenuItemOptionProps } from '@chakra-ui/react'
2-
import { forwardRef, MenuItemOption, Stack, useColorModeValue } from '@chakra-ui/react'
2+
import {
3+
forwardRef,
4+
MenuItemOption,
5+
Stack,
6+
useBreakpointValue,
7+
useColorModeValue,
8+
} from '@chakra-ui/react'
39
import type { AccountId } from '@shapeshiftoss/caip'
4-
import { useCallback } from 'react'
10+
import { useCallback, useMemo } from 'react'
511

612
import { Amount } from '@/components/Amount/Amount'
713
import { RawText } from '@/components/Text'
14+
import { trimWithEndEllipsis } from '@/lib/utils'
815

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

2231
return (
2332
<MenuItemOption ref={ref} color={color} onClick={handleClick} {...props}>
2433
<Stack direction='row' fontSize='sm' spacing={4} width='full'>
2534
<RawText fontWeight='bold' whiteSpace='nowrap' flex={1}>
26-
{title}
35+
{truncatedTitle}
2736
</RawText>
2837
<Amount.Crypto
2938
whiteSpace='nowrap'
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
import { Stack } from '@chakra-ui/react'
1+
import { Stack, useBreakpointValue } from '@chakra-ui/react'
22
import type { FC } from 'react'
3+
import { useMemo } from 'react'
34

45
import { RawText } from '@/components/Text'
6+
import { trimWithEndEllipsis } from '@/lib/utils'
57

68
type AccountGroupProps = {
79
title: string
810
subtitle?: string
911
}
1012

11-
export const AccountSegment: FC<AccountGroupProps> = ({ title, subtitle }) => (
12-
<Stack
13-
direction='row'
14-
color='text.subtle'
15-
fontSize='sm'
16-
alignItems='center'
17-
justifyContent='space-between'
18-
width='full'
19-
>
20-
<RawText>{title}</RawText>
21-
{subtitle && (
22-
<RawText fontFamily='monospace' fontWeight='bold'>
23-
{subtitle}
24-
</RawText>
25-
)}
26-
</Stack>
27-
)
13+
export const AccountSegment: FC<AccountGroupProps> = ({ title, subtitle }) => {
14+
const maxLength = useBreakpointValue({ base: 25, md: 50 })
15+
const truncatedSubtitle = useMemo(
16+
() => trimWithEndEllipsis(subtitle, maxLength),
17+
[subtitle, maxLength],
18+
)
19+
20+
return (
21+
<Stack
22+
direction='row'
23+
color='text.subtle'
24+
fontSize='sm'
25+
alignItems='center'
26+
justifyContent='space-between'
27+
width='full'
28+
>
29+
<RawText>{title}</RawText>
30+
{subtitle && (
31+
<RawText fontFamily='monospace' fontWeight='bold'>
32+
{truncatedSubtitle}
33+
</RawText>
34+
)}
35+
</Stack>
36+
)
37+
}

src/lib/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const trimWithEndEllipsis = (content?: string, trimmedContentLength?: num
2424

2525
if (!trimmedContentLength) return content
2626

27-
if (content.length < trimmedContentLength) return content
27+
if (content.length <= trimmedContentLength) return content
2828

2929
return content.slice(0, trimmedContentLength).concat('...')
3030
}

src/state/slices/portfolioSlice/utils/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,10 @@ describe('trimWithEndEllipsis', () => {
154154
expect(trimWithEndEllipsis('abcdef', 191)).toEqual('abcdef')
155155
expect(trimWithEndEllipsis(LongFoxDescription, 191)).toEqual(ExpectedTrimmedFoxDescription)
156156
})
157+
158+
it('should not add ellipsis when content length equals max length', () => {
159+
const exactly50Chars = 'BlackRock USD Institutional Digital Liquidity Fund'
160+
expect(exactly50Chars.length).toEqual(50)
161+
expect(trimWithEndEllipsis(exactly50Chars, 50)).toEqual(exactly50Chars)
162+
})
157163
})

0 commit comments

Comments
 (0)