Skip to content

Commit

Permalink
refactor: tx expression, query key, and change file name
Browse files Browse the repository at this point in the history
  • Loading branch information
poomthiti committed May 2, 2023
1 parent 7e266fe commit c02e4f6
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box } from "@chakra-ui/react";
import type { ChangeEvent } from "react";

import { ErrorFetching } from "../common";
import { ErrorFetching } from "../ErrorFetching";
import { useInternalNavigate } from "lib/app-provider";
import { Pagination } from "lib/components/pagination";
import { usePaginator } from "lib/components/pagination/usePaginator";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box } from "@chakra-ui/react";
import type { ChangeEvent } from "react";

import { ErrorFetching } from "../common";
import { ErrorFetching } from "../ErrorFetching";
import { useInternalNavigate } from "lib/app-provider";
import { Pagination } from "lib/components/pagination";
import { usePaginator } from "lib/components/pagination/usePaginator";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box } from "@chakra-ui/react";
import type { ChangeEvent } from "react";

import { ErrorFetching } from "../common";
import { ErrorFetching } from "../ErrorFetching";
import { Pagination } from "lib/components/pagination";
import { usePaginator } from "lib/components/pagination/usePaginator";
import { EmptyState } from "lib/components/state";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box } from "@chakra-ui/react";
import type { ChangeEvent } from "react";

import { ErrorFetching } from "../common";
import { ErrorFetching } from "../ErrorFetching";
import { useInternalNavigate } from "lib/app-provider";
import { Pagination } from "lib/components/pagination";
import { usePaginator } from "lib/components/pagination/usePaginator";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Alert, AlertDescription, Box, Flex } from "@chakra-ui/react";
import type { ChangeEvent } from "react";
import { useEffect, useState } from "react";

import { ErrorFetching } from "../common";
import { ErrorFetching } from "../ErrorFetching";
import { CustomIcon } from "lib/components/icon";
import { Pagination } from "lib/components/pagination";
import { usePaginator } from "lib/components/pagination/usePaginator";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/query-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const createQueryFnWithTimeout =
<T>(queryFn: () => Promise<T>, ms: number = 6 * 60 * 1000) =>
<T>(queryFn: () => Promise<T>, ms: number = 6 * 1000) =>
() =>
Promise.race([
queryFn(),
Expand Down
1 change: 1 addition & 0 deletions src/lib/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./code";
export * from "./contract";
export * from "./proposal";
export * from "./tx";
export * from "./account";
13 changes: 7 additions & 6 deletions src/lib/services/accountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useWallet } from "@cosmos-kit/react";
import type { UseQueryResult } from "@tanstack/react-query";
import { useQuery } from "@tanstack/react-query";

import { useCelatoneApp, useChainId } from "lib/app-provider";
import { getAccountIdByAddressQueryDocument } from "lib/query/account";
import { useCelatoneApp } from "lib/app-provider";
import { getAccountIdByAddressQueryDocument } from "lib/query";
import type { Addr, Balance, Option } from "lib/types";

import { getAccountBalanceInfo } from "./account";
Expand Down Expand Up @@ -34,17 +34,18 @@ export const useAccountBalances = (
);
};

export const useAccountId = (walletAddress: Option<Addr>) => {
export const useAccountId = (
walletAddress: Option<Addr>
): UseQueryResult<Option<number>> => {
const { indexerGraphClient } = useCelatoneApp();
const chainId = useChainId();
const queryFn = () => {
if (!walletAddress)
throw new Error("Error fetching account id: failed to retrieve address.");
return indexerGraphClient
.request(getAccountIdByAddressQueryDocument, { address: walletAddress })
.then(({ accounts_by_pk }) => accounts_by_pk?.id);
.then<Option<number>>(({ accounts_by_pk }) => accounts_by_pk?.id);
};
return useQuery(["account_id", chainId, walletAddress], queryFn, {
return useQuery(["account_id", indexerGraphClient, walletAddress], queryFn, {
enabled: Boolean(walletAddress),
retry: 1,
refetchOnWindowFocus: false,
Expand Down
23 changes: 16 additions & 7 deletions src/lib/services/expression/txExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ export const useTxExpression = ({
}) =>
useMemo(() => {
const hasFilter = Object.values(filters).some((filter: boolean) => filter);
return {
...(accountId
? { account_id: { _eq: accountId } }
: { ...(address ? { account: { address: { _eq: address } } } : {}) }),
...(isSigner === undefined ? {} : { is_signer: { _eq: isSigner } }),
...(hasFilter || search
const accountIdExp = accountId ? { account_id: { _eq: accountId } } : {};
const addressExp = address
? { account: { address: { _eq: address } } }
: {};
const applyAccountExp = Object.keys(accountIdExp).length
? accountIdExp
: addressExp;
const isSignerExp =
isSigner === undefined ? {} : { is_signer: { _eq: isSigner } };
const filterExp =
hasFilter || search
? {
transaction: {
...(hasFilter ? generateActionsFilter(filters) : {}),
Expand All @@ -63,6 +68,10 @@ export const useTxExpression = ({
: {}),
},
}
: {}),
: {};
return {
...applyAccountExp,
...isSignerExp,
...filterExp,
};
}, [address, accountId, filters, isSigner, search]);
1 change: 1 addition & 0 deletions src/lib/services/txService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const useTxsByAddressPagination = (
[
"transactions_by_address_pagination",
address,
accountId,
search,
filters,
isSigner,
Expand Down

0 comments on commit c02e4f6

Please sign in to comment.