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
Merged
35 changes: 32 additions & 3 deletions components/bank/components/historyBox.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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.
// This is used as a delay between successful queries.
const HISTORY_BOX_REFRESH_INTERVAL = 1000;
chalabi2 marked this conversation as resolved.
Show resolved Hide resolved

export interface TransactionGroup {
tx_hash: string;
block_number: number;
Expand Down Expand Up @@ -36,16 +40,41 @@
totalPages: number;
txLoading: boolean;
isError: boolean;
refetch: () => void;
refetch: () => Promise<any>;
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 68 in components/bank/components/historyBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/bank/components/historyBox.tsx#L61-L68

Added lines #L61 - L68 were not covered by tests
});
}

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

function formatDateShort(dateString: string): string {
const date = new Date(dateString);
return date.toLocaleString('en-US', {
Expand Down
2 changes: 1 addition & 1 deletion components/groups/components/groupControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type GroupControlsProps = {
denomLoading: boolean;
isDenomError: boolean;
refetchBalances: () => void;
refetchHistory: () => void;
refetchHistory: () => Promise<any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Use Promise<void> instead of Promise<any>.

Using any type bypasses TypeScript's type checking. Since refetchHistory is used to trigger a refresh without returning any meaningful value, it should return Promise<void>.

Apply this diff to update the type signature:

-refetchHistory: () => Promise<any>;
+refetchHistory: () => Promise<void>;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
refetchHistory: () => Promise<any>;
refetchHistory: () => Promise<void>;

refetchDenoms: () => void;
refetchGroupInfo: () => void;
pageSize: number;
Expand Down