()
| 43 | const optionsFilter: OptionsFilter = ({ options }) => options |
| 44 | |
| 45 | export default function SearchBox() { |
| 46 | const router = useRouter() |
| 47 | const params = useParams() |
| 48 | const searchString = searchStringFromParams(params) |
| 49 | |
| 50 | const [currentTerm, setCurrentTerm] = useState('') |
| 51 | const [debouncedSearchValue] = useDebounce(currentTerm, 300) |
| 52 | const { data } = useSuspenseQuery(query, { |
| 53 | variables: { |
| 54 | repoIds: [], |
| 55 | searchString, |
| 56 | topicSynonymSearchString: debouncedSearchValue, |
| 57 | viewerId: '', |
| 58 | }, |
| 59 | }) |
| 60 | const { view: { queryInfo } } = data |
| 61 | const [searchTerms, setSearchTerms] = useState<string[]>(termsFromQueryInfo(queryInfo)) |
| 62 | |
| 63 | const synonyms = data.view?.topicLiveSearch?.synonyms || [] |
| 64 | const options: ComboboxItem[] = [] |
| 65 | const seen = new Set() |
| 66 | const newQueryInfo: Map<string, string> = new Map() |
| 67 | |
| 68 | synonyms.forEach(({ displayName, id }) => { |
| 69 | if (!seen.has(id)) { |
| 70 | seen.add(id) |
| 71 | options.push({ value: id, label: displayName }) |
| 72 | newQueryInfo.set(displayName, id) |
| 73 | } |
| 74 | }) |
| 75 | |
| 76 | const searchTermsUpdated = useCallback(async (newSearchTerms: string[]) => { |
| 77 | setSearchTerms(newSearchTerms) |
| 78 | |
| 79 | // Allow the search box to be cleared without having side effects |
| 80 | if (newSearchTerms.length > 0) { |
| 81 | const path = buildPath(newSearchTerms, queryInfo, newQueryInfo) |
| 82 | router.push(path) |
| 83 | } |
| 84 | }, [setSearchTerms, queryInfo, params, searchStringFromParams]) |
| 85 | |
| 86 | return ( |
| 87 | <FocusTrap> |
| 88 | <TagsInput |
| 89 | allowDuplicates |
| 90 | clearable |
| 91 | data={options} |
| 92 | disabled={false} |
| 93 | filter={optionsFilter} |
| 94 | leftSection={icon} |
| 95 | onChange={searchTermsUpdated} |
| 96 | onSearchChange={setCurrentTerm} |
| 97 | placeholder="Search" |
| 98 | radius="xl" |
| 99 | searchValue={currentTerm} |
| 100 | size="lg" |
| 101 | value={searchTerms} |
| 102 | /> |
nothing calls this directly
no test coverage detected