2021-12-17 02:17:13 +00:00
|
|
|
import React, { useContext } from "react";
|
2022-01-29 23:44:26 +00:00
|
|
|
import PlainAddress from "./PlainAddress";
|
2021-12-17 02:17:13 +00:00
|
|
|
import { resolverRendererRegistry } from "../api/address-resolver";
|
|
|
|
import { useResolvedAddress } from "../useResolvedAddresses";
|
|
|
|
import { RuntimeContext } from "../useRuntime";
|
2022-01-29 23:44:26 +00:00
|
|
|
import { ChecksummedAddress } from "../types";
|
2021-07-03 00:40:02 +00:00
|
|
|
|
|
|
|
type AddressOrENSNameProps = {
|
2022-01-29 23:44:26 +00:00
|
|
|
address: ChecksummedAddress;
|
2021-07-03 00:40:02 +00:00
|
|
|
selectedAddress?: string;
|
2021-07-19 03:50:32 +00:00
|
|
|
dontOverrideColors?: boolean;
|
2021-07-03 00:40:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const AddressOrENSName: React.FC<AddressOrENSNameProps> = ({
|
|
|
|
address,
|
|
|
|
selectedAddress,
|
2021-07-19 03:50:32 +00:00
|
|
|
dontOverrideColors,
|
2021-10-31 00:31:31 +00:00
|
|
|
}) => {
|
2021-12-17 02:17:13 +00:00
|
|
|
const { provider } = useContext(RuntimeContext);
|
|
|
|
const resolvedAddress = useResolvedAddress(provider, address);
|
2021-10-31 06:16:03 +00:00
|
|
|
const linkable = address !== selectedAddress;
|
|
|
|
|
2021-11-01 07:15:12 +00:00
|
|
|
if (!resolvedAddress) {
|
2021-10-31 06:16:03 +00:00
|
|
|
return (
|
|
|
|
<PlainAddress
|
|
|
|
address={address}
|
|
|
|
linkable={linkable}
|
|
|
|
dontOverrideColors={dontOverrideColors}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const [resolver, resolvedName] = resolvedAddress;
|
2021-10-31 08:10:42 +00:00
|
|
|
const renderer = resolverRendererRegistry.get(resolver);
|
|
|
|
if (renderer === undefined) {
|
2021-10-31 06:16:03 +00:00
|
|
|
return (
|
2021-10-31 08:10:42 +00:00
|
|
|
<PlainAddress
|
2021-10-31 06:16:03 +00:00
|
|
|
address={address}
|
|
|
|
linkable={linkable}
|
|
|
|
dontOverrideColors={dontOverrideColors}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-31 08:10:42 +00:00
|
|
|
return renderer(address, resolvedName, linkable, !!dontOverrideColors);
|
2021-10-31 00:31:31 +00:00
|
|
|
};
|
2021-07-03 00:40:02 +00:00
|
|
|
|
2021-10-31 01:05:26 +00:00
|
|
|
export default AddressOrENSName;
|