From 8710c83813dd726fcd1a233f127cf510ca491507 Mon Sep 17 00:00:00 2001 From: Michele Riva Date: Wed, 20 Mar 2024 08:19:21 -0700 Subject: [PATCH 1/2] reverts #6478 - removes debounce on search --- .../Common/Search/States/WithSearchBox.tsx | 377 +++++++++--------- 1 file changed, 188 insertions(+), 189 deletions(-) diff --git a/components/Common/Search/States/WithSearchBox.tsx b/components/Common/Search/States/WithSearchBox.tsx index 273391c18f880..9b90a507214c8 100644 --- a/components/Common/Search/States/WithSearchBox.tsx +++ b/components/Common/Search/States/WithSearchBox.tsx @@ -1,27 +1,26 @@ -'use client'; +"use client"; import { - MagnifyingGlassIcon, - ChevronLeftIcon, -} from '@heroicons/react/24/outline'; -import type { Results, Nullable } from '@orama/orama'; -import classNames from 'classnames'; -import { useState, useRef, useEffect } from 'react'; -import type { FC } from 'react'; - -import styles from '@/components/Common/Search/States/index.module.css'; -import { WithAllResults } from '@/components/Common/Search/States/WithAllResults'; -import { WithEmptyState } from '@/components/Common/Search/States/WithEmptyState'; -import { WithError } from '@/components/Common/Search/States/WithError'; -import { WithNoResults } from '@/components/Common/Search/States/WithNoResults'; -import { WithPoweredBy } from '@/components/Common/Search/States/WithPoweredBy'; -import { WithSearchResult } from '@/components/Common/Search/States/WithSearchResult'; -import { useClickOutside } from '@/hooks/react-client'; -import { useRouter } from '@/navigation.mjs'; -import { DEFAULT_ORAMA_QUERY_PARAMS } from '@/next.constants.mjs'; -import { search as oramaSearch, getInitialFacets } from '@/next.orama.mjs'; -import type { SearchDoc } from '@/types'; -import { debounce } from '@/util/debounce'; + MagnifyingGlassIcon, + ChevronLeftIcon, +} from "@heroicons/react/24/outline"; +import type { Results, Nullable } from "@orama/orama"; +import classNames from "classnames"; +import { useState, useRef, useEffect } from "react"; +import type { FC } from "react"; + +import styles from "@/components/Common/Search/States/index.module.css"; +import { WithAllResults } from "@/components/Common/Search/States/WithAllResults"; +import { WithEmptyState } from "@/components/Common/Search/States/WithEmptyState"; +import { WithError } from "@/components/Common/Search/States/WithError"; +import { WithNoResults } from "@/components/Common/Search/States/WithNoResults"; +import { WithPoweredBy } from "@/components/Common/Search/States/WithPoweredBy"; +import { WithSearchResult } from "@/components/Common/Search/States/WithSearchResult"; +import { useClickOutside } from "@/hooks/react-client"; +import { useRouter } from "@/navigation.mjs"; +import { DEFAULT_ORAMA_QUERY_PARAMS } from "@/next.constants.mjs"; +import { search as oramaSearch, getInitialFacets } from "@/next.orama.mjs"; +import type { SearchDoc } from "@/types"; type Facets = { [key: string]: number }; @@ -30,171 +29,171 @@ type SearchResults = Nullable>; type SearchBoxProps = { onClose: () => void }; export const WithSearchBox: FC = ({ onClose }) => { - const [searchTerm, setSearchTerm] = useState(''); - const [searchResults, setSearchResults] = useState(null); - const [selectedFacet, setSelectedFacet] = useState(0); - const [searchError, setSearchError] = useState>(null); - - const router = useRouter(); - const searchInputRef = useRef(null); - const searchBoxRef = useRef(null); - - const search = (term: string) => { - oramaSearch({ - term, - ...DEFAULT_ORAMA_QUERY_PARAMS, - mode: 'fulltext', - returning: [ - 'path', - 'pageSectionTitle', - 'pageTitle', - 'path', - 'siteSection', - ], - ...filterBySection(), - }) - .then(setSearchResults) - .catch(setSearchError); - }; - - useClickOutside(searchBoxRef, () => { - reset(); - onClose(); - }); - - useEffect(() => { - searchInputRef.current?.focus(); - - getInitialFacets().then(setSearchResults).catch(setSearchError); - - return reset; - }, []); - - useEffect( - () => { - debounce(() => search(searchTerm), 1000)(); - }, - // we don't need to care about memoization of search function - // eslint-disable-next-line react-hooks/exhaustive-deps - [searchTerm, selectedFacet] - ); - - const reset = () => { - setSearchTerm(''); - setSearchResults(null); - setSelectedFacet(0); - }; - - const onSubmit = (e: React.FormEvent) => { - e.preventDefault(); - router.push(`/search?q=${searchTerm}§ion=${selectedFacetName}`); - onClose(); - }; - - const changeFacet = (idx: number) => setSelectedFacet(idx); - - const filterBySection = () => { - if (selectedFacet === 0) { - return {}; - } - - return { where: { siteSection: { eq: selectedFacetName } } }; - }; - - const facets: Facets = { - all: searchResults?.facets - ? Object.values(searchResults?.facets.siteSection.values).reduce( - (a, b) => a + b, - 0 - ) - : 0, - ...(searchResults?.facets?.siteSection?.values ?? {}), - }; - - const selectedFacetName = Object.keys(facets)[selectedFacet]; - - return ( -
-
-
-
- - - - -
- setSearchTerm(event.target.value)} - value={searchTerm} - /> -
-
- -
- {Object.keys(facets).map((facetName, idx) => ( - - ))} -
- -
- {searchError && } - - {!searchError && !searchTerm && } - - {!searchError && searchTerm && ( - <> - {searchResults && - searchResults.count > 0 && - searchResults.hits.map(hit => ( - - ))} - - {searchResults && searchResults.count === 0 && ( - - )} - - {searchResults && searchResults.count > 8 && ( - - )} - - )} -
- -
- -
-
-
-
- ); + const [searchTerm, setSearchTerm] = useState(""); + const [searchResults, setSearchResults] = useState(null); + const [selectedFacet, setSelectedFacet] = useState(0); + const [searchError, setSearchError] = useState>(null); + + const router = useRouter(); + const searchInputRef = useRef(null); + const searchBoxRef = useRef(null); + + const search = (term: string) => { + oramaSearch({ + term, + ...DEFAULT_ORAMA_QUERY_PARAMS, + mode: "fulltext", + returning: [ + "path", + "pageSectionTitle", + "pageTitle", + "path", + "siteSection", + ], + ...filterBySection(), + }) + .then(setSearchResults) + .catch(setSearchError); + }; + + useClickOutside(searchBoxRef, () => { + reset(); + onClose(); + }); + + useEffect(() => { + searchInputRef.current?.focus(); + + getInitialFacets().then(setSearchResults).catch(setSearchError); + + return reset; + }, []); + + useEffect( + () => { + search(searchTerm); + }, + // we don't need to care about memoization of search function + // eslint-disable-next-line react-hooks/exhaustive-deps + [searchTerm, selectedFacet], + ); + + const reset = () => { + setSearchTerm(""); + setSearchResults(null); + setSelectedFacet(0); + }; + + const onSubmit = (e: React.FormEvent) => { + e.preventDefault(); + router.push(`/search?q=${searchTerm}§ion=${selectedFacetName}`); + onClose(); + }; + + const changeFacet = (idx: number) => setSelectedFacet(idx); + + const filterBySection = () => { + if (selectedFacet === 0) { + return {}; + } + + return { where: { siteSection: { eq: selectedFacetName } } }; + }; + + const facets: Facets = { + all: searchResults?.facets + ? Object.values(searchResults?.facets.siteSection.values).reduce( + (a, b) => a + b, + 0, + ) + : 0, + ...(searchResults?.facets?.siteSection?.values ?? {}), + }; + + const selectedFacetName = Object.keys(facets)[selectedFacet]; + + return ( +
+
+
+
+ + + + +
+ setSearchTerm(event.target.value)} + value={searchTerm} + /> +
+
+ +
+ {Object.keys(facets).map((facetName, idx) => ( + + ))} +
+ +
+ {searchError && } + + {!searchError && !searchTerm && } + + {!searchError && searchTerm && ( + <> + {searchResults && + searchResults.count > 0 && + searchResults.hits.map((hit) => ( + + ))} + + {searchResults && searchResults.count === 0 && ( + + )} + + {searchResults && searchResults.count > 8 && ( + + )} + + )} +
+ +
+ +
+
+
+
+ ); }; From cb185288964eede1bb9c53cf41f63897d5458520 Mon Sep 17 00:00:00 2001 From: Michele Riva Date: Wed, 20 Mar 2024 08:48:22 -0700 Subject: [PATCH 2/2] fixes style --- .../Common/Search/States/WithSearchBox.tsx | 376 +++++++++--------- 1 file changed, 188 insertions(+), 188 deletions(-) diff --git a/components/Common/Search/States/WithSearchBox.tsx b/components/Common/Search/States/WithSearchBox.tsx index 9b90a507214c8..c15188d3c7b7b 100644 --- a/components/Common/Search/States/WithSearchBox.tsx +++ b/components/Common/Search/States/WithSearchBox.tsx @@ -1,26 +1,26 @@ -"use client"; +'use client'; import { - MagnifyingGlassIcon, - ChevronLeftIcon, -} from "@heroicons/react/24/outline"; -import type { Results, Nullable } from "@orama/orama"; -import classNames from "classnames"; -import { useState, useRef, useEffect } from "react"; -import type { FC } from "react"; - -import styles from "@/components/Common/Search/States/index.module.css"; -import { WithAllResults } from "@/components/Common/Search/States/WithAllResults"; -import { WithEmptyState } from "@/components/Common/Search/States/WithEmptyState"; -import { WithError } from "@/components/Common/Search/States/WithError"; -import { WithNoResults } from "@/components/Common/Search/States/WithNoResults"; -import { WithPoweredBy } from "@/components/Common/Search/States/WithPoweredBy"; -import { WithSearchResult } from "@/components/Common/Search/States/WithSearchResult"; -import { useClickOutside } from "@/hooks/react-client"; -import { useRouter } from "@/navigation.mjs"; -import { DEFAULT_ORAMA_QUERY_PARAMS } from "@/next.constants.mjs"; -import { search as oramaSearch, getInitialFacets } from "@/next.orama.mjs"; -import type { SearchDoc } from "@/types"; + MagnifyingGlassIcon, + ChevronLeftIcon, +} from '@heroicons/react/24/outline'; +import type { Results, Nullable } from '@orama/orama'; +import classNames from 'classnames'; +import { useState, useRef, useEffect } from 'react'; +import type { FC } from 'react'; + +import styles from '@/components/Common/Search/States/index.module.css'; +import { WithAllResults } from '@/components/Common/Search/States/WithAllResults'; +import { WithEmptyState } from '@/components/Common/Search/States/WithEmptyState'; +import { WithError } from '@/components/Common/Search/States/WithError'; +import { WithNoResults } from '@/components/Common/Search/States/WithNoResults'; +import { WithPoweredBy } from '@/components/Common/Search/States/WithPoweredBy'; +import { WithSearchResult } from '@/components/Common/Search/States/WithSearchResult'; +import { useClickOutside } from '@/hooks/react-client'; +import { useRouter } from '@/navigation.mjs'; +import { DEFAULT_ORAMA_QUERY_PARAMS } from '@/next.constants.mjs'; +import { search as oramaSearch, getInitialFacets } from '@/next.orama.mjs'; +import type { SearchDoc } from '@/types'; type Facets = { [key: string]: number }; @@ -29,171 +29,171 @@ type SearchResults = Nullable>; type SearchBoxProps = { onClose: () => void }; export const WithSearchBox: FC = ({ onClose }) => { - const [searchTerm, setSearchTerm] = useState(""); - const [searchResults, setSearchResults] = useState(null); - const [selectedFacet, setSelectedFacet] = useState(0); - const [searchError, setSearchError] = useState>(null); - - const router = useRouter(); - const searchInputRef = useRef(null); - const searchBoxRef = useRef(null); - - const search = (term: string) => { - oramaSearch({ - term, - ...DEFAULT_ORAMA_QUERY_PARAMS, - mode: "fulltext", - returning: [ - "path", - "pageSectionTitle", - "pageTitle", - "path", - "siteSection", - ], - ...filterBySection(), - }) - .then(setSearchResults) - .catch(setSearchError); - }; - - useClickOutside(searchBoxRef, () => { - reset(); - onClose(); - }); - - useEffect(() => { - searchInputRef.current?.focus(); - - getInitialFacets().then(setSearchResults).catch(setSearchError); - - return reset; - }, []); - - useEffect( - () => { - search(searchTerm); - }, - // we don't need to care about memoization of search function - // eslint-disable-next-line react-hooks/exhaustive-deps - [searchTerm, selectedFacet], - ); - - const reset = () => { - setSearchTerm(""); - setSearchResults(null); - setSelectedFacet(0); - }; - - const onSubmit = (e: React.FormEvent) => { - e.preventDefault(); - router.push(`/search?q=${searchTerm}§ion=${selectedFacetName}`); - onClose(); - }; - - const changeFacet = (idx: number) => setSelectedFacet(idx); - - const filterBySection = () => { - if (selectedFacet === 0) { - return {}; - } - - return { where: { siteSection: { eq: selectedFacetName } } }; - }; - - const facets: Facets = { - all: searchResults?.facets - ? Object.values(searchResults?.facets.siteSection.values).reduce( - (a, b) => a + b, - 0, - ) - : 0, - ...(searchResults?.facets?.siteSection?.values ?? {}), - }; - - const selectedFacetName = Object.keys(facets)[selectedFacet]; - - return ( -
-
-
-
- - - - -
- setSearchTerm(event.target.value)} - value={searchTerm} - /> -
-
- -
- {Object.keys(facets).map((facetName, idx) => ( - - ))} -
- -
- {searchError && } - - {!searchError && !searchTerm && } - - {!searchError && searchTerm && ( - <> - {searchResults && - searchResults.count > 0 && - searchResults.hits.map((hit) => ( - - ))} - - {searchResults && searchResults.count === 0 && ( - - )} - - {searchResults && searchResults.count > 8 && ( - - )} - - )} -
- -
- -
-
-
-
- ); + const [searchTerm, setSearchTerm] = useState(''); + const [searchResults, setSearchResults] = useState(null); + const [selectedFacet, setSelectedFacet] = useState(0); + const [searchError, setSearchError] = useState>(null); + + const router = useRouter(); + const searchInputRef = useRef(null); + const searchBoxRef = useRef(null); + + const search = (term: string) => { + oramaSearch({ + term, + ...DEFAULT_ORAMA_QUERY_PARAMS, + mode: 'fulltext', + returning: [ + 'path', + 'pageSectionTitle', + 'pageTitle', + 'path', + 'siteSection', + ], + ...filterBySection(), + }) + .then(setSearchResults) + .catch(setSearchError); + }; + + useClickOutside(searchBoxRef, () => { + reset(); + onClose(); + }); + + useEffect(() => { + searchInputRef.current?.focus(); + + getInitialFacets().then(setSearchResults).catch(setSearchError); + + return reset; + }, []); + + useEffect( + () => { + search(searchTerm); + }, + // we don't need to care about memoization of search function + // eslint-disable-next-line react-hooks/exhaustive-deps + [searchTerm, selectedFacet] + ); + + const reset = () => { + setSearchTerm(''); + setSearchResults(null); + setSelectedFacet(0); + }; + + const onSubmit = (e: React.FormEvent) => { + e.preventDefault(); + router.push(`/search?q=${searchTerm}§ion=${selectedFacetName}`); + onClose(); + }; + + const changeFacet = (idx: number) => setSelectedFacet(idx); + + const filterBySection = () => { + if (selectedFacet === 0) { + return {}; + } + + return { where: { siteSection: { eq: selectedFacetName } } }; + }; + + const facets: Facets = { + all: searchResults?.facets + ? Object.values(searchResults?.facets.siteSection.values).reduce( + (a, b) => a + b, + 0 + ) + : 0, + ...(searchResults?.facets?.siteSection?.values ?? {}), + }; + + const selectedFacetName = Object.keys(facets)[selectedFacet]; + + return ( +
+
+
+
+ + + + +
+ setSearchTerm(event.target.value)} + value={searchTerm} + /> +
+
+ +
+ {Object.keys(facets).map((facetName, idx) => ( + + ))} +
+ +
+ {searchError && } + + {!searchError && !searchTerm && } + + {!searchError && searchTerm && ( + <> + {searchResults && + searchResults.count > 0 && + searchResults.hits.map(hit => ( + + ))} + + {searchResults && searchResults.count === 0 && ( + + )} + + {searchResults && searchResults.count > 8 && ( + + )} + + )} +
+ +
+ +
+
+
+
+ ); };