Skip to content

Commit

Permalink
refactor: pass search params down from the explorer page
Browse files Browse the repository at this point in the history
  • Loading branch information
BrickheadJohnny committed Jul 12, 2024
1 parent 7c56133 commit d08774c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/app/explorer/_components/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import { YourGuilds } from "app/explorer/_components/YourGuilds"
import useIsStuck from "hooks/useIsStuck"
import { useSetAtom } from "jotai"
import { Suspense } from "react"
import { SearchParams } from "types"
import Robot from "/public/landing/robot.svg"
import { isSearchStuckAtom } from "../atoms"
import { ActiveSection } from "../types"
import { GuildInfiniteScroll } from "./GuildInfiniteScroll"
import { StickyBar } from "./StickyBar"

export const Explorer = () => {
export const Explorer = ({ searchParams }: { searchParams: SearchParams }) => {
const { keyPair } = useUserPublic()
const setIsSearchStuck = useSetAtom(isSearchStuckAtom)
const setIsWalletSelectorModalOpen = useSetAtom(walletSelectorModalAtom)
Expand Down Expand Up @@ -52,9 +53,8 @@ export const Explorer = () => {
<GuildSearchBar />
</Suspense>
</div>
<Suspense>
<GuildInfiniteScroll />
</Suspense>

<GuildInfiniteScroll searchParams={searchParams} />
</section>
</>
)
Expand Down
22 changes: 9 additions & 13 deletions src/app/explorer/_components/GuildInfiniteScroll.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
"use client"

import { GuildCardSkeleton, GuildCardWithLink } from "@/components/GuildCard"
import { Spinner } from "@phosphor-icons/react"
import useUser from "components/[guild]/hooks/useUser"
import { env } from "env"
import { useFetcherWithSign } from "hooks/useFetcherWithSign"
import { useScrollBatchedRendering } from "hooks/useScrollBatchedRendering"
import { useSearchParams } from "next/navigation"
import { memo, useRef } from "react"
import { SWRConfiguration } from "swr"
import useSWRInfinite from "swr/infinite"
import { GuildBase } from "types"
import {
GuildCardSkeleton,
GuildCardWithLink,
} from "../../../v2/components/GuildCard"
import { GuildBase, SearchParams } from "types"

const BATCH_SIZE = 24

Expand All @@ -26,8 +22,7 @@ const GuildCards = ({ guildData }: { guildData?: GuildBase[] }) => {
return Array.from({ length: BATCH_SIZE }, (_, i) => <GuildCardSkeleton key={i} />)
}

const useExploreGuilds = () => {
const searchParams = useSearchParams()
const useExploreGuilds = (searchParams?: SearchParams) => {
const { isSuperAdmin } = useUser()
const fetcherWithSign = useFetcherWithSign()
const options: SWRConfiguration = {
Expand All @@ -43,7 +38,7 @@ const useExploreGuilds = () => {
const url = new URL("/v2/guilds", env.NEXT_PUBLIC_API)
const params: Record<string, string> = {
order: "FEATURED",
...Object.fromEntries(searchParams?.entries() ?? []),
...searchParams,
offset: (BATCH_SIZE * pageIndex).toString(),
limit: BATCH_SIZE.toString(),
}
Expand All @@ -60,16 +55,17 @@ const useExploreGuilds = () => {
)
}

export const GuildInfiniteScroll = () => {
const searchParams = useSearchParams()
const search = searchParams?.get("search")
export const GuildInfiniteScroll = ({
searchParams,
}: { searchParams: SearchParams }) => {
const search = searchParams.search
const ref = useRef<HTMLElement>(null)
const {
data: filteredGuilds,
setSize,
isValidating,
isLoading,
} = useExploreGuilds()
} = useExploreGuilds(searchParams)
const renderedGuilds = filteredGuilds?.flat()

useScrollBatchedRendering({
Expand Down
10 changes: 8 additions & 2 deletions src/app/explorer/_components/GuildSearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client"

import { usePrevious } from "@/hooks/usePrevious"
import { MagnifyingGlass, PushPin, Sparkle } from "@phosphor-icons/react"
import { ActiveSection } from "app/explorer/types"
import useDebouncedState from "hooks/useDebouncedState"
Expand All @@ -23,7 +24,7 @@ export const GuildSearchBar = () => {
(searchParams?.get("order")?.toString() as Order) || Order.Featured
)
const [search, setSearch] = useState(searchParams?.get("search")?.toString() || "")
const debouncedSearch = useDebouncedState(search, 150)
const debouncedSearch = useDebouncedState(search, 200)

useEffect(() => {
const newSearchParams = new URLSearchParams(
Expand All @@ -32,10 +33,15 @@ export const GuildSearchBar = () => {
)
)

/**
* ?order=FEATURED is the default order, so added this early return to avoid navigating from /explorer to /explorer?order=FEATURED after the initial page load
*/
if (!searchParams?.get("order") && !search) return

router.push(`${pathname}?${newSearchParams.toString()}`, {
scroll: false,
})
}, [debouncedSearch, order])
}, [search, debouncedSearch, order])

return (
<div className="relative flex flex-col gap-3 sm:flex-row sm:gap-0">
Expand Down
5 changes: 3 additions & 2 deletions src/app/explorer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Layout } from "@/components/Layout"
import { Anchor } from "@/components/ui/Anchor"
import { env } from "env"
import { unstable_serialize as infinite_unstable_serialize } from "swr/infinite"
import { SearchParams } from "types"
import { Explorer } from "./_components/Explorer"
import { ExplorerSWRProvider } from "./_components/ExplorerSWRProvider"
import { HeaderBackground } from "./_components/HeaderBackground"
import { ActiveSection } from "./types"

const Page = async () => {
const Page = async ({ searchParams }: { searchParams: SearchParams }) => {
const featuredPath = `/v2/guilds?order=FEATURED&offset=0&limit=24`
const newestPath = `/v2/guilds?order=NEWEST&offset=0&limit=24`
const [ssrFeaturedGuilds, ssrNewestGuilds] = await Promise.all([
Expand Down Expand Up @@ -50,7 +51,7 @@ const Page = async () => {
</Layout.Hero>

<Layout.Main>
<Explorer />
<Explorer searchParams={searchParams} />
</Layout.Main>

<Layout.Footer>
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ type DetailedPinLeaderboardUserData = {
pins: LeaderboardPinData[]
}

type SearchParams = { [key: string]: string | string[] | undefined }

export { supportedEventSources, supportedSocialLinks, ValidationMethod }
export type {
BaseUser,
Expand Down Expand Up @@ -803,4 +805,5 @@ export type {
User,
UserAddress,
WalletError,
SearchParams,
}

0 comments on commit d08774c

Please sign in to comment.