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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions components/bank/components/historyBox.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +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';
import { useIntervalDebounceEffect } from '@/hooks/useDebounceEffect';

// Interval to refresh the history box transaction and metadata.
// This is used as a delay between successful queries.
const HISTORY_BOX_REFRESH_INTERVAL = 2000;

export interface TransactionGroup {
tx_hash: string;
Expand Down Expand Up @@ -36,16 +41,22 @@ export function HistoryBox({
totalPages: number;
txLoading: boolean;
isError: boolean;
refetch: () => void;
refetch: () => Promise<unknown>;
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;

useIntervalDebounceEffect(
() => Promise.all([refetch(), refetchMetadatas()]),
HISTORY_BOX_REFRESH_INTERVAL,
[refetch, 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<unknown>;
refetchDenoms: () => void;
refetchGroupInfo: () => void;
pageSize: number;
Expand Down
41 changes: 41 additions & 0 deletions hooks/useDebounceEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { DependencyList, useEffect } from 'react';

/**
* Create a debounce effect that will call the callback function after the delay has passed,
* on an interval.
* @param callback The function to call
* @param delay The delay in milliseconds
* @param dependencies The dependencies to watch for changes
*/
export const useIntervalDebounceEffect = (
callback: () => Promise<unknown>,
delay: number,
dependencies?: DependencyList
) => {
useEffect(() => {
let done = false;
let latestTimer: Timer | undefined;

async function inner() {
if (done) return;

try {
await callback();
} catch (error) {
console.error('Error during refetch:', error);
} finally {
if (!done) {
latestTimer = setTimeout(inner, delay);

Check warning on line 28 in hooks/useDebounceEffect.ts

View check run for this annotation

Codecov / codecov/patch

hooks/useDebounceEffect.ts#L19-L28

Added lines #L19 - L28 were not covered by tests
}
}
}

latestTimer = setTimeout(inner, delay);

return () => {
done = true;
clearTimeout(latestTimer);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencies);
};