Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
5f8b0a23
Commit
5f8b0a23
authored
Sep 13, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor to HashStringShorten
parent
3b0f5a12
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
22 deletions
+37
-22
AddressLinkWithTooltip.tsx
ui/shared/AddressLinkWithTooltip.tsx
+6
-2
HashStringShorten.tsx
ui/shared/HashStringShorten.tsx
+16
-0
HashStringShortenDynamic.tsx
ui/shared/HashStringShortenDynamic.tsx
+15
-20
No files found.
ui/shared/AddressLinkWithTooltip.tsx
View file @
5f8b0a23
...
...
@@ -3,8 +3,9 @@ import type { HTMLChakraProps } from '@chakra-ui/system';
import
React
from
'
react
'
;
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
'
;
const
FONT_WEIGHT
=
'
600
'
;
...
...
@@ -37,7 +38,10 @@ const AddressLinkWithTooltip = ({ address, type = 'address', truncated, withCopy
lineHeight=
"24px"
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
>
{
withCopy
&&
<
CopyToClipboard
text=
{
address
}
/>
}
</
Flex
>
...
...
ui/shared/HashStringShorten.tsx
0 → 100644
View file @
5f8b0a23
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
;
ui/shared/
AddressWithDots
.tsx
→
ui/shared/
HashStringShortenDynamic
.tsx
View file @
5f8b0a23
...
...
@@ -21,15 +21,12 @@ const HEAD_MIN_LENGTH = 4;
interface
Props
{
address
:
string
;
fontWeight
:
string
|
number
;
truncated
?:
boolean
;
fontWeight
?:
string
|
number
;
}
const
shortenAddress
=
(
address
:
string
)
=>
address
.
slice
(
0
,
4
)
+
'
...
'
+
address
.
slice
(
-
4
);
const
AddressWithDots
=
({
address
,
fontWeight
,
truncated
}:
Props
)
=>
{
const
HashStringShortenDynamic
=
({
address
,
fontWeight
=
'
400
'
}:
Props
)
=>
{
const
addressRef
=
useRef
<
HTMLSpanElement
>
(
null
);
const
[
displayedAddress
,
setAddress
]
=
React
.
useState
(
truncated
?
shortenAddress
(
address
)
:
address
);
const
[
displayedAddress
,
setAddress
]
=
React
.
useState
(
address
);
const
isFontFaceLoaded
=
useFontFaceObserver
([
{
family
:
BODY_TYPEFACE
,
weight
:
String
(
fontWeight
)
as
FontFace
[
'
weight
'
]
},
...
...
@@ -73,23 +70,21 @@ const AddressWithDots = ({ address, fontWeight, truncated }: Props) => {
// but we don't want to create more resize event listeners
// that's why there are separate useEffect hooks
useEffect
(()
=>
{
!
truncated
&&
calculateString
();
},
[
calculateString
,
isFontFaceLoaded
,
truncated
]);
calculateString
();
},
[
calculateString
,
isFontFaceLoaded
]);
useEffect
(()
=>
{
if
(
!
truncated
)
{
const
resizeHandler
=
_debounce
(
calculateString
,
100
);
const
resizeObserver
=
new
ResizeObserver
(
resizeHandler
);
resizeObserver
.
observe
(
document
.
body
);
return
function
cleanup
()
{
resizeObserver
.
unobserve
(
document
.
body
);
};
}
},
[
calculateString
,
truncated
]);
const
resizeHandler
=
_debounce
(
calculateString
,
100
);
const
resizeObserver
=
new
ResizeObserver
(
resizeHandler
);
resizeObserver
.
observe
(
document
.
body
);
return
function
cleanup
()
{
resizeObserver
.
unobserve
(
document
.
body
);
};
},
[
calculateString
]);
const
content
=
<
span
ref=
{
addressRef
}
>
{
displayedAddress
}
</
span
>;
const
isTruncated
=
truncated
||
address
.
length
!==
displayedAddress
.
length
;
const
isTruncated
=
address
.
length
!==
displayedAddress
.
length
;
if
(
isTruncated
)
{
return
(
...
...
@@ -104,4 +99,4 @@ function getWidth(el: HTMLElement) {
return
el
.
getBoundingClientRect
().
width
;
}
export
default
React
.
memo
(
AddressWithDots
);
export
default
React
.
memo
(
HashStringShortenDynamic
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment