diff --git a/packages/extension-polkagate/src/popup/history/index.tsx b/packages/extension-polkagate/src/popup/history/index.tsx index 55c06233f..1dd41b99d 100644 --- a/packages/extension-polkagate/src/popup/history/index.tsx +++ b/packages/extension-polkagate/src/popup/history/index.tsx @@ -44,7 +44,7 @@ export default function TransactionHistory (): React.ReactElement { setTabIndex ={setTabIndex} tabIndex={tabIndex} /> - + {grouped && Object.keys(grouped).length > 0 && Object.entries(grouped)?.map((group) => { const [date, info] = group; @@ -71,6 +71,7 @@ export default function TransactionHistory (): React.ReactElement { {(grouped === undefined || (transfersTx.isFetching && tabHistory?.length === 0)) && } +
{grouped && { @@ -85,7 +86,6 @@ export default function TransactionHistory (): React.ReactElement { ) } } -
); diff --git a/packages/extension-polkagate/src/popup/history/modal/HistoryModal.tsx b/packages/extension-polkagate/src/popup/history/modal/HistoryModal.tsx index 4b2eabacb..bd30c3aa7 100644 --- a/packages/extension-polkagate/src/popup/history/modal/HistoryModal.tsx +++ b/packages/extension-polkagate/src/popup/history/modal/HistoryModal.tsx @@ -51,7 +51,7 @@ export default function HistoryModal ({ address, setDisplayPopup }: Props): Reac setTabIndex ={setTabIndex} tabIndex={tabIndex} /> - + {grouped && Object.keys(grouped).length > 0 && Object.entries(grouped)?.map((group) => { const [date, info] = group; @@ -93,7 +93,7 @@ export default function HistoryModal ({ address, setDisplayPopup }: Props): Reac ) } } -
+
} diff --git a/packages/extension-polkagate/src/popup/history/useTransactionHistory.tsx b/packages/extension-polkagate/src/popup/history/useTransactionHistory.tsx index 65cfc89b3..2a37e1faa 100644 --- a/packages/extension-polkagate/src/popup/history/useTransactionHistory.tsx +++ b/packages/extension-polkagate/src/popup/history/useTransactionHistory.tsx @@ -180,30 +180,48 @@ export default function useTransactionHistory (address: string | undefined, tabI return; } - const observerCallback = async (): Promise => { + const observerCallback = (entries: IntersectionObserverEntry[]): void => { + const [entry] = entries; + + if (!entry.isIntersecting) { + return; // If the observer object is not in view, do nothing + } + if (receivingTransfers.current?.isFetching) { - return; + return; // If already fetching, do nothing } if (!receivingTransfers.current?.hasMore) { - return observerInstance?.current?.disconnect(); + observerInstance.current?.disconnect(); + console.log('No more data to load, disconnecting observer.'); + + return; } - await getTransfers(receivingTransfers.current); + getTransfers(receivingTransfers.current) // Fetch more transfers if available + .catch((error) => { + console.error('Error fetching transfers:', error); + }); }; const options = { root: document.getElementById('scrollArea'), rootMargin: '0px', - threshold: 1.0 + threshold: 1.0 // Trigger when 100% of the target (observerObj) is visible }; - // eslint-disable-next-line @typescript-eslint/no-misused-promises observerInstance.current = new IntersectionObserver(observerCallback, options); + const target = document.getElementById('observerObj'); - target && observerInstance.current.observe(target); - }, [chainName, formatted, getTransfers, tabHistory]); + if (target) { + observerInstance.current.observe(target); // Start observing the target + } + + return () => { + observerInstance.current?.disconnect(); + }; + }, [chainName, formatted, getTransfers]); return { grouped, tabHistory, transfersTx }; } diff --git a/packages/extension-polkagate/src/util/api/getTransfers.ts b/packages/extension-polkagate/src/util/api/getTransfers.ts index e2304ce26..e237e5272 100644 --- a/packages/extension-polkagate/src/util/api/getTransfers.ts +++ b/packages/extension-polkagate/src/util/api/getTransfers.ts @@ -1,24 +1,21 @@ // Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors // SPDX-License-Identifier: Apache-2.0 -// @ts-nocheck -/* eslint-disable sort-keys */ +import type { TransferRequest } from '../types'; import request from 'umi-request'; -import { TransferRequest } from '../types'; - const nullObject = { code: 0, - message: 'Success', - generated_at: Date.now(), data: { count: 0, transfers: null - } + }, + generated_at: Date.now(), + message: 'Success' } as unknown as TransferRequest; -export function getTxTransfers(chainName: string, address: string, pageNum: number, pageSize: number): Promise { +export function getTxTransfers (chainName: string, address: string, pageNum: number, pageSize: number): Promise { if (!chainName) { return Promise.resolve(nullObject); } @@ -46,6 +43,6 @@ export function getTxTransfers(chainName: string, address: string, pageNum: numb }); } -function postReq(api: string, data: Record = {}, option?: Record): Promise { +function postReq (api: string, data: Record = {}, option?: Record): Promise { return request.post(api, { data, ...option }); }