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: Can't search for more than one keyword #367

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ const TagsOptions: FC<ITagsOptions> = ({
{widget_setting.setting.name}:
</div>
<SearchBar
initialInputValue={searchState}
options={searchState ? options : undefined}
updateSearch={false}
initialSearchValues={[]}
Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/src/mobile-components/FilterSearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const FilterSearchBar = <T extends TOption>({
updateSearch={false}
isOptionSelected={isOptionSelected}
message={message}
closeMenuOnSelect
autoFocus
/>
</span>
</div>
Expand Down
40 changes: 10 additions & 30 deletions packages/frontend/src/mobile-containers/SuperfeedContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ const SuperfeedContainer: FC<{
const { setSearchState, keywordResults, isFetchingKeywordResults } =
useFilterKeywordSearch();

const flattenedKeywordResults = useMemo(
() =>
Object.values(keywordResults ?? {}).reduce(
(acc, curr) => [...acc, ...curr],
[]
),
[keywordResults]
);

const selectedLocalFilters = useAppSelector(selectedLocalFiltersSelector);
const selectedSyncedFilters = useAppSelector(selectedSyncedFiltersSelector);

Expand Down Expand Up @@ -210,15 +201,14 @@ const SuperfeedContainer: FC<{
*
* In this case, we want to set the search state to the tags.
*/
if (
tagsFromSearch &&
!flattenedKeywordResults.find((kw) =>
tagsFromSearch.includes(kw.slug)
)
) {
if (tagsFromSearch) {
Logger.debug(
"Setting search state from search params:",
tagsFromSearch
);
setSearchState(tagsFromSearch);
}
}, [tagsFromSearch, flattenedKeywordResults, setSearchState]);
}, [tagsFromSearch, setSearchState]);

const keywordOptions = useMemo(
() => groupedKeywordsAsOptions(keywordResults),
Expand All @@ -227,30 +217,20 @@ const SuperfeedContainer: FC<{

const initialSearchValues = useMemo(() => {
if (!tagsFromSearch) return undefined;
const matchedKeywords = tagsFromSearch
?.split(",")
.map((tag) => {
return flattenedKeywordResults.filter((t) => t.slug === tag)[0];
})
.filter((kw) => kw)
.map((kw) => ({
...kw,
label: kw.name,
value: kw.slug,
}));
if (matchedKeywords.length > 0) return matchedKeywords;
return tagsFromSearch?.split(",").map((tagName, i) => ({
// this is not accurate, but unfortunately the filter_keywords endpoint doesn't
// provide good results so there is no guarantee that a superfeed item keyword
// exists in the filter_keywords response
// exists in the filter_keywords response. Also, the filter_keywords response
// only includes results from the current search, but the search bar can include
// previously searched keywords.
id: i,
name: tagName,
slug: tagName,
type: ESupportedFilters.ConceptTags,
label: tagName,
value: tagName,
}));
}, [tagsFromSearch, flattenedKeywordResults]);
}, [tagsFromSearch]);

return (
<AudioPlayerProvider>
Expand Down
9 changes: 5 additions & 4 deletions packages/ui-kit/src/components/search/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ export interface ISearchProps<Option = unknown> {
trendingOptions?: Option[] | undefined;
disabled?: boolean;
placeholder: string;
initialInputValue?: string;
initialSearchValues: Option[];
closeMenuOnSelect?: boolean;
updateSearch?: boolean;
autoFocus?: boolean;
isFetchingKeywordResults?: boolean;
isFetchingTrendingKeywordResults?: boolean;
showBackdrop?: boolean;
Expand Down Expand Up @@ -183,9 +183,9 @@ export const SearchBar = <T,>({
initialSearchValues,
closeMenuOnSelect = false,
updateSearch = true,
autoFocus = false,
componentClassNames,
customComponents,
initialInputValue,
isFetchingKeywordResults,
isFetchingTrendingKeywordResults,
showBackdrop,
Expand Down Expand Up @@ -218,6 +218,7 @@ export const SearchBar = <T,>({
}
};

// keep input state on blur
const handleInputChange = useCallback(
(value: string, meta: InputActionMeta) => {
if (["input-blur", "menu-close"].indexOf(meta.action) === -1) {
Expand Down Expand Up @@ -315,7 +316,7 @@ export const SearchBar = <T,>({
<Select
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
autoFocus
autoFocus={autoFocus}
onChange={(e, changeType) => handleSearchValues(e, changeType)}
onInputChange={handleInputChange}
isClearable
Expand All @@ -341,7 +342,7 @@ export const SearchBar = <T,>({
onKeyDown={(e) => {
handleKeyDown(e);
}}
inputValue={initialInputValue}
inputValue={inputValue}
/>
</div>
);
Expand Down
Loading