Skip to content

Commit

Permalink
fix(console): refactor useAllowance hook (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
baktun14 authored Aug 26, 2024
1 parent c8ced52 commit 269d227
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.

This file was deleted.

10 changes: 5 additions & 5 deletions apps/deploy-web/src/components/authorizations/Authorizations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { NextSeo } from "next-seo";

import { Fieldset } from "@src/components/shared/Fieldset";
import { useWallet } from "@src/context/WalletProvider";
import { useAllowance } from "@src/hooks/useAllowance";
import { useAllowancesIssued, useGranteeGrants, useGranterGrants } from "@src/queries/useGrantsQuery";
import { AllowanceType, GrantType } from "@src/types/grant";
import { averageBlockTime } from "@src/utils/priceUtils";
Expand All @@ -21,13 +20,17 @@ import { DeploymentGrantTable } from "./DeploymentGrantTable";
import { FeeGrantTable } from "./FeeGrantTable";
import { GranteeRow } from "./GranteeRow";
import { GrantModal } from "./GrantModal";
import { useAllowance } from "@src/hooks/useAllowance";

type RefreshingType = "granterGrants" | "granteeGrants" | "allowancesIssued" | "allowancesGranted" | null;
const defaultRefetchInterval = 30 * 1000;
const refreshingInterval = 1000;

export const Authorizations: React.FunctionComponent = () => {
const { address, signAndBroadcastTx } = useWallet();
const { address, signAndBroadcastTx, isManaged } = useWallet();
const {
fee: { all: allowancesGranted, isLoading: isLoadingAllowancesGranted, setDefault, default: defaultAllowance }
} = useAllowance(address, isManaged);
const [editingGrant, setEditingGrant] = useState<GrantType | null>(null);
const [editingAllowance, setEditingAllowance] = useState<AllowanceType | null>(null);
const [showGrantModal, setShowGrantModal] = useState(false);
Expand All @@ -46,9 +49,6 @@ export const Authorizations: React.FunctionComponent = () => {
const { data: allowancesIssued, isLoading: isLoadingAllowancesIssued } = useAllowancesIssued(address, {
refetchInterval: isRefreshing === "allowancesIssued" ? refreshingInterval : defaultRefetchInterval
});
const {
fee: { all: allowancesGranted, isLoading: isLoadingAllowancesGranted, setDefault, default: defaultAllowance }
} = useAllowance();

useEffect(() => {
let timeout: NodeJS.Timeout;
Expand Down
8 changes: 4 additions & 4 deletions apps/deploy-web/src/context/WalletProvider/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { event } from "nextjs-google-analytics";
import { SnackbarKey, useSnackbar } from "notistack";

import { LoadingState, TransactionModal } from "@src/components/layout/TransactionModal";
import { useAllowance } from "@src/hooks/useAllowance";
import { useUsdcDenom } from "@src/hooks/useDenom";
import { useManagedWallet } from "@src/hooks/useManagedWallet";
import { useUser } from "@src/hooks/useUser";
Expand All @@ -27,6 +26,7 @@ import { UrlService } from "@src/utils/urlUtils";
import { LocalWalletDataType } from "@src/utils/walletUtils";
import { useSelectedChain } from "../CustomChainProvider";
import { useSettings } from "../SettingsProvider";
import { useAllowance } from "@src/hooks/useAllowance";

const ERROR_MESSAGES = {
5: "Insufficient funds",
Expand Down Expand Up @@ -79,14 +79,14 @@ export const WalletProvider = ({ children }) => {
const { settings } = useSettings();
const usdcIbcDenom = useUsdcDenom();
const user = useUser();

const userWallet = useSelectedChain();
const { wallet: managedWallet, isLoading, create, refetch } = useManagedWallet();
const { address: walletAddress, username, isWalletConnected } = useMemo(() => managedWallet || userWallet, [managedWallet, userWallet]);
const { addEndpoints } = useManager();
const isManaged = !!managedWallet;
const {
fee: { default: feeGranter }
} = useAllowance();
} = useAllowance(walletAddress as string, isManaged);

useWhen(managedWallet, refreshBalances);

Expand Down Expand Up @@ -349,7 +349,7 @@ export const WalletProvider = ({ children }) => {
logout,
signAndBroadcastTx,
refreshBalances,
isManaged: !!managedWallet,
isManaged: isManaged,
isWalletLoading: isLoading,
isTrialing: !!managedWallet?.isTrialing,
creditAmount: managedWallet?.creditAmount
Expand Down
21 changes: 11 additions & 10 deletions apps/deploy-web/src/hooks/useAllowance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Link from "next/link";
import { useSnackbar } from "notistack";
import { useLocalStorage } from "usehooks-ts";

import { useWallet } from "@src/context/WalletProvider";
import { useWhen } from "@src/hooks/useWhen";
import { useAllowancesGranted } from "@src/queries/useGrantsQuery";

Expand All @@ -24,15 +23,14 @@ const AllowanceNotificationMessage: FC = () => (
</>
);

export const useAllowance = () => {
const { address, isManaged } = useWallet();
export const useAllowance = (address: string, isManaged: boolean) => {
const [defaultFeeGranter, setDefaultFeeGranter] = useLocalStorage<string | undefined>(`default-fee-granters/${address}`, undefined);
const { data: allFeeGranters, isLoading, isFetched } = useAllowancesGranted(address);
const { enqueueSnackbar } = useSnackbar();

const actualAddresses = useMemo(() => {
const actualAllowanceAddresses = useMemo(() => {
if (!address || !allFeeGranters) {
return [];
return null;
}

return allFeeGranters.reduce((acc, grant) => {
Expand All @@ -45,14 +43,15 @@ export const useAllowance = () => {
}, [allFeeGranters, address]);

useWhen(
isFetched && address && !isManaged,
isFetched && address && !isManaged && !!actualAllowanceAddresses,
() => {
const _actualAllowanceAddresses = actualAllowanceAddresses as string[];
const persistedAddresses = persisted[address] || [];
const added = difference(actualAddresses, persistedAddresses);
const removed = difference(persistedAddresses, actualAddresses);
const added = difference(_actualAllowanceAddresses, persistedAddresses);
const removed = difference(persistedAddresses, _actualAllowanceAddresses);

if (added.length || removed.length) {
persisted[address] = actualAddresses;
persisted[address] = _actualAllowanceAddresses;
localStorage.setItem(`fee-granters`, JSON.stringify(persisted));
}

Expand All @@ -70,9 +69,11 @@ export const useAllowance = () => {

if (defaultFeeGranter && removed.includes(defaultFeeGranter)) {
setDefaultFeeGranter(undefined);
} else if (!defaultFeeGranter && _actualAllowanceAddresses.length) {
setDefaultFeeGranter(_actualAllowanceAddresses[0]);
}
},
[actualAddresses, persisted]
[actualAllowanceAddresses, persisted]
);

return useMemo(
Expand Down
2 changes: 0 additions & 2 deletions apps/deploy-web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Router from "next/router";
import { ThemeProvider } from "next-themes";
import NProgress from "nprogress";

import { AllowanceWatcher } from "@src/components/authorizations/AllowanceWatcher";
import GoogleAnalytics from "@src/components/layout/CustomGoogleAnalytics";
import { CustomIntlProvider } from "@src/components/layout/CustomIntlProvider";
import { PageHead } from "@src/components/layout/PageHead";
Expand Down Expand Up @@ -71,7 +70,6 @@ const App: React.FunctionComponent<Props> = props => {
<BackgroundTaskProvider>
<TemplatesProvider>
<LocalNoteProvider>
<AllowanceWatcher />
<GoogleAnalytics />
<Component {...pageProps} />
</LocalNoteProvider>
Expand Down
8 changes: 4 additions & 4 deletions apps/deploy-web/src/queries/useGrantsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ApiUrlService } from "@src/utils/apiUtils";
import { QueryKeys } from "./queryKeys";

async function getGranterGrants(apiEndpoint: string, address: string) {
if (!address || !apiEndpoint) return [];
if (!address || !apiEndpoint) return undefined;

const response = await axios.get(ApiUrlService.granterGrants(apiEndpoint, address));
const filteredGrants = response.data.grants.filter(
Expand All @@ -26,7 +26,7 @@ export function useGranterGrants(address: string, options = {}) {
}

async function getGranteeGrants(apiEndpoint: string, address: string) {
if (!address || !apiEndpoint) return [];
if (!address || !apiEndpoint) return undefined;

const response = await axios.get(ApiUrlService.granteeGrants(apiEndpoint, address));
const filteredGrants = response.data.grants.filter(
Expand All @@ -47,7 +47,7 @@ export function useGranteeGrants(address: string, options = {}) {
}

async function getAllowancesIssued(apiEndpoint: string, address: string) {
if (!address || !apiEndpoint) return [];
if (!address || !apiEndpoint) return undefined;

const response = await axios.get(ApiUrlService.allowancesIssued(apiEndpoint, address));

Expand All @@ -61,7 +61,7 @@ export function useAllowancesIssued(address: string, options = {}) {
}

async function getAllowancesGranted(apiEndpoint: string, address: string) {
if (!address || !apiEndpoint) return [];
if (!address || !apiEndpoint) return undefined;

const response = await axios.get(ApiUrlService.allowancesGranted(apiEndpoint, address));

Expand Down

0 comments on commit 269d227

Please sign in to comment.