Skip to content

Commit

Permalink
feat: store selected sort option in query string
Browse files Browse the repository at this point in the history
  • Loading branch information
pomegranited committed Jul 10, 2024
1 parent 9cd428e commit b440b92
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/search-manager/SearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* https://github.com/algolia/instantsearch/issues/1658
*/
import React from 'react';
import { useSearchParams } from 'react-router-dom';
import { MeiliSearch, type Filter } from 'meilisearch';

import { ContentHit, SearchSortOption } from './data/api';
Expand Down Expand Up @@ -46,11 +47,22 @@ export const SearchContextProvider: React.FC<{
const [searchKeywords, setSearchKeywords] = React.useState('');
const [blockTypesFilter, setBlockTypesFilter] = React.useState<string[]>([]);
const [tagsFilter, setTagsFilter] = React.useState<string[]>([]);
const [searchParams, setSearchParams] = useSearchParams();

// Use the selected search order option
// Note: SearchSortOption.RELEVANCE is special -- it is empty string, and represents the default sort setting.
const [searchSortOrder, setSearchSortOrder] = React.useState(SearchSortOption.RELEVANCE);
const sort = searchSortOrder ? [searchSortOrder] : [];
// The search sort order can be set in the query string, or the state.
// E.g. ?sort=display_name:desc maps to SearchSortOption.TITLE_ZA.
// TODO: generalize this approach in case we want to use it for keyword / filters too.
const [tmpSearchSortOrder, tmpSetSearchSortOrder] = React.useState(SearchSortOption.RELEVANCE);
const sortParam: SearchSortOption | string | undefined = searchParams.get('sort');

Check failure on line 56 in src/search-manager/SearchManager.ts

View workflow job for this annotation

GitHub Actions / tests

Type 'string | null' is not assignable to type 'string | undefined'.
const searchSortOrder = Object.values(SearchSortOption).includes(sortParam) ? sortParam : tmpSearchSortOrder;

Check failure on line 57 in src/search-manager/SearchManager.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type 'string | undefined' is not assignable to parameter of type 'SearchSortOption'.
const setSearchSortOrder = (value: SearchSortOption) => {
// Update the URL parameters to store the selected search option
setSearchParams({ ...searchParams, sort: value }, { replace: true });

Check failure on line 60 in src/search-manager/SearchManager.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type '{ sort: SearchSortOption; append(name: string, value: string): void; delete(name: string): void; get(name: string): string | null; getAll(name: string): string[]; ... 8 more ...; size: number; }' is not assignable to parameter of type 'URLSearchParamsInit | ((prev: URLSearchParams) => URLSearchParamsInit) | undefined'.
tmpSetSearchSortOrder(value);
};
// Note: SearchSortOption.RELEVANCE is special -- it means "no custom
// sorting", so we send it as an empty array.
const sort = searchSortOrder === SearchSortOption.RELEVANCE ? [] : [searchSortOrder];

const canClearFilters = blockTypesFilter.length > 0 || tagsFilter.length > 0;
const clearFilters = React.useCallback(() => {
Expand Down

0 comments on commit b440b92

Please sign in to comment.