diff --git a/package-lock.json b/package-lock.json index 21ac8bdd..47ee79ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "simple-staking", - "version": "0.3.21", + "version": "0.3.22", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "simple-staking", - "version": "0.3.21", + "version": "0.3.22", "dependencies": { "@babylonlabs-io/btc-staking-ts": "0.3.0", "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", diff --git a/package.json b/package.json index 30e50b8a..cc8613a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-staking", - "version": "0.3.21", + "version": "0.3.22", "private": true, "scripts": { "dev": "next dev", diff --git a/src/app/components/Staking/FinalityProviders/FinalityProviderSearch.tsx b/src/app/components/Staking/FinalityProviders/FinalityProviderSearch.tsx index 6889d7e2..f2f6a9d2 100644 --- a/src/app/components/Staking/FinalityProviders/FinalityProviderSearch.tsx +++ b/src/app/components/Staking/FinalityProviders/FinalityProviderSearch.tsx @@ -1,36 +1,19 @@ -import { useDebounce } from "@uidotdev/usehooks"; -import { useSearchParams } from "next/navigation"; -import React, { useEffect, useState } from "react"; +"use client"; + +import React from "react"; import { FiSearch } from "react-icons/fi"; interface FinalityProviderSearchProps { + searchValue: string; onSearch: (searchTerm: string) => void; } export const FinalityProviderSearch: React.FC = ({ + searchValue, onSearch, }) => { - // Get the finality provider from the search params - const searchParams = useSearchParams(); - const initialSearchFp = searchParams.get("fp"); - - const [searchTerm, setSearchTerm] = useState(initialSearchFp || ""); - const debouncedSearchTerm = useDebounce(searchTerm, 300); - - // Effect for handling initial search - useEffect(() => { - if (initialSearchFp) { - onSearch(initialSearchFp); - } - }, [initialSearchFp, onSearch]); - - // Effect for handling subsequent searches - useEffect(() => { - onSearch(debouncedSearchTerm); - }, [debouncedSearchTerm, onSearch]); - const handleSearch = (e: React.ChangeEvent) => { - setSearchTerm(e.target.value); + onSearch(e.target.value); }; return ( @@ -39,10 +22,11 @@ export const FinalityProviderSearch: React.FC = ({
+ diff --git a/src/app/components/Staking/FinalityProviders/FinalityProviders.tsx b/src/app/components/Staking/FinalityProviders/FinalityProviders.tsx index 7794ef13..a40efed7 100644 --- a/src/app/components/Staking/FinalityProviders/FinalityProviders.tsx +++ b/src/app/components/Staking/FinalityProviders/FinalityProviders.tsx @@ -92,9 +92,8 @@ export const FinalityProviders: React.FC = ({ handleError, ]); - const { handleSearch, filteredProviders } = useFinalityProvidersData( - fps?.finalityProviders, - ); + const { searchValue, handleSearch, filteredProviders } = + useFinalityProvidersData(fps?.finalityProviders); if (!fps?.finalityProviders?.length) { return ; @@ -106,7 +105,10 @@ export const FinalityProviders: React.FC = ({ Step-1: Select a finality provider

- +

Finality Provider

diff --git a/src/app/hooks/finalityProviders/useFinalityProvidersData.ts b/src/app/hooks/finalityProviders/useFinalityProvidersData.ts index 97d08c20..60c50647 100644 --- a/src/app/hooks/finalityProviders/useFinalityProvidersData.ts +++ b/src/app/hooks/finalityProviders/useFinalityProvidersData.ts @@ -1,33 +1,40 @@ -import { useCallback, useEffect, useState } from "react"; +"use client"; + +import { useDebounce } from "@uidotdev/usehooks"; +import { useSearchParams } from "next/navigation"; +import { useCallback, useMemo, useState } from "react"; import { FinalityProvider as FinalityProviderInterface } from "@/app/types/finalityProviders"; export const useFinalityProvidersData = ( initialProviders: FinalityProviderInterface[] | undefined, ) => { - const [filteredProviders, setFilteredProviders] = useState(initialProviders); + const searchParams = useSearchParams(); + const [searchTerm, setSearchTerm] = useState(searchParams.get("fp") || ""); + const debouncedSearchTerm = useDebounce(searchTerm, 300); - useEffect(() => { - setFilteredProviders(initialProviders); - }, [initialProviders]); + const filteredProviders = useMemo( + () => + initialProviders?.filter((fp) => { + const matchesMoniker = fp.description?.moniker + ?.toLowerCase() + .includes(debouncedSearchTerm.toLowerCase()); + const matchesPk = fp.btcPk + .toLowerCase() + .includes(debouncedSearchTerm.toLowerCase()); - const handleSearch = useCallback( - (searchTerm: string) => { - const filtered = initialProviders?.filter( - (fp) => - fp.description?.moniker - ?.toLowerCase() - .includes(searchTerm.toLowerCase()) || - fp.btcPk.toLowerCase().includes(searchTerm.toLowerCase()), - ); - setFilteredProviders(filtered); - }, - [initialProviders], + return matchesMoniker || matchesPk; + }), + [initialProviders, debouncedSearchTerm], ); + const handleSearch = useCallback((searchValue: string) => { + setSearchTerm(searchValue); + }, []); + return { filteredProviders, - setFilteredProviders, handleSearch, + searchValue: searchTerm, }; };