Skip to content

Commit

Permalink
update react-query v5.62.0
Browse files Browse the repository at this point in the history
  • Loading branch information
karancoder committed Dec 2, 2024
1 parent 6209154 commit d31c02c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 132 deletions.
2 changes: 1 addition & 1 deletion packages/graz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@dao-dao/cosmiframe": "0.1.0",
"@keplr-wallet/cosmos": "0.12.156",
"@metamask/providers": "12.0.0",
"@tanstack/react-query": "4.35.0",
"@tanstack/react-query": "5.62.0",
"@terra-money/station-connector": "1.1.0",
"@vectis/extension-client": "^0.7.2",
"@walletconnect/sign-client": "2.17.2",
Expand Down
76 changes: 37 additions & 39 deletions packages/graz/src/hooks/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ export const useBalances = <TMulti extends MultiChainHookArgs>(
[address, args?.chainId, chains, clients],
);

return useQuery(
return useQuery({
queryKey,
async ({ queryKey: [, _clients, _chains, _address] }) => {
queryFn: async ({ queryKey: [, _clients, _chains, _address] }) => {
if (!_address) {
throw new Error("address is not defined");
}
Expand All @@ -166,18 +166,16 @@ export const useBalances = <TMulti extends MultiChainHookArgs>(
});
return res;
},
{
enabled:
Boolean(address) &&
Boolean(chains) &&
chains.length > 0 &&
!isEmpty(clients) &&
(args?.enabled === undefined ? true : args.enabled),
refetchOnMount: false,
refetchOnReconnect: true,
refetchOnWindowFocus: false,
},
);
enabled:
Boolean(address) &&
Boolean(chains) &&
chains.length > 0 &&
!isEmpty(clients) &&
(args?.enabled === undefined ? true : args.enabled),
refetchOnMount: false,
refetchOnReconnect: true,
refetchOnWindowFocus: false,
});
};

