|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Ronan LE MEILLAT |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import { Input } from "@heroui/input"; |
| 26 | +import { Kbd } from "@heroui/kbd"; |
| 27 | +import { useTranslation } from "react-i18next"; |
| 28 | +import { SearchIcon } from "@/components/icons"; |
| 29 | +import { usePurchaseSearch } from "@/hooks/usePurchaseSearch"; |
| 30 | +import { useSearch } from "@/context/SearchContext"; |
| 31 | +import { AuthenticationGuardWithPermission } from "./auth0"; |
| 32 | + |
| 33 | +interface SearchBarProps { |
| 34 | + onSearchResults?: (results: string[]) => void; |
| 35 | +} |
| 36 | + |
| 37 | +export const SearchBar = ({ onSearchResults }: SearchBarProps) => { |
| 38 | + const { t } = useTranslation(); |
| 39 | + const { setSearchResults, setSearchQuery } = useSearch(); |
| 40 | + |
| 41 | + const handleSearchResultsReceived = (results: string[]) => { |
| 42 | + setSearchResults(results); |
| 43 | + onSearchResults?.(results); |
| 44 | + }; |
| 45 | + |
| 46 | + const { searchQuery, isSearching, handleSearchChange, clearSearch } = |
| 47 | + usePurchaseSearch(handleSearchResultsReceived); |
| 48 | + |
| 49 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 50 | + setSearchQuery(e.target.value); |
| 51 | + handleSearchChange(e.target.value); |
| 52 | + }; |
| 53 | + |
| 54 | + const handleClear = () => { |
| 55 | + clearSearch(); |
| 56 | + setSearchQuery(""); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <AuthenticationGuardWithPermission |
| 61 | + key={`nav-search`} |
| 62 | + permission="search:api"> |
| 63 | + <Input |
| 64 | + aria-label={t("search")} |
| 65 | + classNames={{ |
| 66 | + inputWrapper: "bg-default-100", |
| 67 | + input: "text-sm", |
| 68 | + }} |
| 69 | + endContent={ |
| 70 | + <Kbd className="hidden lg:inline-block" keys={["command"]}> |
| 71 | + K |
| 72 | + </Kbd> |
| 73 | + } |
| 74 | + labelPlacement="outside" |
| 75 | + placeholder={`${t("search")}…`} |
| 76 | + startContent={ |
| 77 | + <SearchIcon className="text-base text-default-400 pointer-events-none flex-shrink-0" /> |
| 78 | + } |
| 79 | + type="search" |
| 80 | + value={searchQuery} |
| 81 | + isDisabled={isSearching} |
| 82 | + onChange={handleChange} |
| 83 | + onClear={handleClear} |
| 84 | + isClearable |
| 85 | + /> |
| 86 | + </AuthenticationGuardWithPermission> |
| 87 | + ); |
| 88 | +}; |
0 commit comments