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

fix: search on comparator #753

Merged
merged 1 commit into from
Dec 2, 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
6 changes: 4 additions & 2 deletions src/components/comparateur/overscreens/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTranslations } from 'next-intl'
import React, { ForwardedRef, forwardRef, useEffect, useState } from 'react'
import { ForwardedRef, forwardRef, useEffect, useState } from 'react'
import useParamContext from 'src/providers/ParamProvider'
import { ComputedEquivalent } from 'types/equivalent'
import { getName } from 'utils/Equivalent/equivalent'
Expand Down Expand Up @@ -34,7 +34,9 @@ const Checkbox = (
}

useEffect(() => {
setInteracted(false)
if (interacted) {
setInteracted(false)
}
}, [equivalents])

return (
Expand Down
10 changes: 7 additions & 3 deletions src/components/comparateur/overscreens/EquivalentsOverscreen.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use client'

import { useTranslations } from 'next-intl'
import React, { useEffect, useRef, useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import useParamContext from 'src/providers/ParamProvider'
import { useSearchEquivalent } from 'src/providers/useSearchEquivalent'
import { categories } from 'data/categories'
import { track } from 'utils/matomo'
import useDebounce from 'hooks/useDebounce'
import Button from 'components/base/buttons/Button'
import SearchIcon from 'components/base/icons/search'
import HiddenLabel from 'components/form/HiddenLabel'
Expand All @@ -29,10 +30,13 @@ const EquivalentsOverscreen = () => {
}, [equivalents])

const [search, setSearch] = useState('')
const results = useSearchEquivalent(search, true)

const debouncedSearch: string = useDebounce(search)
const results = useSearchEquivalent(debouncedSearch, true, undefined, true)

const t = useTranslations('comparateur.overscreen')
const tModal = useTranslations('modal')

return (
<>
<div className={styles.header}>
Expand Down Expand Up @@ -63,7 +67,7 @@ const EquivalentsOverscreen = () => {
{tModal('close')}
</Button>
</div>
{search ? (
{debouncedSearch ? (
results.length > 0 ? (
<ul className={styles.content}>
<Equivalents
Expand Down
33 changes: 22 additions & 11 deletions src/providers/useSearchEquivalent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ const equivalents = computedEquivalents
})
.flatMap(getEquivalentWithCarpool)

export const useSearchEquivalent = (search: string, excludeEmpty?: boolean, category?: number) => {
export const useSearchEquivalent = (
search: string,
excludeEmpty?: boolean,
category?: number,
emptyResults?: boolean
) => {
const [results, setResults] = useState<ComputedEquivalent[]>([])
const [fuses, setFuses] = useState<{ fuse: Fuse<ComputedEquivalent>; nonEmptyFuse: Fuse<ComputedEquivalent> }>()

Expand Down Expand Up @@ -72,17 +77,23 @@ export const useSearchEquivalent = (search: string, excludeEmpty?: boolean, cate

useEffect(() => {
if (fuses) {
setResults(
excludeEmpty
? search.length > 0
? fuses.nonEmptyFuse.search(search.normalize('NFD').replace(/[\u0300-\u036f]/g, '')).map(({ item }) => item)
: computedEquivalents.filter((equivalent) => equivalent.value).sort((a, b) => (a.slug > b.slug ? 1 : -1))
: search.length > 0
? fuses.fuse.search(search.normalize('NFD').replace(/[\u0300-\u036f]/g, '')).map(({ item }) => item)
: computedEquivalents.sort((a, b) => (a.slug > b.slug ? 1 : -1))
)
if (emptyResults && search.length === 0) {
setResults([])
} else {
setResults(
excludeEmpty
? search.length > 0
? fuses.nonEmptyFuse
.search(search.normalize('NFD').replace(/[\u0300-\u036f]/g, ''))
.map(({ item }) => item)
: computedEquivalents.filter((equivalent) => equivalent.value).sort((a, b) => (a.slug > b.slug ? 1 : -1))
: search.length > 0
? fuses.fuse.search(search.normalize('NFD').replace(/[\u0300-\u036f]/g, '')).map(({ item }) => item)
: computedEquivalents.sort((a, b) => (a.slug > b.slug ? 1 : -1))
)
}
}
}, [search, fuses])
}, [search, fuses, emptyResults])

return results
}
Loading