|
| 1 | +import React from 'react'; |
| 2 | +import { Avatar, AvatarProps } from '../avatar/avatar'; |
| 3 | + |
| 4 | +import styled from 'styled-components'; |
| 5 | +import BodyText from '../body-text/body-text.tsx'; |
| 6 | +import Tooltip from '../tooltip/tooltip.tsx'; |
| 7 | +import { HashLink } from '../hash-link/hash-link.tsx'; |
| 8 | +import FlexRow from '../flex-row/flex-row.tsx'; |
| 9 | +import FlexColumn from '../flex-column/flex-column.tsx'; |
| 10 | +import { HashLength, shortenCsprName } from '../../utils/formatters.ts'; |
| 11 | +import { Size } from '../../types.ts'; |
| 12 | +import TruncateBox from '../truncate-box/truncate-box.tsx'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Address component can be used to display a public key or hash associated with an account. |
| 16 | + * It supports various configurations, including loading state, logo, name, csprName, and tooltip. |
| 17 | + * |
| 18 | + * Properties: |
| 19 | + * @property {boolean} loading - Specifies whether the address component is in a loading state. |
| 20 | + * @property {string | null} logo - The logo associated with the address, if available. |
| 21 | + * @property {string | undefined} name - The display name of the address. |
| 22 | + * @property {string | undefined} [csprName] - The CSPR.name associated with the address, if applicable. |
| 23 | + * @property {string | null | undefined} hash - The public key or hash associated with the address. |
| 24 | + * @property {string} [tooltipCaption] - Text to be displayed in a tooltip for additional context. |
| 25 | + * @property {string} [navigateToPath] - The path to navigate to when interacting with the address. |
| 26 | + * @property {HashLength} [hashLength] - Specifies the length of the hash representation. |
| 27 | + * @property {Size} [nameTruncateSize] - Defines the size of the name text. |
| 28 | + * @property {AvatarProps['size']} [avatarSize] - The size of the avatar related to the address. |
| 29 | + * @property {HashFontSize} [hashFontSize] - Specifies the font size to display the hash. |
| 30 | + * @property {boolean} [minifiedCopyNotification] - Determines if the address component should be rendered in a minimized style. |
| 31 | + * @property {keyof any} [navigationPath] - **@deprecated** Use `navigateToPath` instead. |
| 32 | + * @property {'full' | 'tiny'} [copyNotifyingStyle] - **@deprecated** Use `minifiedCopyNotification` instead. |
| 33 | + */ |
| 34 | +interface AddressProps { |
| 35 | + loading: boolean; |
| 36 | + logo: string | null; |
| 37 | + name: string | undefined; |
| 38 | + hash: string | null | undefined; |
| 39 | + csprName?: string | undefined; |
| 40 | + tooltipCaption?: string; |
| 41 | + additionalTooltipBlock?: React.ReactElement; |
| 42 | + navigateToPath?: string; |
| 43 | + hashLength?: HashLength; |
| 44 | + nameTruncateSize?: Size; |
| 45 | + avatarSize?: AvatarProps['size']; |
| 46 | + hashFontSize?: HashFontSize; |
| 47 | + minifiedCopyNotification?: boolean; |
| 48 | + /** @deprecated use *navigateToPath* instead */ |
| 49 | + navigationPath?: keyof any; |
| 50 | + /** @deprecated use *minifiedCopyNotification* instead */ |
| 51 | + copyNotifyingStyle?: 'full' | 'tiny'; |
| 52 | +} |
| 53 | + |
| 54 | +const StyledTruncateBox = styled(TruncateBox)(() => ({ |
| 55 | + height: '20px', |
| 56 | +})); |
| 57 | + |
| 58 | +const StyledBodyText = styled(BodyText)(({ theme }) => ({ |
| 59 | + color: theme.styleguideColors.contentBlue, |
| 60 | + '& > *': { |
| 61 | + color: theme.styleguideColors.contentBlue, |
| 62 | + }, |
| 63 | + '&:hover > *': { |
| 64 | + color: theme.styleguideColors.fillPrimaryRed, |
| 65 | + }, |
| 66 | + '&:active > *': { |
| 67 | + color: theme.styleguideColors.fillPrimaryRedClick, |
| 68 | + }, |
| 69 | +})); |
| 70 | + |
| 71 | +export enum HashFontSize { |
| 72 | + 'default' = 'default', |
| 73 | + 'big' = 'big', |
| 74 | +} |
| 75 | + |
| 76 | +export const Address = ({ |
| 77 | + hash, |
| 78 | + csprName, |
| 79 | + logo, |
| 80 | + name, |
| 81 | + loading, |
| 82 | + hashLength, |
| 83 | + minifiedCopyNotification, |
| 84 | + navigateToPath, |
| 85 | + tooltipCaption, |
| 86 | + additionalTooltipBlock, |
| 87 | + nameTruncateSize = 5, |
| 88 | + avatarSize = 'default', |
| 89 | + hashFontSize = HashFontSize.default, |
| 90 | +}: AddressProps) => { |
| 91 | + if (loading || !hash) { |
| 92 | + return ( |
| 93 | + <FlexRow align="center" itemsSpacing={12}> |
| 94 | + <Avatar hash={hash} loading={loading} size={avatarSize} /> |
| 95 | + </FlexRow> |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + if (hash === '00') { |
| 100 | + // hash == '00' means that it is a Immediate Switch Block |
| 101 | + // NOTE: as part of Casper network node v1.5, the node software now creates an "immediate switch block" on upgrades; |
| 102 | + // there are no rewards for this block. it simply captures the information after application of the upgrade, |
| 103 | + // which allows this to be deterministically detected |
| 104 | + return ( |
| 105 | + <FlexRow align="center" itemsSpacing={12}> |
| 106 | + <Avatar hash={hash} loading={loading} size={avatarSize} /> |
| 107 | + <FlexColumn> |
| 108 | + <BodyText size={2} monotype> |
| 109 | + {hash} |
| 110 | + </BodyText> |
| 111 | + <BodyText size={3} variation="darkGray" noWrap> |
| 112 | + System |
| 113 | + </BodyText> |
| 114 | + </FlexColumn> |
| 115 | + </FlexRow> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + return ( |
| 120 | + <FlexRow align="center" itemsSpacing={12}> |
| 121 | + {logo ? ( |
| 122 | + <Avatar |
| 123 | + src={logo} |
| 124 | + loading={loading} |
| 125 | + size={avatarSize} |
| 126 | + alt={'Account logo'} |
| 127 | + /> |
| 128 | + ) : ( |
| 129 | + <Avatar hash={hash} loading={loading} size={avatarSize} /> |
| 130 | + )} |
| 131 | + |
| 132 | + <Tooltip |
| 133 | + caption={tooltipCaption} |
| 134 | + tooltipContent={hash} |
| 135 | + additionalBlock={additionalTooltipBlock} |
| 136 | + > |
| 137 | + <FlexColumn> |
| 138 | + {name ? ( |
| 139 | + <> |
| 140 | + <StyledBodyText |
| 141 | + size={3} |
| 142 | + scale={hashFontSize === HashFontSize.big ? 'sm' : undefined} |
| 143 | + monotype={!csprName} |
| 144 | + > |
| 145 | + <HashLink |
| 146 | + minified={minifiedCopyNotification} |
| 147 | + href={navigateToPath} |
| 148 | + hash={hash} |
| 149 | + csprName={ |
| 150 | + csprName && shortenCsprName(csprName, HashLength.TINY) |
| 151 | + } |
| 152 | + hashLength={hashLength} |
| 153 | + /> |
| 154 | + </StyledBodyText> |
| 155 | + <FlexRow itemsSpacing={6} align={'center'}> |
| 156 | + <StyledTruncateBox size={nameTruncateSize}> |
| 157 | + <BodyText size={3} variation="darkGray" noWrap> |
| 158 | + {name} |
| 159 | + </BodyText> |
| 160 | + </StyledTruncateBox> |
| 161 | + </FlexRow> |
| 162 | + </> |
| 163 | + ) : ( |
| 164 | + <StyledBodyText |
| 165 | + size={3} |
| 166 | + scale={hashFontSize === HashFontSize.big ? 'sm' : undefined} |
| 167 | + monotype={!csprName} |
| 168 | + > |
| 169 | + <HashLink |
| 170 | + href={navigateToPath} |
| 171 | + hash={hash} |
| 172 | + csprName={ |
| 173 | + csprName && shortenCsprName(csprName, HashLength.TINY) |
| 174 | + } |
| 175 | + hashLength={hashLength} |
| 176 | + minified={minifiedCopyNotification} |
| 177 | + /> |
| 178 | + </StyledBodyText> |
| 179 | + )} |
| 180 | + </FlexColumn> |
| 181 | + </Tooltip> |
| 182 | + </FlexRow> |
| 183 | + ); |
| 184 | +}; |
0 commit comments