Skip to content

Commit

Permalink
Optimize useHistory and useTxs
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Aug 27, 2024
1 parent 18fc89a commit f0d534c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
12 changes: 1 addition & 11 deletions packages/xtoken-ui/src/hooks/use-history.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GQL_GET_HISTORY } from "../config";
import { HistoryReqParams, HistoryResData } from "../types";
import { useQuery } from "@apollo/client";
import { useEffect, useState } from "react";
import { useWallet } from "./use-wallet";

export function useHistory(page: number, enabled?: boolean) {
Expand All @@ -17,14 +16,5 @@ export function useHistory(page: number, enabled?: boolean) {
skip: !enabled,
});

const [data, setData] = useState(_data?.historyRecords.records ?? []);
const [total, setTotal] = useState(_data?.historyRecords.total ?? 0);
useEffect(() => {
if (!loading) {
setData(_data?.historyRecords.records ?? []);
setTotal(_data?.historyRecords.total ?? 0);
}
}, [loading, _data?.historyRecords]);

return { loading, data, total, refetch };
return { loading, data: _data?.historyRecords.records ?? [], total: _data?.historyRecords.total ?? 0, refetch };
}
19 changes: 7 additions & 12 deletions packages/xtoken-ui/src/hooks/use-txs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GQL_GET_TXS } from "../config";
import { TxsReqParams, TxsResData } from "../types";
import { useQuery } from "@apollo/client";
import { useEffect, useState } from "react";

export function useTxs(sender: string, page: number, row = 10) {
const {
Expand All @@ -15,15 +14,11 @@ export function useTxs(sender: string, page: number, row = 10) {
fetchPolicy: "cache-and-network",
});

const [data, setData] = useState(_data?.historyRecords.records ?? []);
const [total, setTotal] = useState(_data?.historyRecords.total ?? 0);

useEffect(() => {
if (!loading) {
setData(_data?.historyRecords.records ?? []);
setTotal(_data?.historyRecords.total ?? 0);
}
}, [loading, _data?.historyRecords.records, _data?.historyRecords.total]);

return { loading, data, total, networkStatus, refetch };
return {
loading,
data: _data?.historyRecords.records ?? [],
total: _data?.historyRecords.total ?? 0,
networkStatus,
refetch,
};
}

0 comments on commit f0d534c

Please sign in to comment.