-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revamp AccountLink and implement searching for account names
- Loading branch information
Showing
29 changed files
with
413 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,156 @@ | ||
import { FC } from 'react' | ||
import { FC, ReactNode } from 'react' | ||
import { Link as RouterLink } from 'react-router-dom' | ||
import { useScreenSize } from '../../hooks/useScreensize' | ||
import Link from '@mui/material/Link' | ||
import { RouteUtils } from '../../utils/route-utils' | ||
import InfoIcon from '@mui/icons-material/Info' | ||
import Typography from '@mui/material/Typography' | ||
import { COLORS } from '../../../styles/theme/colors' | ||
import { SearchScope } from '../../../types/searchScope' | ||
import { useAccountName } from '../../hooks/useAccountName' | ||
import { trimLongString } from '../../utils/trimLongString' | ||
import Tooltip from '@mui/material/Tooltip' | ||
import { tooltipDelay } from '../../../styles/theme' | ||
import Box from '@mui/material/Box' | ||
import { HighlightedText } from '../HighlightedText' | ||
import { AdaptiveHighlightedText } from '../HighlightedText/AdaptiveHighlightedText' | ||
import { WithTooltip } from '../AdaptiveTrimmer/WithToolTip' | ||
import { AdaptiveTrimmer } from '../AdaptiveTrimmer/AdaptiveTrimmer' | ||
|
||
const WithLink: FC<{ | ||
content: ReactNode | ||
to: string | ||
plain?: boolean | ||
}> = ({ content, to, plain }) => { | ||
return ( | ||
<Typography | ||
variant="mono" | ||
component="span" | ||
sx={{ | ||
maxWidth: '100%', | ||
overflowX: 'hidden', | ||
...(plain | ||
? { color: COLORS.grayExtraDark, fontWeight: 400 } | ||
: { color: COLORS.brandDark, fontWeight: 700 }), | ||
}} | ||
> | ||
{plain ? ( | ||
content | ||
) : ( | ||
<Link component={RouterLink} to={to}> | ||
{content} | ||
</Link> | ||
)} | ||
</Typography> | ||
) | ||
} | ||
|
||
export const AccountLink: FC<{ | ||
scope: SearchScope | ||
address: string | ||
|
||
/** | ||
* Should we always trim the text to a short line? | ||
*/ | ||
alwaysTrim?: boolean | ||
|
||
/** | ||
* Plain mode? (No link required) | ||
*/ | ||
plain?: boolean | ||
}> = ({ scope, address, alwaysTrim, plain }) => { | ||
|
||
/** | ||
* What part of the name should be highlighted (if any) | ||
*/ | ||
highlightedPartOfName?: string | undefined | ||
|
||
/** | ||
* Any extra tooltips to display | ||
* | ||
* (Besides the content necessary because of potential shortening) | ||
*/ | ||
extraTooltip?: ReactNode | ||
}> = ({ scope, address, alwaysTrim, plain, highlightedPartOfName, extraTooltip }) => { | ||
const { isTablet } = useScreenSize() | ||
const { name: accountName } = useAccountName(scope, address) | ||
const to = RouteUtils.getAccountRoute(scope, address) | ||
|
||
const shortMode = alwaysTrim || isTablet // Do we want shortened lines? | ||
|
||
const nameDisplay = accountName ? ( | ||
<Box sx={{ fontWeight: 'bold' }}> | ||
{shortMode ? ( | ||
<Tooltip | ||
arrow | ||
placement="top" | ||
title={accountName} | ||
enterDelay={tooltipDelay} | ||
enterNextDelay={tooltipDelay} | ||
> | ||
<span>{trimLongString(accountName, 12, 0)}</span> | ||
</Tooltip> | ||
) : ( | ||
accountName | ||
)} | ||
</Box> | ||
const tooltipPostfix = extraTooltip ? ( | ||
<> | ||
<InfoIcon /> | ||
{extraTooltip} | ||
</> | ||
) : undefined | ||
|
||
const addressDisplay = shortMode ? ( | ||
<Tooltip arrow placement="top" title={address} enterDelay={tooltipDelay} enterNextDelay={tooltipDelay}> | ||
<span>{trimLongString(address, 6, 6)}</span> | ||
</Tooltip> | ||
) : ( | ||
address | ||
) | ||
// Are we in a table? | ||
if (alwaysTrim) { | ||
// In a table, we only ever want one short line | ||
|
||
const label = shortMode ? ( // Working with limited horizontal space | ||
accountName ? ( // Do we want two lines ? | ||
<> | ||
{nameDisplay}({addressDisplay}) | ||
</> | ||
) : ( | ||
// No, only a single short line, either name or address | ||
nameDisplay ?? addressDisplay | ||
return ( | ||
<WithLink | ||
content={ | ||
<WithTooltip | ||
title={ | ||
accountName ? ( | ||
<div> | ||
<Box sx={{ fontWeight: 'bold' }}>{accountName}</Box> | ||
<Box sx={{ fontWeight: 'normal' }}>{address}</Box> | ||
{tooltipPostfix} | ||
</div> | ||
) : ( | ||
address | ||
) | ||
} | ||
content={accountName ? trimLongString(accountName, 12, 0) : trimLongString(address, 6, 6)} | ||
/> | ||
} | ||
to={to} | ||
plain={plain} | ||
/> | ||
) | ||
) : // We can have a long line | ||
accountName ? ( | ||
`${accountName} (${address})` | ||
) : ( | ||
address | ||
) | ||
} | ||
|
||
if (!isTablet) { | ||
// Details in desktop mode. | ||
// We want one long line, with name and address. | ||
|
||
return ( | ||
<WithLink | ||
content={ | ||
<WithTooltip | ||
title={tooltipPostfix} | ||
content={ | ||
accountName ? ( | ||
<span> | ||
<HighlightedText text={accountName} pattern={highlightedPartOfName} /> {address} | ||
</span> | ||
) : ( | ||
address | ||
) | ||
} | ||
/> | ||
} | ||
to={to} | ||
plain={plain} | ||
/> | ||
) | ||
} | ||
|
||
// We need to show the data in details mode on mobile. | ||
// We want two lines, one for name (if available), one for address | ||
// Both line adaptively shortened to fill available space | ||
return ( | ||
<Typography | ||
variant="mono" | ||
component="span" | ||
sx={ | ||
plain | ||
? { color: COLORS.grayExtraDark, fontWeight: 400 } | ||
: { color: COLORS.brandDark, fontWeight: 700 } | ||
<WithLink | ||
content={ | ||
<> | ||
<AdaptiveHighlightedText | ||
text={accountName} | ||
pattern={highlightedPartOfName} | ||
extraTooltip={extraTooltip} | ||
/> | ||
<AdaptiveTrimmer text={address} strategy="middle" extraTooltip={extraTooltip} /> | ||
</> | ||
} | ||
> | ||
{plain ? ( | ||
label | ||
) : ( | ||
<Link component={RouterLink} to={to}> | ||
{label} | ||
</Link> | ||
)} | ||
</Typography> | ||
to={to} | ||
plain={plain} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.