Skip to content

Commit

Permalink
feat(explorer): filter content using query params
Browse files Browse the repository at this point in the history
Content can now be filtered using either the params groupid or admin

re semaphore-protocol#25
  • Loading branch information
tb38r committed Dec 14, 2024
1 parent 72590e7 commit 875ea4a
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 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,15 @@ 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 [adminParam, ] = useState(new URLSearchParams(searchParams).get("admin"))
// console.log("group", groupIdParam)
// const [groupIdParam,] = useState(new URLSearchParams(searchParams).get("groupid"))
// console.log("group", groupIdParam)

useEffect(() => {
const fetchData = async () => {
setLoading(true)
Expand All @@ -38,7 +44,11 @@ export default function Network() {
const filterGroups = useCallback(
(groupIdOrAdmin: string) => {
let groups: GroupResponse[]
if (groupIdOrAdmin.startsWith("0x")) {
if (groupIdParam) {
groups = allGroups.filter((group) => (!groupIdOrAdmin ? true : group.id === groupIdOrAdmin))
} else if (adminParam) {
groups = allGroups.filter((group) => (!groupIdOrAdmin ? true : group.admin === groupIdOrAdmin))
} else if (groupIdOrAdmin.startsWith("0x")) {
groupIdOrAdmin = groupIdOrAdmin.toLowerCase()
groups = allGroups.filter((group) => (!groupIdOrAdmin ? true : group.admin?.includes(groupIdOrAdmin)))
} else {
Expand All @@ -50,6 +60,10 @@ export default function Network() {
[allGroups]
)

useEffect(() => {
filterGroups(adminParam || groupIdParam || "")
}, [adminParam, groupIdParam, filterGroups])

return loading ? (
<div className="flex justify-center items-center h-screen">
<div className="loader" />
Expand Down

0 comments on commit 875ea4a

Please sign in to comment.