Skip to content

Commit

Permalink
[accounts] Add ANS and labels together
Browse files Browse the repository at this point in the history
Resolves #849
  • Loading branch information
gregnazario committed Feb 5, 2025
1 parent 9ee86f4 commit 63dc39e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
33 changes: 20 additions & 13 deletions src/api/hooks/useGetANS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
standardizeAddress,
} from "../../utils";
import {ResponseError} from "../client";
import {NameType} from "../../components/TitleHashButton";

const TTL = 60000; // 1 minute

Expand All @@ -31,26 +32,32 @@ export function useGetNameFromAddress(
address: string,
shouldCache = false,
isValidator = false,
nameType = NameType.ANY,
) {
const [state] = useGlobalState();
const queryResult = useQuery<string | null, ResponseError>({
queryKey: ["ANSName", address, shouldCache, state.network_name],
queryKey: ["ANSName", address, shouldCache, state.network_name, nameType],
queryFn: () => {
const standardizedAddress = standardizeAddress(address);
const lowercaseStandardizedAddress = standardizedAddress.toLowerCase();
const knownName = knownAddresses[lowercaseStandardizedAddress];
if (knownName) {
return knownName;
}
const scamName = scamAddresses[lowercaseStandardizedAddress];
if (scamName) {
return scamName;
}
if (nameType !== NameType.ANS) {
const knownName = knownAddresses[lowercaseStandardizedAddress];
if (knownName) {
return knownName;
}
const scamName = scamAddresses[lowercaseStandardizedAddress];
if (scamName) {
return scamName;
}

// Change cache key specifically to invalidate all previous cached keys
const cachedName = getLocalStorageWithExpiry(`${address}:name`);
if (cachedName) {
return cachedName;
// Change cache key specifically to invalidate all previous cached keys
const cachedName = getLocalStorageWithExpiry(`${address}:name`);
if (cachedName) {
return cachedName;
}
if (nameType === NameType.LABEL) {
return null;
}
}
// Ensure there's always .apt at the end
return genANSName(
Expand Down
24 changes: 21 additions & 3 deletions src/components/TitleHashButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ interface TitleHashButtonProps {
hash: string;
type: HashType;
isValidator?: boolean;
nameType?: NameType;
}

export enum NameType {
ANY = "any",
ANS = "ans",
LABEL = "label",
}

export default function TitleHashButton({
hash,
type,
isValidator = false,
nameType = NameType.ANY,
}: TitleHashButtonProps) {
if (type !== HashType.NAME) {
return <HashButton hash={hash} />;
} else {
return <Name address={hash} isValidator={isValidator} />;
return (
<Name address={hash} isValidator={isValidator} nameType={nameType} />
);
}
}

Expand Down Expand Up @@ -122,9 +132,17 @@ function HashButton({hash}: {hash: string}) {
);
}

function Name({address, isValidator}: {address: string; isValidator: boolean}) {
function Name({
address,
isValidator,
nameType,
}: {
address: string;
isValidator: boolean;
nameType?: NameType;
}) {
const theme = useTheme();
const name = useGetNameFromAddress(address, true, isValidator);
const name = useGetNameFromAddress(address, true, isValidator, nameType);

if (!name) {
return null;
Expand Down
16 changes: 14 additions & 2 deletions src/pages/Account/Title.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {Stack, Typography} from "@mui/material";
import React from "react";
import TitleHashButton, {HashType} from "../../components/TitleHashButton";
import TitleHashButton, {
HashType,
NameType,
} from "../../components/TitleHashButton";
import {usePageMetadata} from "../../components/hooks/usePageMetadata";

type AccountTitleProps = {
Expand Down Expand Up @@ -38,7 +41,16 @@ export default function AccountTitle({
<Typography variant="h3">{title}</Typography>
<Stack direction="row" spacing={1}>
<TitleHashButton hash={address} type={HashType.ACCOUNT} />
<TitleHashButton hash={address} type={HashType.NAME} />
<TitleHashButton
hash={address}
type={HashType.NAME}
nameType={NameType.LABEL}
/>
<TitleHashButton
hash={address}
type={HashType.NAME}
nameType={NameType.ANS}
/>
</Stack>
</Stack>
);
Expand Down

0 comments on commit 63dc39e

Please sign in to comment.