Skip to content

Commit

Permalink
Add functionality that forces search machine to reload
Browse files Browse the repository at this point in the history
on route change.
  • Loading branch information
spaceo committed Feb 6, 2025
1 parent cd7f0a9 commit 369f603
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
4 changes: 0 additions & 4 deletions components/pages/searchPageLayout/SearchPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ const SearchPageLayout = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loadMoreRefIsInView])

useEffect(() => {
actor.send({ type: "RESET_BOOTSTRAP_STATE" })
}, [actor])

const isNoSearchResult = !isLoadingResults && (!data.search || !data.search.pages[0].length)
const hitCountText = data.search?.hitcount ? `(${data.search.hitcount})` : ""
const searchQueryText = searchQuery ? `"${searchQuery}"` : ""
Expand Down
1 change: 0 additions & 1 deletion components/shared/searchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const SearchInput = ({ className, placeholder }: SearchInputProps) => {
queryParams: { q: query },
})
router.push(url)
actor.send({ type: "SEARCH" })
}

return (
Expand Down
1 change: 1 addition & 0 deletions lib/machines/search/search.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default searchMachineSetup.createMachine({
target: "loadingMoreSearchResults",
},
RESET_BOOTSTRAP_STATE: {
actions: ["resetSearchData", "resetFilters", "setSubmittedQueryInContext", "resetOffset"],
target: "bootstrap",
},
},
Expand Down
44 changes: 35 additions & 9 deletions lib/machines/search/useSearchMachineActor.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use client"

import { useQueryClient } from "@tanstack/react-query"
import { useSelector } from "@xstate/react"
import _ from "lodash"
import { ReadonlyURLSearchParams } from "next/navigation"
import { ReadonlyURLSearchParams, usePathname } from "next/navigation"
import { useSearchParams } from "next/navigation"
import { useEffect, useRef, useState } from "react"
import { AnyEventObject, createActor } from "xstate"
Expand Down Expand Up @@ -51,13 +52,24 @@ searchActor.on("filterToggled", (emittedEvent: AnyEventObject) => {
*/
const useSearchMachineActor = () => {
const searchParams = useSearchParams()
const pathname = usePathname()
const urlQ = searchParams.get("q")
const [previousPathname, setPreviousPathname] = useState<string | undefined>(undefined)
const searchParamString = searchParams.toString()
const queryClient = useQueryClient()
const [isBootstrapped, setIsBootstrapped] = useState(false)
const actorRef = useRef(searchActor)
const actor = actorRef.current

const searchQuery = useSelector(actor, snapshot => {
return snapshot.context.submittedQuery
})
const storedQueryClient = useSelector(actor, snapshot => {
return snapshot.context.queryClient
})

// Handle bootstrapping of the machine.
useEffect(() => {
if (!actor.getSnapshot().matches("bootstrap") || isBootstrapped) {
if (!actor.getSnapshot().matches("bootstrap")) {
return
}

Expand All @@ -71,15 +83,29 @@ const useSearchMachineActor = () => {
actor.send({ type: "SET_SEARCH_STRING", q })
}

actor.send({ type: "SET_QUERY_CLIENT", queryClient })
// Only set query client if it is not already set.
if (!storedQueryClient) {
actor.send({ type: "SET_QUERY_CLIENT", queryClient })
}

actor.send({ type: "BOOTSTRAP_DONE" })
})

// Handle route changes.
// If the search query changes, reset the machine state.
// This is necessary because the machine state is not reset when the URL changes.
useEffect(() => {
if (!urlQ) {
return
}
const currentPathname = `${pathname}${searchParamString}`

if (previousPathname && currentPathname !== previousPathname) {
actor.send({ type: "RESET_BOOTSTRAP_STATE" })
}

setIsBootstrapped(true)
// We choose to ignore the eslint warning below
// because we want to make sure it only reruns if isBootstrapped changes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isBootstrapped])
setPreviousPathname(currentPathname)
}, [searchParamString, searchQuery, pathname])

return actor
}
Expand Down

0 comments on commit 369f603

Please sign in to comment.