sortOptions
Returns the five product sort orders the listing understands, with names in the page language and the current one flagged, ready for a select.
import { sortOptions } from '@salla.sa/twilight-theme-engine/utils';In plain words
Product lists let shoppers sort: suggested, best selling, top rated, price low to high, price high to low. sortOptions(current, t) returns those five as a list of { id, name, is_selected }, with name translated. Use id as each option's value and name as its label.
The listing page reads the chosen id from the sort parameter of the address, as in ?sort=bestSell.
Signature
function sortOptions(
currentSort: string,
t: (key: string, fallback: string) => string
): Array<{ id: string; name: string; is_selected: boolean }>
// ids, in this order:
// 'ourSuggest' | 'bestSell' | 'topRated' | 'priceFromLowToTop' | 'priceFromTopToLow'Try it live
ourSuggest | مقترحاتنا | is_selected: false |
bestSell | الاكثر مبيعاً | is_selected: true |
topRated | الاعلى تقييماً | is_selected: false |
priceFromLowToTop | السعر من الاقل إلى الاعلى | is_selected: false |
priceFromTopToLow | السعر من الاعلى إلى الاقل | is_selected: false |
import { useMemo } from 'react';
import { useTranslation } from '@salla.sa/twilight-theme-engine/i18n';
import { useLocation, useNavigate } from '@salla.sa/twilight-theme-engine/providers';
import { sortOptions } from '@salla.sa/twilight-theme-engine/utils';
export function SortSelect({ current = 'bestSell' }: { current?: string }) {
const { t } = useTranslation();
const navigate = useNavigate();
const location = useLocation();
const options = useMemo(() => sortOptions(current, t), [current, t]);
return (
<select
className="form-input"
value={current}
onChange={(event) => {
// Built from the router's location, never window.location.
const params = new URLSearchParams(location.searchStr);
params.set('sort', event.target.value);
params.delete('page');
navigate(`${location.pathname}?${params.toString()}`);
}}
>
{options.map((option) => (
<option key={option.id} value={option.id}>
{option.name}
</option>
))}
</select>
);
}
Example
import { useMemo } from 'react';
import { useTranslation } from '@salla.sa/twilight-theme-engine/i18n';
import { useLocation, useNavigate } from '@salla.sa/twilight-theme-engine/providers';
import { sortOptions } from '@salla.sa/twilight-theme-engine/utils';
export function SortSelect({ current }: { current: string }) {
const { t } = useTranslation();
const navigate = useNavigate();
const location = useLocation();
const options = useMemo(() => sortOptions(current, t), [current, t]);
return (
<select
className="form-input"
value={current}
onChange={(event) => {
const params = new URLSearchParams(location.searchStr);
params.set('sort', event.target.value);
params.delete('page');
navigate(`${location.pathname}?${params.toString()}`);
}}
>
{options.map((option) => (
<option key={option.id} value={option.id}>
{option.name}
</option>
))}
</select>
);
}
How it behaves
The names come from
twith the keyspages.categories.sort_by_suggestions,sort_by_sales,sort_by_rating,sort_by_price_azandsort_by_price_za, falling back to "Our Suggestions", "Best Seller", "Top Rated", "Price Low to High" and "Price High to Low". PasstfromuseTranslation().is_selectedis simplycurrentSort === id. The listing loader usesourSuggestwhen the address has nosort, and passes the value to the products API unchanged.The engine's
ProductListPagebuilds its select from it, exactly like the example: it setssort, removespageand navigates with the router's location.It returns a new array on every call; wrap it in
useMemowhen a child depends on its identity.
Gotchas
Building the new address from
window.locationadds the store segment a second time onlocalhostand the preview host, where the address bar carries it. UseuseLocation()anduseNavigate()from@salla.sa/twilight-theme-engine/providers.A
currentvalue that is not one of the five ids flags nothing, and a controlled select then shows its first option.
Related
One module behind every product grid: categories, search, tags, brand pages, and the latest, best-selling and offers lists.
useNavigateReturns a navigate(path) function that moves to another store page from code, without reloading the whole page.
useLocationReads the current address as the router sees it (path, parsed query, raw query, hash), in the server render and in the browser.
Source and docs
- Engine source:
packages/theme-engine/src/utils/sort-options.ts