Skip to content

Commit 4cbfd9f

Browse files
authored
Merge pull request #2160 from oasisprotocol/csillag/highlight-different-address-representations
Hover highlight: recognize different representations of the same address
2 parents 31751e2 + a801b28 commit 4cbfd9f

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

.changelog/2160.trivial.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hover highlight: recognize different representations of the same address

src/app/components/HoverHighlightingContext/WithHoverHighlighting.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const WithHoverHighlighting: FC<{ children: ReactNode; address: string }>
88
children,
99
address,
1010
}) => {
11-
const { highlightedAddress, highlightAddress, releaseAddress } = useHoverHighlighting()
11+
const { shouldHighlight, selectAddress, releaseAddress } = useHoverHighlighting()
1212
useEffect(() => {
1313
// Release address on unmount
1414
return () => releaseAddress(address)
@@ -17,11 +17,10 @@ export const WithHoverHighlighting: FC<{ children: ReactNode; address: string }>
1717
const { isTablet } = useScreenSize()
1818
// We have decided that we only want this feature on desktop,
1919
// so we are on tablet (or mobile), just return the wrapped contnt directly.
20-
const isHighlighted =
21-
!isTablet && !!highlightedAddress && highlightedAddress.toLowerCase() === address.toLowerCase()
20+
const isHighlighted = !isTablet && shouldHighlight(address)
2221
return (
2322
<Box
24-
onMouseEnter={() => highlightAddress(address)}
23+
onMouseEnter={() => selectAddress(address)}
2524
onMouseLeave={() => releaseAddress(address)}
2625
sx={{
2726
display: 'inline-flex',

src/app/components/HoverHighlightingContext/index.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
import { createContext, FC, ReactNode, useContext, useState } from 'react'
2+
import { getEvmBech32Address, isValidEthAddress } from '../../utils/helpers'
23

34
interface HoverHighlightingContextInfo {
4-
readonly highlightedAddress: string | undefined
5-
highlightAddress: (value: string) => void
5+
shouldHighlight: (address: string) => boolean
6+
selectAddress: (value: string) => void
67
releaseAddress: (value: string) => void
78
}
89

910
const HoverHighlightingContext = createContext<HoverHighlightingContextInfo | null>(null)
1011

1112
const noContext: HoverHighlightingContextInfo = {
12-
highlightedAddress: undefined,
13-
highlightAddress: () => {},
13+
shouldHighlight: () => false,
14+
selectAddress: () => {},
1415
releaseAddress: () => {},
1516
}
1617

18+
/**
19+
* Convert highlight address to a uniform format:
20+
*/
21+
const normalizeAddress = (address: string) =>
22+
isValidEthAddress(address)
23+
? getEvmBech32Address(address).toLowerCase() // We always want oasis address, not eth
24+
: address.toLowerCase() // wherever else this is, just lowercase it
25+
1726
export const HoverHighlightingContextProvider: FC<{ children: ReactNode }> = ({ children }) => {
18-
const [address, setAddress] = useState<string | undefined>()
19-
const releaseAddress = (oldAddress: string) => {
20-
if (address === oldAddress) setAddress(undefined)
27+
const [selectedAddress, setSelectedAddress] = useState<string | undefined>()
28+
const shouldHighlight = (address: string) =>
29+
!!selectedAddress && selectedAddress === normalizeAddress(address)
30+
const selectAddress = (address: string) => setSelectedAddress(normalizeAddress(address))
31+
const releaseAddress = (address: string) => {
32+
if (selectedAddress === normalizeAddress(address)) setSelectedAddress(undefined)
2133
}
2234
return (
2335
<HoverHighlightingContext.Provider
2436
value={{
25-
highlightedAddress: address,
26-
highlightAddress: setAddress,
37+
shouldHighlight,
38+
selectAddress,
2739
releaseAddress,
2840
}}
2941
>

0 commit comments

Comments
 (0)