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

Filter groups/admins via query params #29

Open
wants to merge 2 commits 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
28 changes: 21 additions & 7 deletions apps/explorer/src/app/[network]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -12,9 +12,13 @@ export default function Network() {

const [allGroups, setAllGroups] = useState<GroupResponse[]>([])
const [filteredGroups, setFilteredGroups] = useState<GroupResponse[]>([])

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)
Expand All @@ -40,24 +44,34 @@ 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 ? (
<div className="flex justify-center items-center h-screen">
<div className="loader" />
</div>
) : (
allGroups && (
<div className="mx-auto max-w-7xl px-4 lg:px-8 pt-20">
<SearchBar className="mb-6" placeholder="Group ID, Admin" onChange={filterGroups} />
<SearchBar
className="mb-6"
placeholder="Group ID, Admin"
onChange={filterGroups}
queryParam={queryParam}
/>

<div className="flex justify-center flex-col pb-10 font-[family-name:var(--font-geist-sans)]">
<ul className="divide-y divide-gray-300 min-w-xl">
Expand Down
19 changes: 17 additions & 2 deletions apps/explorer/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLInputElement>) => {
const { value } = event.target
setSearchQuery(value)
onChange(value)
}

export default function SearchBar({ placeholder, onChange, className }: any) {
return (
<div className={`relative mt-2 rounded-md shadow-sm ${className}`}>
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<FaSearch className="text-gray-600" />
</div>
<input
onChange={(event) => 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}
/>
</div>
)
Expand Down
Loading