Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ENS resolver hooks, display results in the console (Ready for review) #105

Merged
merged 17 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/AccountAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const AccountAddressFormatted: FC<{
const addressBookEntry = useAppSelector((state) =>
addressBookSelectors.selectById(state, createEntryId(network, address))
);

const parsedAddress = ellipsis
? ellipsisAddress(ethers.utils.getAddress(address), ellipsis)
: ethers.utils.getAddress(address);
Expand Down
12 changes: 9 additions & 3 deletions src/components/AddressBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
Divider,
IconButton,
SvgIconProps,
TextField,
Tooltip,
Avatar,
} from "@mui/material";
import { FC, useEffect, useState } from "react";
import StarIcon from "@mui/icons-material/Star";
Expand All @@ -23,6 +23,7 @@ import {
getEntryId,
} from "../redux/slices/addressBook.slice";
import { Network } from "../redux/networks";
import { ensApi } from "../redux/slices/ensResolver.slice";
import { ethers } from "ethers";

export const AddressBookButton: FC<{
Expand All @@ -35,6 +36,10 @@ export const AddressBookButton: FC<{
);
const [isDialogOpen, setIsDialogOpen] = useState(false);

const avatarUrl = ensApi.useLookupAvatarQuery(
address
)

return (
<>
<Tooltip
Expand All @@ -54,6 +59,7 @@ export const AddressBookButton: FC<{
open={isDialogOpen}
handleClose={() => setIsDialogOpen(false)}
/>
{avatarUrl.currentData ? <Avatar alt={address} src={avatarUrl.currentData?.avatar} /> : ''}
</>
);
};
Expand All @@ -70,6 +76,7 @@ export const AddressBookDialog: FC<{
);

const getInitialNameTag = () => existingEntry?.nameTag ?? "";

const [nameTag, setNameTag] = useState<string>(getInitialNameTag());

// Fixes: https://github.com/superfluid-finance/superfluid-console/issues/21
Expand Down Expand Up @@ -112,7 +119,6 @@ export const AddressBookDialog: FC<{
</Box>
<Divider />
<DialogContent>
<DialogContentText></DialogContentText>
<TextField
autoFocus
margin="dense"
Expand All @@ -121,7 +127,7 @@ export const AddressBookDialog: FC<{
type="text"
fullWidth
variant="standard"
value={nameTag}
value={`${nameTag}`}
onChange={(event) => setNameTag(event.target.value)}
/>
</DialogContent>
Expand Down
29 changes: 29 additions & 0 deletions src/components/AddressName.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { memo } from "react";
import { useAddressName } from "../hooks/useAddressENS";
import {shortenHex} from "../utils/shortenHex";

export interface AddressNameProps {
address: string;
length?: "short" | "medium" | "long";
}

export default memo(function AddressName({
address,
length = "short",
}: AddressNameProps) {
const addressName = useAddressName(address);
if (addressName.name) {
return <>{addressName.name}</>;
} else {
return (
<>
{length === "long"
? addressName.addressChecksummed
: shortenHex(
addressName.addressChecksummed,
length === "medium" ? 8 : 4
)}
</>
);
}
});
2 changes: 1 addition & 1 deletion src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const SearchBar: FC<PropsWithChildren<unknown>> = ({ children}) => {

export default SearchBar;

export const searchBarPlaceholderText = "Search any address on any network..."
export const searchBarPlaceholderText = "Search any ENS, address on any network..."

// NOTE: Search bar UI is inspired by: https://mui.com/components/app-bar/#app-bar-with-a-primary-search-field

Expand Down
16 changes: 12 additions & 4 deletions src/components/SearchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const SearchDialog: FC<{ open: boolean; close: () => void }> = ({
};

const router = useRouter();

const [searchTermVisible, setSearchTermVisible] = useState("");
const [searchTermDebounced, _setSearchTermDebounced] =
useState(searchTermVisible);
Expand All @@ -67,9 +66,7 @@ const SearchDialog: FC<{ open: boolean; close: () => void }> = ({
const handleRouteChange = () => {
handleClose();
};

router.events.on("routeChangeStart", handleRouteChange);

// If the component is unmounted, unsubscribe
// from the event with the `off` method:
return () => {
Expand Down Expand Up @@ -139,7 +136,18 @@ const SearchDialog: FC<{ open: boolean; close: () => void }> = ({
href={`/${x.network.slugName}/accounts/${account.id}`}
passHref
>
<ListItemButton component="a">
<ListItemButton component="a"
sx={{ display: 'flex', alignItems: 'flex-start',
flexDirection: 'column', justifyContent: 'flex-start' }}>
{
account.ENS.includes('.') ?
<Typography sx={{ mr: 2, mb: 0.5 }} variant={"h5"} component="h2">
{account.ENS}
</Typography>
:
''
}

<AccountAddressFormatted
network={x.network}
address={account.id}
Expand Down
2 changes: 0 additions & 2 deletions src/components/SfAppBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Box,
AppBar,
Button,
Toolbar,
Container,
Stack,
Expand All @@ -14,7 +13,6 @@ import SearchDialog from "./SearchDialog";
import Image from "next/image";
import SearchBar from "./SearchBar";
import { FC, useState } from "react";
import SubgraphIcon from "./SubgraphIcon";
import SettingsIcon from "@mui/icons-material/SettingsOutlined";
import SettingsDrawer from "./SettingsDrawer";

Expand Down
65 changes: 65 additions & 0 deletions src/hooks/useAddressENS.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ensApi } from "../redux/slices/ensResolver.slice";
import { useMemo } from "react";
import {ethers} from "ethers";

interface AddressNameResult {
addressChecksummed: string;
name: string;
avatar: string;
isFetching: boolean;
}

export const useAddressName = (address: string): AddressNameResult => {
const isSearchTermAddress = useMemo(
() => ethers.utils.isAddress(address),
[address]
);

if(isSearchTermAddress){
return useAddress(address);
}

else{
return useName(address);
}
};

export const useName = (address: string): AddressNameResult => {
try{
const ensAddressQuery = ensApi.useResolveNameQuery(address);
return {
addressChecksummed: ensAddressQuery.currentData?.address ?? "",
name: address,
avatar: '',
isFetching: ensAddressQuery.isFetching,
};
}
catch(e){
return {
addressChecksummed: address,
name: address,
avatar: "",
isFetching: false,
};
}
}

export const useAddress = (address: string): AddressNameResult => {
try{
const ensLookupQuery = ensApi.useLookupAddressQuery(address);
Seroxdesign marked this conversation as resolved.
Show resolved Hide resolved
return {
addressChecksummed: address,
name: ensLookupQuery.data?.name ?? "",
avatar: "",
isFetching: ensLookupQuery.isFetching,
};
}
catch(e){
return {
addressChecksummed: address,
name: address,
avatar: "",
isFetching: false,
};
}
}
12 changes: 9 additions & 3 deletions src/hooks/useSearch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {Network, networks, networksByChainId} from "../redux/networks";
import _ from "lodash";
import {ethers} from "ethers";
import { useAddressName } from "./useAddressENS";
import {useSearchAddressBook} from "./useSearchAddressBook";
import {SubgraphSearchByAddressResult, useSearchSubgraphByAddress} from "./useSearchSubgraphByAddress";
import {SubgraphSearchByTokenSymbolResult, useSearchSubgraphByTokenSymbol} from "./useSearchSubgraphByTokenSymbol";
import { useEffect } from "react";
import { SerializedError } from "@reduxjs/toolkit";

export type NetworkSearchResult = {
Expand All @@ -22,7 +24,10 @@ export type NetworkSearchResult = {
};

export const useSearch = (searchTerm: string) => {
const subgraphSearchByAddressResults = useSearchSubgraphByAddress(searchTerm);

const searchResults = useAddressName(searchTerm);

const subgraphSearchByAddressResults = useSearchSubgraphByAddress(searchResults.addressChecksummed);
const subgraphSearchByTokenSymbolResults =
useSearchSubgraphByTokenSymbol(searchTerm);
const addressBookResults = useSearchAddressBook(searchTerm);
Expand Down Expand Up @@ -102,7 +107,8 @@ export const useSearch = (searchTerm: string) => {
network: network,
isFetching:
searchByAddressMappedResult.isFetching ||
searchByTokenSymbolMappedResult.isFetching,
searchByTokenSymbolMappedResult.isFetching ||
searchResults.isFetching,
error:
searchByAddressMappedResult.error &&
searchByTokenSymbolMappedResult.error,
Expand All @@ -119,7 +125,7 @@ export const useSearch = (searchTerm: string) => {
accounts: _.uniqBy(
searchByAddressMappedResult.accounts
.concat(addressBookResult.accounts)
.map((x) => ({...x, id: ethers.utils.getAddress(x.id)})),
.map((x) => ({...x, id: ethers.utils.getAddress(x.id), ENS: searchResults.name})),
(x) => x.id
),
};
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSearchAddressBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useSearchAddressBook = (searchTerm: string) => {

return networks.map((network) => {
const addressBookEntries = useAppSelector((state) =>
searchTerm !== "" && !isSearchTermAddress
searchTerm !== "" && !isSearchTermAddress
? addressBookSelectors
.selectAll(state)
.filter((x) => x.chainId === network.chainId)
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useSearchSubgraphByAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export const useSearchSubgraphByAddress = (searchTerm: string) => {
addressBytes: searchTerm.toLowerCase(),
},
}
: skipToken
:
skipToken
)
);
};
31 changes: 23 additions & 8 deletions src/pages/[_network]/accounts/[_id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import InfoTooltipBtn from "../../../components/InfoTooltipBtn";
import AccountNetworkSelect from "../../../components/NetworkSelect/AccountNetworkSelect";
import SkeletonAddress from "../../../components/skeletons/SkeletonAddress";
import SkeletonNetwork from "../../../components/skeletons/SkeletonNetwork";
import { ensApi } from "../../../redux/slices/ensResolver.slice";
import SubgraphQueryLink from "../../../components/SubgraphQueryLink";
import {
incomingStreamOrderingDefault,
Expand Down Expand Up @@ -64,12 +65,15 @@ import ellipsisAddress from "../../../utils/ellipsisAddress";
const AccountPage: NextPage = () => {
const network = useContext(NetworkContext);
const address = useContext(IdContext);

const accountQuery = sfSubgraph.useAccountQuery({
chainId: network.chainId,
id: address,
});

const ensAddressQuery = ensApi.useLookupAddressQuery(address);

const ensName = ensAddressQuery.currentData?.name

const prefetchStreamsQuery = sfSubgraph.usePrefetch("streams");
const prefetchIndexesQuery = sfSubgraph.usePrefetch("indexes");
const prefetchIndexSubscriptionsQuery =
Expand Down Expand Up @@ -109,7 +113,7 @@ const AccountPage: NextPage = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [tabValue]);

const addressBookEntry = useAppSelector((state) =>
const addressBookEntry = useAppSelector((state: any) =>
network
? addressBookSelectors.selectById(state, createEntryId(network, address))
: undefined
Expand Down Expand Up @@ -149,18 +153,29 @@ const AccountPage: NextPage = () => {
network={network}
address={accountQuery.data.id}
/>
<Typography
data-cy={"ensName"}
variant="h4"
component="h1"
sx={{ mx: 1 }}
>
{
addressBookEntry ?
addressBookEntry.nameTag
:
ensName
}
</Typography>
<Typography
data-cy={"address"}
variant="h4"
component="h1"
sx={{ mx: 1 }}
>
{addressBookEntry
? addressBookEntry.nameTag
: ellipsisAddress(
ethers.utils.getAddress(accountQuery.data.id),
6
)}
{ellipsisAddress(
ethers.utils.getAddress(accountQuery.data.id),
6
)}
</Typography>
<CopyIconBtn
copyText={ethers.utils.getAddress(accountQuery.data.id)}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CacheProvider, EmotionCache } from "@emotion/react";
import createEmotionCache from "../utils/createEmotionCache";
import { wrapper } from "../redux/store";
import SfAppBar from "../components/SfAppBar";
import { FC, PropsWithChildren, useEffect, useRef, useState } from "react";
import { FC, PropsWithChildren, useEffect, useRef } from "react";
import Box from "@mui/material/Box";
import "../styles/graphiql.min.css";
import "../styles/app.css";
Expand Down
Loading