/**
Expand Down Expand Up @@ -216,19 +214,17 @@ export const useBalance = <TMulti extends MultiChainHookArgs>(

const queryKey = ["USE_BALANCE", args.denom, balances, chains, address, args.chainId] as const;

const query = useQuery(
const query = useQuery({
queryKey,
({ queryKey: [, _denom, _balances] }) => {
queryFn: ({ queryKey: [, _denom, _balances] }) => {
return _balances?.find((x) => x.denom === _denom);
},
{
enabled:
Boolean(args.denom) &&
Boolean(balances) &&
Boolean(balances?.length) &&
(args.enabled === undefined ? true : args.enabled),
},
);
enabled:
Boolean(args.denom) &&
Boolean(balances) &&
Boolean(balances?.length) &&
(args.enabled === undefined ? true : args.enabled),
});

return {
...query,
Expand Down Expand Up @@ -276,8 +272,10 @@ export type UseConnectChainArgs = MutationEventArgs<ConnectArgs, ConnectResult>;
* @see {@link connect}
*/
export const useConnect = ({ onError, onLoading, onSuccess }: UseConnectChainArgs = {}) => {
const queryKey = ["USE_CONNECT", onError, onLoading, onSuccess];
const mutation = useMutation(queryKey, connect, {
const mutationKey = ["USE_CONNECT", onError, onLoading, onSuccess];
const mutation = useMutation({
mutationKey,
mutationFn: connect,
onError: (err, args) => onError?.(err, args),
onMutate: onLoading,
onSuccess: (connectResult) => Promise.resolve(onSuccess?.(connectResult)),
Expand All @@ -287,7 +285,7 @@ export const useConnect = ({ onError, onLoading, onSuccess }: UseConnectChainArg
connect: (args?: ConnectArgs) => mutation.mutate(args),
connectAsync: (args?: ConnectArgs) => mutation.mutateAsync(args),
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
isSupported: Boolean(isSupported),
status: mutation.status,
Expand Down Expand Up @@ -319,8 +317,10 @@ export const useConnect = ({ onError, onLoading, onSuccess }: UseConnectChainArg
* @see {@link disconnect}
*/
export const useDisconnect = ({ onError, onLoading, onSuccess }: MutationEventArgs = {}) => {
const queryKey = ["USE_DISCONNECT", onError, onLoading, onSuccess];
const mutation = useMutation(queryKey, disconnect, {
const mutationKey = ["USE_DISCONNECT", onError, onLoading, onSuccess];
const mutation = useMutation({
mutationKey,
mutationFn: disconnect,
onError: (err) => Promise.resolve(onError?.(err, undefined)),
onMutate: onLoading,
onSuccess: () => Promise.resolve(onSuccess?.(undefined)),
Expand All @@ -330,7 +330,7 @@ export const useDisconnect = ({ onError, onLoading, onSuccess }: MutationEventAr
disconnect: (args?: { chainId?: ChainId }) => mutation.mutate(args),
disconnectAsync: (args?: { chainId?: ChainId }) => mutation.mutateAsync(args),
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
status: mutation.status,
};
Expand Down Expand Up @@ -416,9 +416,9 @@ export const useBalanceStaked = <TMulti extends MultiChainHookArgs>(

const queryKey = useMemo(() => ["USE_BALANCE_STAKED", client, chains, address] as const, [chains, address, client]);

return useQuery(
return useQuery({
queryKey,
async ({ queryKey: [, _client, _chains, _address] }) => {
queryFn: async ({ queryKey: [, _client, _chains, _address] }) => {
if (!_address) {
throw new Error("address is not defined");
}
Expand All @@ -432,11 +432,9 @@ export const useBalanceStaked = <TMulti extends MultiChainHookArgs>(
});
return res;
},
{
enabled: Boolean(address) && Boolean(chains) && chains.length > 0 && Boolean(client),
refetchOnMount: false,
refetchOnReconnect: true,
refetchOnWindowFocus: false,
},
);
enabled: Boolean(address) && Boolean(chains) && chains.length > 0 && Boolean(client),
refetchOnMount: false,
refetchOnReconnect: true,
refetchOnWindowFocus: false,
});
};
32 changes: 17 additions & 15 deletions packages/graz/src/hooks/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export const useChainInfos = ({ chainId }: { chainId?: string[] }) => {
export const useActiveChainCurrency = ({ denom }: { denom: string }): UseQueryResult<AppCurrency | undefined> => {
const chains = useActiveChains();
const queryKey = ["USE_ACTIVE_CHAIN_CURRENCY", denom] as const;
const query = useQuery(
const query = useQuery({
queryKey,
({ queryKey: [, _denom] }) =>
queryFn: ({ queryKey: [, _denom] }) =>
chains?.find((c) => c.currencies.find((x) => x.coinMinimalDenom === _denom))?.currencies.find((x) => x),
);
});
return query;
};

Expand All @@ -118,17 +118,15 @@ export const useQueryClientValidators = <T extends QueryClient & StakingExtensio
}): UseQueryResult<QueryValidatorsResponse> => {
const status = args.status ?? "BOND_STATUS_BONDED";
const queryKey = ["USE_ACTIVE_CHAIN_VALIDATORS", args.queryClient, status] as const;
const query = useQuery(
const query = useQuery({
queryKey,
async ({ queryKey: [, _queryClient, _status] }) => {
queryFn: async ({ queryKey: [, _queryClient, _status] }) => {
if (!_queryClient) throw new Error("Query client is not defined");
const res = await _queryClient.staking.validators(_status);
return res;
},
{
enabled: typeof args.queryClient !== "undefined",
},
);
enabled: typeof args.queryClient !== "undefined",
});
return query;
};

Expand Down Expand Up @@ -196,16 +194,18 @@ export type UseSuggestChainArgs = MutationEventArgs<ChainInfo>;
* ```
*/
export const useSuggestChain = ({ onError, onLoading, onSuccess }: UseSuggestChainArgs = {}) => {
const queryKey = ["USE_SUGGEST_CHAIN", onError, onLoading, onSuccess];
const mutation = useMutation(queryKey, suggestChain, {
const mutationKey = ["USE_SUGGEST_CHAIN", onError, onLoading, onSuccess];
const mutation = useMutation({
mutationKey,
mutationFn: suggestChain,
onError: (err, args) => Promise.resolve(onError?.(err, args.chainInfo)),
onMutate: (data) => onLoading?.(data.chainInfo),
onSuccess: (chainInfo) => Promise.resolve(onSuccess?.(chainInfo)),
});

return {
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
suggest: mutation.mutate,
suggestAsync: mutation.mutateAsync,
Expand Down Expand Up @@ -246,16 +246,18 @@ export type UseSuggestChainAndConnectArgs = MutationEventArgs<SuggestChainAndCon
* ```
*/
export const useSuggestChainAndConnect = ({ onError, onLoading, onSuccess }: UseSuggestChainAndConnectArgs = {}) => {
const queryKey = ["USE_SUGGEST_CHAIN_AND_CONNECT", onError, onLoading, onSuccess];
const mutation = useMutation(queryKey, suggestChainAndConnect, {
const mutationKey = ["USE_SUGGEST_CHAIN_AND_CONNECT", onError, onLoading, onSuccess];
const mutation = useMutation({
mutationKey,
mutationFn: suggestChainAndConnect,
onError: (err, args) => Promise.resolve(onError?.(err, args)),
onMutate: (args) => onLoading?.(args),
onSuccess: (res) => Promise.resolve(onSuccess?.(res)),
});
const { data: isSupported } = useCheckWallet();
return {
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
isSupported: Boolean(isSupported),
status: mutation.status,
Expand Down
90 changes: 39 additions & 51 deletions packages/graz/src/hooks/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ export const useSendTokens = ({
const { data: account } = useAccount();
const accountAddress = account?.bech32Address;

const mutation = useMutation(
["USE_SEND_TOKENS", onError, onLoading, onSuccess, accountAddress],
(args: SendTokensArgs) => sendTokens({ senderAddress: accountAddress, ...args }),
{
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (txResponse) => Promise.resolve(onSuccess?.(txResponse)),
},
);
const mutation = useMutation({
mutationKey: ["USE_SEND_TOKENS", onError, onLoading, onSuccess, accountAddress],
mutationFn: (args: SendTokensArgs) => sendTokens({ senderAddress: accountAddress, ...args }),
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (txResponse) => Promise.resolve(onSuccess?.(txResponse)),
});

return {
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
sendTokens: mutation.mutate,
sendTokensAsync: mutation.mutateAsync,
Expand Down Expand Up @@ -99,19 +97,17 @@ export const useSendIbcTokens = ({
const { data: account } = useAccount();
const accountAddress = account?.bech32Address;

const mutation = useMutation(
["USE_SEND_IBC_TOKENS", onError, onLoading, onSuccess, accountAddress],
(args: SendIbcTokensArgs) => sendIbcTokens({ senderAddress: accountAddress, ...args }),
{
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (txResponse) => Promise.resolve(onSuccess?.(txResponse)),
},
);
const mutation = useMutation({
mutationKey: ["USE_SEND_IBC_TOKENS", onError, onLoading, onSuccess, accountAddress],
mutationFn: (args: SendIbcTokensArgs) => sendIbcTokens({ senderAddress: accountAddress, ...args }),
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (txResponse) => Promise.resolve(onSuccess?.(txResponse)),
});

return {
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
sendIbcTokens: mutation.mutate,
sendIbcTokensAsync: mutation.mutateAsync,
Expand Down Expand Up @@ -165,19 +161,17 @@ export const useInstantiateContract = <Message extends Record<string, unknown>>(
return instantiateContract(contractArgs);
};

const mutation = useMutation(
["USE_INSTANTIATE_CONTRACT", onError, onLoading, onSuccess, codeId, accountAddress],
const mutation = useMutation({
mutationKey: ["USE_INSTANTIATE_CONTRACT", onError, onLoading, onSuccess, codeId, accountAddress],
mutationFn,
{
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (instantiateResult) => Promise.resolve(onSuccess?.(instantiateResult)),
},
);
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (instantiateResult) => Promise.resolve(onSuccess?.(instantiateResult)),
});

return {
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
instantiateContract: mutation.mutate,
instantiateContractAsync: mutation.mutateAsync,
Expand Down Expand Up @@ -242,19 +236,17 @@ export const useExecuteContract = <Message extends Record<string, unknown>>({
return executeContract(executeArgs);
};

const mutation = useMutation(
["USE_EXECUTE_CONTRACT", onError, onLoading, onSuccess, contractAddress, accountAddress],
const mutation = useMutation({
mutationKey: ["USE_EXECUTE_CONTRACT", onError, onLoading, onSuccess, contractAddress, accountAddress],
mutationFn,
{
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (executeResult) => Promise.resolve(onSuccess?.(executeResult)),
},
);
onError: (err, data) => Promise.resolve(onError?.(err, data)),
onMutate: onLoading,
onSuccess: (executeResult) => Promise.resolve(onSuccess?.(executeResult)),
});

return {
error: mutation.error,
isLoading: mutation.isLoading,
isLoading: mutation.isPending,
isSuccess: mutation.isSuccess,
executeContract: mutation.mutate,
executeContractAsync: mutation.mutateAsync,
Expand All @@ -275,16 +267,14 @@ export const useQuerySmart = <TData, TError>(args?: {
queryMsg?: Record<string, unknown>;
}): UseQueryResult<TData, TError> => {
const { data: client } = useCosmWasmClient();
const query: UseQueryResult<TData, TError> = useQuery(
["USE_QUERY_SMART", args?.address, args?.queryMsg, client],
({ queryKey: [, _address] }) => {
const query: UseQueryResult<TData, TError> = useQuery({
queryKey: ["USE_QUERY_SMART", args?.address, args?.queryMsg, client],
queryFn: ({ queryKey: [, _address] }) => {
if (!args?.address || !args.queryMsg) throw new Error("address or queryMsg undefined");
return getQuerySmart(args.address, args.queryMsg, client);
},
{
enabled: Boolean(args?.address) && Boolean(args?.queryMsg) && Boolean(client),
},
);
enabled: Boolean(args?.address) && Boolean(args?.queryMsg) && Boolean(client),
});

return query;
};
Expand All @@ -302,16 +292,14 @@ export const useQueryRaw = <TError>(args?: {
}): UseQueryResult<Uint8Array | null, TError> => {
const { data: client } = useCosmWasmClient();
const queryKey = ["USE_QUERY_RAW", args?.key, args?.address, client] as const;
const query: UseQueryResult<Uint8Array | null, TError> = useQuery(
const query: UseQueryResult<Uint8Array | null, TError> = useQuery({
queryKey,
({ queryKey: [, _address] }) => {
queryFn: ({ queryKey: [, _address] }) => {
if (!args?.address || !args.key) throw new Error("address or key undefined");
return getQueryRaw(args.address, args.key, client);
},
{
enabled: Boolean(args?.address) && Boolean(args?.key) && Boolean(client),
},
);
enabled: Boolean(args?.address) && Boolean(args?.key) && Boolean(client),
});

return query;
};
5 changes: 4 additions & 1 deletion packages/graz/src/hooks/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export const useCheckWallet = (type?: WalletType): UseQueryResult<boolean> => {
const walletType = useGrazInternalStore((x) => type || x.walletType);

const queryKey = ["USE_CHECK_WALLET", walletType] as const;
const query = useQuery(queryKey, ({ queryKey: [, _type] }) => checkWallet(_type));
const query = useQuery({
queryKey,
queryFn: ({ queryKey: [, _type] }) => checkWallet(_type),
});

return query;
};
Loading

0 comments on commit d31c02c

Please sign in to comment.