Skip to content

Commit

Permalink
refactor: introduce hook for primary names
Browse files Browse the repository at this point in the history
  • Loading branch information
kunstmusik committed Nov 26, 2024
1 parent e125cea commit ae6172a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
23 changes: 3 additions & 20 deletions src/components/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* eslint-disable tailwindcss/migration-from-tailwind-2 */
import { AoPrimaryName } from '@ar.io/sdk/web';
import { Popover, PopoverButton, PopoverPanel } from '@headlessui/react';
import useBalances from '@src/hooks/useBalances';
import usePrimaryName from '@src/hooks/usePrimaryName';
import { useGlobalState } from '@src/store';
import {
formatBalance,
formatPrimaryName,
formatWalletAddress,
} from '@src/utils';
import { forwardRef, ReactElement, useEffect, useState } from 'react';
import { forwardRef, ReactElement, useState } from 'react';
import Button, { ButtonType } from './Button';
import CopyButton from './CopyButton';
import Placeholder from './Placeholder';
Expand Down Expand Up @@ -44,30 +44,13 @@ const Profile = () => {
(state) => state.walletStateInitialized,
);
const wallet = useGlobalState((state) => state.wallet);
const arIOReadSDK = useGlobalState((state) => state.arIOReadSDK);
const updateWallet = useGlobalState((state) => state.updateWallet);
const walletAddress = useGlobalState((state) => state.walletAddress);
const { data: balances } = useBalances(walletAddress);
const ticker = useGlobalState((state) => state.ticker);

const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [primaryName, setPrimaryName] = useState<AoPrimaryName>();

useEffect(() => {
const update = async () => {
if (walletAddress && arIOReadSDK) {
try {
const primaryName = await arIOReadSDK.getPrimaryName({
address: walletAddress.toString(),
});
setPrimaryName(primaryName);
} catch (e) {
setPrimaryName(undefined);
}
}
};
update();
}, [walletAddress, arIOReadSDK]);
const { data: primaryName } = usePrimaryName(walletAddress?.toString());

return walletAddress ? (
<Popover className="relative">
Expand Down
23 changes: 23 additions & 0 deletions src/hooks/usePrimaryName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useGlobalState } from '@src/store';
import { useQuery } from '@tanstack/react-query';

const usePrimaryName = (walletAddress?: string) => {
const arIOReadSDK = useGlobalState((state) => state.arIOReadSDK);

const res = useQuery({
queryKey: ['primaryName', walletAddress],
queryFn: async () => {
if (!walletAddress || !arIOReadSDK) {
throw new Error('Wallet Address or SDK not available');
}
const primaryName = await arIOReadSDK.getPrimaryName({
address: walletAddress.toString(),
});
return primaryName;
},
});

return res;
};

export default usePrimaryName;

0 comments on commit ae6172a

Please sign in to comment.