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

better handling of state in search #748

Merged
merged 1 commit into from
Jul 18, 2024
Merged
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
10 changes: 5 additions & 5 deletions app/components/search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect, useState} from 'react'
import {useEffect, useRef, useState} from 'react'
import {useSearchParams} from '@remix-run/react'
import debounce from 'lodash/debounce'
import {useSearch} from '~/hooks/useSearch'
Expand Down Expand Up @@ -33,15 +33,15 @@ export default function Search({limitFromUrl, className}: Props) {
}
}

// This ugly thing here is so that the useEffect doesn't need `searchFn` as its dependancy
const searchFnRef = useRef(searchFn)
searchFnRef.current = searchFn
useEffect(() => {
const query = searchParams.get('q')
if (loadedQuestions && query) {
searchFn(query)
searchFnRef.current(query)
setShowResults(true)
}
// Properly adding all dependancies would require transforming `searchFn` into a callback
// or something, which in turn would cause this `useEffect` to be called a lot more often.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loadedQuestions, searchParams])

const handleChange = debounce(searchFn, 100)
Expand Down
2 changes: 1 addition & 1 deletion app/routes/questions.toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const loadToC = async (request: any): Promise<LoaderResp> => {
return {
data: data
.filter(canBeShown)
.filter(({tags}) => tags.includes(INTRODUCTORY) || tags.includes(ADVANCED))
.filter(({tags}) => tags?.includes(INTRODUCTORY) || tags?.includes(ADVANCED))
.map(formatQuestion(1))
.sort((a, b) => (a.order || 0) - (b.order || 0)),
timestamp,
Expand Down
Loading