diff --git a/apps/explorer/src/app/[network]/page.tsx b/apps/explorer/src/app/[network]/page.tsx index f1ca09b..b650d3f 100644 --- a/apps/explorer/src/app/[network]/page.tsx +++ b/apps/explorer/src/app/[network]/page.tsx @@ -2,8 +2,8 @@ import { GroupResponse, SemaphoreSubgraph } from "@semaphore-protocol/data" import { SupportedNetwork } from "@semaphore-protocol/utils" -import { usePathname } from "next/navigation" -import { useCallback, useEffect, useState } from "react" +import { usePathname, useSearchParams } from "next/navigation" +import { useCallback, useEffect, useState, useMemo } from "react" import SearchBar from "@/components/SearchBar" export default function Network() { @@ -12,9 +12,13 @@ export default function Network() { const [allGroups, setAllGroups] = useState([]) const [filteredGroups, setFilteredGroups] = useState([]) - const [loading, setLoading] = useState(false) + const searchParams = useSearchParams() + const adminParam = useMemo(() => new URLSearchParams(searchParams).get("admin"), [searchParams.toString()]) + const groupIdParam = useMemo(() => new URLSearchParams(searchParams).get("groupid"), [searchParams.toString()]) + const queryParam = adminParam || groupIdParam + useEffect(() => { const fetchData = async () => { setLoading(true) @@ -40,16 +44,21 @@ export default function Network() { let groups: GroupResponse[] if (groupIdOrAdmin.startsWith("0x")) { groupIdOrAdmin = groupIdOrAdmin.toLowerCase() - groups = allGroups.filter((group) => (!groupIdOrAdmin ? true : group.admin?.includes(groupIdOrAdmin))) + groups = allGroups.filter((group) => group.admin?.includes(groupIdOrAdmin)) } else { - groups = allGroups.filter((group) => (!groupIdOrAdmin ? true : group.id.includes(groupIdOrAdmin))) + groups = allGroups.filter( + (group) => group.id.includes(groupIdOrAdmin) || group.admin === groupIdOrAdmin + ) } - setFilteredGroups(groups) }, [allGroups] ) + useEffect(() => { + filterGroups(adminParam || groupIdParam || "") + }, [adminParam, groupIdParam, filterGroups]) + return loading ? (
@@ -57,7 +66,12 @@ export default function Network() { ) : ( allGroups && (
- +
    diff --git a/apps/explorer/src/components/SearchBar.tsx b/apps/explorer/src/components/SearchBar.tsx index bca2887..b72b0fa 100644 --- a/apps/explorer/src/components/SearchBar.tsx +++ b/apps/explorer/src/components/SearchBar.tsx @@ -1,19 +1,34 @@ "use client" import { FaSearch } from "react-icons/fa" +import { useState, useEffect, ChangeEvent } from "react" + +export default function SearchBar({ placeholder, onChange, className, queryParam }: any) { + const [searchQuery, setSearchQuery] = useState("") + useEffect(() => { + if (queryParam) { + setSearchQuery(queryParam) + } + }, [queryParam]) + + const handleInputChange = (event: ChangeEvent) => { + const { value } = event.target + setSearchQuery(value) + onChange(value) + } -export default function SearchBar({ placeholder, onChange, className }: any) { return (
    onChange(event.target.value)} + onChange={handleInputChange} name="search-bar" type="text" placeholder={placeholder} className="block w-full rounded-md border-0 py-1.5 pl-9 pr-20 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-none text-sm sm:leading-6" + value={searchQuery} />
    )