Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/app/explorePools/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// src/app/explorePools/page.tsx
"use client";
import { useDebouncedValue } from "@/hooks/useDebouncedValue";
import React, { useEffect, useState, useCallback, useMemo } from "react";
import { useAccount, useWalletClient, useChainId } from "wagmi";
import { PredictionPoolFactoryABI } from "@/utils/abi/PredictionPoolFactory";
Expand Down Expand Up @@ -50,6 +51,7 @@ function ExploreFatePoolsClient() {
const [pools, setPools] = useState<Pool[]>([]);
const [loading, setLoading] = useState<boolean>(true);
const [searchQuery, setSearchQuery] = useState<string>("");
const debouncedSearchQuery = useDebouncedValue(searchQuery, 300);
const [isCreatingPool, setIsCreatingPool] = useState<boolean>(false);
const [chainStates, setChainStates] = useState<ChainLoadingState[]>([]);
const [refreshing, setRefreshing] = useState<boolean>(false);
Expand Down Expand Up @@ -561,7 +563,7 @@ function ExploreFatePoolsClient() {
const filteredPools = useMemo(
(): Pool[] =>
pools.filter((pool: Pool) => {
const searchLower = searchQuery.toLowerCase();
const searchLower = debouncedSearchQuery.toLowerCase();
const priceFeedName = getPriceFeedName(pool.priceFeedAddress, pool.chainId);
return (
pool.name.toLowerCase().includes(searchLower) ||
Expand All @@ -571,7 +573,7 @@ function ExploreFatePoolsClient() {
priceFeedName.toLowerCase().includes(searchLower)
);
}),
[pools, searchQuery]
[pools, debouncedSearchQuery]
);

const groupedPools = useMemo((): Record<number, Pool[]> => {
Expand Down
27 changes: 27 additions & 0 deletions src/hooks/useDebouncedValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState, useEffect } from 'react';
Comment thread
Atharva0506 marked this conversation as resolved.

/**
* Returns a debounced version of the provided value.
* The returned value only updates after the specified delay
* has elapsed since the last change, preventing excessive
* re-renders and computations (e.g. filtering large lists).
*
* @param value - The value to debounce.
* @param delay - Debounce delay in milliseconds (default: 300ms).
* @returns The debounced value.
*/
export function useDebouncedValue<T>(value: T, delay: number = 300): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);

useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(timer);
};
}, [value, delay]);

return debouncedValue;
}
Loading