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

improve fp search to allow search to not scope to showing tab #611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ const options = [
];

export const FinalityProviderFilter = () => {
const { filterValue, handleFilter } = useFinalityProviderState();
const { filterValue, handleFilter, searchValue } = useFinalityProviderState();

return (
<Select
options={options}
onSelect={handleFilter}
placeholder="Select Status"
defaultValue={filterValue}
value={searchValue ? "" : filterValue}
disabled={Boolean(searchValue)}
renderSelectedOption={(option) => `Showing ${option.label}`}
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Input } from "@babylonlabs-io/bbn-core-ui";
import Image from "next/image";
import { useCallback } from "react";
import { RiSearchLine } from "react-icons/ri";

import cancelCircle from "@/app/assets/cancel-circle.svg";
import { useFinalityProviderState } from "@/app/state/FinalityProviderState";

export const FinalityProviderSearch = () => {
Expand All @@ -14,8 +16,25 @@ export const FinalityProviderSearch = () => {
[handleSearch],
);

const searchSuffix = (
<span className="cursor-pointer">
const onClearSearch = useCallback(() => {
handleSearch("");
}, [handleSearch]);

const searchSuffix = searchValue ? (
<button
onClick={onClearSearch}
className="flex items-center hover:opacity-80 transition-opacity"
>
<Image
src={cancelCircle}
alt="Clear search"
width={18}
height={18}
className="opacity-50 hover:opacity-100 transition-opacity"
/>
</button>
) : (
<span className="text-primary-dark/50">
<RiSearchLine size={20} />
</span>
);
Expand Down
58 changes: 34 additions & 24 deletions src/app/state/FinalityProviderState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useSearchParams } from "next/navigation";
import {
Suspense,
useCallback,
useEffect,
useMemo,
useState,
type PropsWithChildren,
Expand Down Expand Up @@ -70,11 +69,15 @@ function FinalityProviderStateInner({ children }: PropsWithChildren) {
const searchParams = useSearchParams();
const fpParam = searchParams.get("fp");

const [searchValue, setSearchValue] = useState("");
const [searchValue, setSearchValue] = useState(fpParam || "");
const [filterValue, setFilterValue] = useState<string | number>(
fpParam ? "" : "active",
);
const [sortState, setSortState] = useState<SortState>({});
const [previousFilterValue, setPreviousFilterValue] = useState<
string | number
>("active");
const [isInitialLoad, setIsInitialLoad] = useState(true);

const debouncedSearch = useDebounce(searchValue, 300);

Expand All @@ -85,9 +88,26 @@ function FinalityProviderStateInner({ children }: PropsWithChildren) {
name: debouncedSearch,
});

const handleSearch = useCallback((searchTerm: string) => {
setSearchValue(searchTerm);
}, []);
const handleSearch = useCallback(
(searchTerm: string) => {
if (!searchValue && searchTerm) {
setPreviousFilterValue(filterValue);
}

setSearchValue(searchTerm);

if (!(isInitialLoad && fpParam && searchTerm === fpParam)) {
if (searchTerm) {
setFilterValue("");
} else {
setFilterValue(previousFilterValue);
}
} else {
setIsInitialLoad(false);
}
},
[searchValue, filterValue, previousFilterValue, isInitialLoad, fpParam],
);

const handleSort = useCallback((sortField: string) => {
setSortState(({ field, direction }) =>
Expand All @@ -105,6 +125,7 @@ function FinalityProviderStateInner({ children }: PropsWithChildren) {

const handleFilter = useCallback((value: string | number) => {
setFilterValue(value);
setPreviousFilterValue(value);
}, []);

const isRowSelectable = useCallback((row: FinalityProvider) => {
Expand All @@ -120,18 +141,6 @@ function FinalityProviderStateInner({ children }: PropsWithChildren) {
return data.finalityProviders.filter((fp: FinalityProvider) => {
if (!fp) return false;

const isActive = fp.state === FinalityProviderStateEnum.ACTIVE;
const isInactive = inactiveStatuses.has(fp.state);

const statusMatch =
filterValue === "active"
? isActive
: filterValue === "inactive"
? isInactive
: true;

if (!statusMatch) return false;

if (searchValue) {
const searchLower = searchValue.toLowerCase();
return (
Expand All @@ -141,7 +150,14 @@ function FinalityProviderStateInner({ children }: PropsWithChildren) {
);
}

return true;
const isActive = fp.state === FinalityProviderStateEnum.ACTIVE;
const isInactive = inactiveStatuses.has(fp.state);

return filterValue === "active"
? isActive
: filterValue === "inactive"
? isInactive
: true;
});
}, [data?.finalityProviders, filterValue, searchValue]);

Expand All @@ -151,12 +167,6 @@ function FinalityProviderStateInner({ children }: PropsWithChildren) {
[data?.finalityProviders],
);

useEffect(() => {
if (fpParam && data?.finalityProviders) {
handleSearch(fpParam);
}
}, [fpParam, data?.finalityProviders, handleSearch]);

const state = useMemo(
() => ({
searchValue,
Expand Down