diff --git a/src/pages/Script/ScriptsComp.tsx b/src/pages/Script/ScriptsComp.tsx index 6fd8709094..15b340ba7a 100644 --- a/src/pages/Script/ScriptsComp.tsx +++ b/src/pages/Script/ScriptsComp.tsx @@ -26,29 +26,37 @@ import { useSetToast } from '../../components/Toast' export const ScriptTransactions = ({ page, size }: { page: number; size: number }) => { const history = useHistory() const { codeHash, hashType } = useParams<{ codeHash: string; hashType: string }>() + const [transactionsEmpty, setTransactionsEmpty] = useState(false) - const transactionsQuery = useQuery(['scripts_ckb_transactions', codeHash, hashType, page, size], async () => { - const { data, meta } = await v2AxiosIns - .get(`scripts/ckb_transactions`, { - params: { - code_hash: codeHash, - hash_type: hashType, - page, - page_size: size, - }, - }) - .then((res: AxiosResponse) => - toCamelcase>(res.data), - ) + const transactionsQuery = useQuery( + ['scripts_ckb_transactions', codeHash, hashType, page, size], + async () => { + const { data, meta } = await v2AxiosIns + .get(`scripts/ckb_transactions`, { + params: { + code_hash: codeHash, + hash_type: hashType, + page, + page_size: size, + }, + }) + .then((res: AxiosResponse) => + toCamelcase>(res.data), + ) - if (data == null || data.ckbTransactions == null || data.ckbTransactions.length === 0) { - throw new Error('Transactions empty') - } - return { - total: meta?.total ?? 0, - ckbTransactions: data.ckbTransactions, - } - }) + if (data == null || data.ckbTransactions == null || data.ckbTransactions.length === 0) { + setTransactionsEmpty(true) + throw new Error('Transactions empty') + } + return { + total: meta?.total ?? 0, + ckbTransactions: data.ckbTransactions, + } + }, + { + refetchOnWindowFocus: () => !transactionsEmpty, + }, + ) const total = transactionsQuery.data?.total ?? 0 const totalPages = Math.ceil(total / size) @@ -122,33 +130,46 @@ export const ScriptCells = ({ }) => { const history = useHistory() const { codeHash, hashType } = useParams<{ codeHash: string; hashType: string }>() - - const cellsQuery = useQuery([`scripts_${cellType}`, codeHash, hashType, page, size], async () => { - const { data, meta } = await v2AxiosIns - .get(`scripts/${cellType}`, { - params: { - code_hash: codeHash, - hash_type: hashType, - page, - page_size: size, - }, - }) - .then((res: AxiosResponse) => - toCamelcase>(res.data), - ) - const camelCellType = camelcase(cellType) as 'deployedCells' | 'referringCells' - if (data == null) { - throw new Error('Fetch Cells null') - } - const cells = data[camelCellType]! - if (cells == null || cells.length === 0) { - throw new Error('Cells empty') - } - return { - total: meta?.total ?? 0, - cells, - } + const [cellsEmpty, setCellsEmpty] = useState>({ + deployedCells: false, + referringCells: false, }) + + const camelCellType = camelcase(cellType) as 'deployedCells' | 'referringCells' + + const cellsQuery = useQuery( + [`scripts_${cellType}`, codeHash, hashType, page, size], + async () => { + const { data, meta } = await v2AxiosIns + .get(`scripts/${cellType}`, { + params: { + code_hash: codeHash, + hash_type: hashType, + page, + page_size: size, + }, + }) + .then((res: AxiosResponse) => + toCamelcase>(res.data), + ) + if (data == null) { + setCellsEmpty({ ...cellsEmpty, [camelCellType]: true }) + throw new Error('Fetch Cells null') + } + const cells = data[camelCellType]! + if (cells == null || cells.length === 0) { + setCellsEmpty(prev => ({ ...prev, [camelCellType]: true })) + throw new Error('Cells empty') + } + return { + total: meta?.total ?? 0, + cells, + } + }, + { + refetchOnWindowFocus: () => !cellsEmpty[camelCellType], + }, + ) const total = cellsQuery.data?.total ?? 0 const totalPages = Math.ceil(total / size)