Commit 5f8b0a23 authored by tom's avatar tom

refactor to HashStringShorten

parent 3b0f5a12
...@@ -3,8 +3,9 @@ import type { HTMLChakraProps } from '@chakra-ui/system'; ...@@ -3,8 +3,9 @@ import type { HTMLChakraProps } from '@chakra-ui/system';
import React from 'react'; import React from 'react';
import useBasePath from 'lib/hooks/useBasePath'; import useBasePath from 'lib/hooks/useBasePath';
import HashStringShorten from 'ui/shared/HashStringShorten';
import HashStringShortenDynamic from 'ui/shared/HashStringShortenDynamic';
import AddressWithDots from './AddressWithDots';
import CopyToClipboard from './CopyToClipboard'; import CopyToClipboard from './CopyToClipboard';
const FONT_WEIGHT = '600'; const FONT_WEIGHT = '600';
...@@ -37,7 +38,10 @@ const AddressLinkWithTooltip = ({ address, type = 'address', truncated, withCopy ...@@ -37,7 +38,10 @@ const AddressLinkWithTooltip = ({ address, type = 'address', truncated, withCopy
lineHeight="24px" lineHeight="24px"
whiteSpace="nowrap" whiteSpace="nowrap"
> >
<AddressWithDots address={ address } fontWeight={ styles.fontWeight || FONT_WEIGHT } truncated={ truncated }/> { truncated ?
<HashStringShorten address={ address }/> :
<HashStringShortenDynamic address={ address } fontWeight={ styles.fontWeight || FONT_WEIGHT }/>
}
</Link> </Link>
{ withCopy && <CopyToClipboard text={ address }/> } { withCopy && <CopyToClipboard text={ address }/> }
</Flex> </Flex>
......
import { Tooltip } from '@chakra-ui/react';
import React from 'react';
interface Props {
address: string;
}
const HashStringShorten = ({ address }: Props) => {
return (
<Tooltip label={ address }>
{ address.slice(0, 4) + '...' + address.slice(-4) }
</Tooltip>
);
};
export default HashStringShorten;
...@@ -21,15 +21,12 @@ const HEAD_MIN_LENGTH = 4; ...@@ -21,15 +21,12 @@ const HEAD_MIN_LENGTH = 4;
interface Props { interface Props {
address: string; address: string;
fontWeight: string | number; fontWeight?: string | number;
truncated?: boolean;
} }
const shortenAddress = (address: string) => address.slice(0, 4) + '...' + address.slice(-4); const HashStringShortenDynamic = ({ address, fontWeight = '400' }: Props) => {
const AddressWithDots = ({ address, fontWeight, truncated }: Props) => {
const addressRef = useRef<HTMLSpanElement>(null); const addressRef = useRef<HTMLSpanElement>(null);
const [ displayedAddress, setAddress ] = React.useState(truncated ? shortenAddress(address) : address); const [ displayedAddress, setAddress ] = React.useState(address);
const isFontFaceLoaded = useFontFaceObserver([ const isFontFaceLoaded = useFontFaceObserver([
{ family: BODY_TYPEFACE, weight: String(fontWeight) as FontFace['weight'] }, { family: BODY_TYPEFACE, weight: String(fontWeight) as FontFace['weight'] },
...@@ -73,23 +70,21 @@ const AddressWithDots = ({ address, fontWeight, truncated }: Props) => { ...@@ -73,23 +70,21 @@ const AddressWithDots = ({ address, fontWeight, truncated }: Props) => {
// but we don't want to create more resize event listeners // but we don't want to create more resize event listeners
// that's why there are separate useEffect hooks // that's why there are separate useEffect hooks
useEffect(() => { useEffect(() => {
!truncated && calculateString(); calculateString();
}, [ calculateString, isFontFaceLoaded, truncated ]); }, [ calculateString, isFontFaceLoaded ]);
useEffect(() => { useEffect(() => {
if (!truncated) { const resizeHandler = _debounce(calculateString, 100);
const resizeHandler = _debounce(calculateString, 100); const resizeObserver = new ResizeObserver(resizeHandler);
const resizeObserver = new ResizeObserver(resizeHandler);
resizeObserver.observe(document.body);
resizeObserver.observe(document.body); return function cleanup() {
return function cleanup() { resizeObserver.unobserve(document.body);
resizeObserver.unobserve(document.body); };
}; }, [ calculateString ]);
}
}, [ calculateString, truncated ]);
const content = <span ref={ addressRef }>{ displayedAddress }</span>; const content = <span ref={ addressRef }>{ displayedAddress }</span>;
const isTruncated = truncated || address.length !== displayedAddress.length; const isTruncated = address.length !== displayedAddress.length;
if (isTruncated) { if (isTruncated) {
return ( return (
...@@ -104,4 +99,4 @@ function getWidth(el: HTMLElement) { ...@@ -104,4 +99,4 @@ function getWidth(el: HTMLElement) {
return el.getBoundingClientRect().width; return el.getBoundingClientRect().width;
} }
export default React.memo(AddressWithDots); export default React.memo(HashStringShortenDynamic);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment