Skip to content

Commit

Permalink
feat(script): Not reload on focus while 404
Browse files Browse the repository at this point in the history
  • Loading branch information
Daryl-L committed Oct 8, 2023
1 parent e8aeca9 commit 0a1efec
Showing 1 changed file with 68 additions and 47 deletions.
115 changes: 68 additions & 47 deletions src/pages/Script/ScriptsComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 explorerService.api.requesterV2
.get(`scripts/ckb_transactions`, {
params: {
code_hash: codeHash,
hash_type: hashType,
page,
page_size: size,
},
})
.then((res: AxiosResponse) =>
toCamelcase<Response.Response<{ ckbTransactions: CkbTransactionInScript[] }>>(res.data),
)
const transactionsQuery = useQuery(
['scripts_ckb_transactions', codeHash, hashType, page, size],
async () => {
const { data, meta } = await explorerService.api.requesterV2
.get(`scripts/ckb_transactions`, {
params: {
code_hash: codeHash,
hash_type: hashType,
page,
page_size: size,
},
})
.then((res: AxiosResponse) =>
toCamelcase<Response.Response<{ ckbTransactions: CkbTransactionInScript[] }>>(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)

Expand Down Expand Up @@ -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 explorerService.api.requesterV2
.get(`scripts/${cellType}`, {
params: {
code_hash: codeHash,
hash_type: hashType,
page,
page_size: size,
},
})
.then((res: AxiosResponse) =>
toCamelcase<Response.Response<{ deployedCells?: CellInScript[]; referringCells?: CellInScript[] }>>(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<Record<'deployedCells' | 'referringCells', boolean>>({
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 explorerService.api.requesterV2
.get(`scripts/${cellType}`, {
params: {
code_hash: codeHash,
hash_type: hashType,
page,
page_size: size,
},
})
.then((res: AxiosResponse) =>
toCamelcase<Response.Response<{ deployedCells?: CellInScript[]; referringCells?: CellInScript[] }>>(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)

Expand Down

0 comments on commit 0a1efec

Please sign in to comment.