Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refetch transactions and metadatas at an interval after the last refetch #245

Merged
merged 8 commits into from
Feb 6, 2025
34 changes: 31 additions & 3 deletions components/bank/components/historyBox.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { TransactionAmount, TxMessage } from '../types';
import { shiftDigits, formatLargeNumber, formatDenom } from '@/utils';
import { getHandler } from '@/components/bank/handlers/handlerRegistry';
import { MetadataSDKType } from '@liftedinit/manifestjs/dist/codegen/cosmos/bank/v1beta1/bank';
import { useTokenFactoryDenomsMetadata } from '@/hooks';
import TxInfoModal from '../modals/txInfo';

/// Interval to refresh the history box transaction and metadata.
const HISTORY_BOX_REFRESH_INTERVAL = 3000;

export interface TransactionGroup {
tx_hash: string;
block_number: number;
Expand Down Expand Up @@ -36,16 +39,41 @@
totalPages: number;
txLoading: boolean;
isError: boolean;
refetch: () => void;
refetch: () => Promise<void> | void;
hansl marked this conversation as resolved.
Show resolved Hide resolved
skeletonGroupCount: number;
skeletonTxCount: number;
isGroup?: boolean;
}>) {
const [selectedTx, setSelectedTx] = useState<TxMessage | null>(null);
const { metadatas, isMetadatasLoading } = useTokenFactoryDenomsMetadata();
const { metadatas, isMetadatasLoading, refetchMetadatas } = useTokenFactoryDenomsMetadata();

const isLoading = initialLoading || txLoading || isMetadatasLoading;

useEffect(() => {
// Refetch txs and metadata after the last refetch completed. Queries can take longer
// than the refresh interval, so we want to make sure we don't have multiple queries
// running at the same time.
let latestRefetch = Promise.resolve();
let done = false;
let timer = setTimeout(refetchInner, HISTORY_BOX_REFRESH_INTERVAL);

function refetchInner() {
if (done) return;

latestRefetch = latestRefetch
.then(() => refetch())
.then(() => refetchMetadatas())
.then(() => {
timer = setTimeout(refetchInner, HISTORY_BOX_REFRESH_INTERVAL);

Check warning on line 67 in components/bank/components/historyBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/bank/components/historyBox.tsx#L60-L67

Added lines #L60 - L67 were not covered by tests
});
}

return () => {
done = true;
clearTimeout(timer);
};
}, [metadatas, refetchMetadatas]);

hansl marked this conversation as resolved.
Show resolved Hide resolved
function formatDateShort(dateString: string): string {
const date = new Date(dateString);
return date.toLocaleString('en-US', {
Expand Down