Skip to content

Commit

Permalink
fix(search-bar): properly escape and unescape URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Mar 19, 2024
1 parent d490348 commit 0306456
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions site/search/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getIndexName,
parseIndexName,
} from "./searchClient.js"
import { queryParamsToStr } from "@ourworldindata/utils"

type BaseItem = Record<string, unknown>

Expand All @@ -35,7 +36,9 @@ const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
return {
...source,
onSelect({ item, navigator }) {
navigator.navigate({ itemUrl: `/search?q=${item.id}` } as any)
navigator.navigate({
itemUrl: `/search${queryParamsToStr({ q: item.id })}`,
} as any)
},
templates: {
...source.templates,
Expand Down Expand Up @@ -81,7 +84,7 @@ const FeaturedSearchesSource: AutocompleteSource<BaseItem> = {
return ["CO2", "Energy", "Education", "Poverty", "Democracy"].map(
(term) => ({
title: term,
slug: `/search?q=${term}`,
slug: `/search${queryParamsToStr({ q: term })}`,
})
)
},
Expand Down Expand Up @@ -184,7 +187,7 @@ const AllResultsSource: AutocompleteSource<BaseItem> = {
getItems({ query }) {
return [
{
slug: `/search?q=${encodeURI(query)}`,
slug: `/search${queryParamsToStr({ q: query })}`,
title: `All search results for "${query}"`,
},
]
Expand Down Expand Up @@ -242,7 +245,7 @@ export function Autocomplete({
onSubmit({ state, navigator }) {
if (!state.query) return
navigator.navigate({
itemUrl: `/search?q=${state.query}`,
itemUrl: `/search${queryParamsToStr({ q: state.query })}`,
// this method is incorrectly typed - `item` and `state` are optional
} as any)
},
Expand Down
2 changes: 1 addition & 1 deletion site/search/SearchPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class InstantSearchContainer extends React.Component {
if (params.q) {
// Algolia runs the search and fills the searchbox input regardless
// we just need this class to be aware that a query exists so that it doesn't hide the results
this.inputValue = decodeURI(params.q)
this.inputValue = params.q
}
}

Expand Down

0 comments on commit 0306456

Please sign in to comment.