Search results sort as the filter dialog says (#959)

* Search results sort as the filter dialog says

Docs sorted ascending orders backwards and fell back to id order for every
descending order (`-size`, `-published_datetime`); it now takes the same `-`
prefixed orders as the other searches and rejects unknown ones with a 400.

Archives and Docs searches without an order now put the most relevant results
first, as Videos does and as the dialog shows.

Rank has no direction in the dialog: choosing it sends `rank` (most relevant
first) instead of inheriting a descending `-rank` (least relevant first).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* An old -rank link becomes rank when the filter dialog closes

The dialog shows Rank without a direction, but it seeded its draft from the URL
unchanged, so Done left a saved `-rank` (least relevant first) in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-11 19:42:23 -07:00 • committed by GitHub
parent 2800112f3e
commit f1d344d8a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 170 additions and 33 deletions

View file

@ -481,6 +481,9 @@ export function DocSearchFilterButton({size = 'medium'}) {
return <SearchFilter filters={docMimetypeFilterOptions} size={size}/>
}
// The search relevance sort; see `SearchFilterModal`.
const RANK_ORDER = 'rank';
// The app-wide default page size (matches usePages' fallback); used to reset Results Per Page on Clear All.
const DEFAULT_SEARCH_LIMIT = 24;
@ -540,7 +543,8 @@ export function SearchFilterModal(
// Seed the drafts from the URL each time the modal is opened.
React.useEffect(() => {
if (open) {
setDraftSort(sort || null);
// Rank has no direction here; an old `-rank` link becomes `rank` when the modal closes.
setDraftSort(sort === `-${RANK_ORDER}` ? RANK_ORDER : (sort || null));
setDraftFilter(filter || null);
setDraftTags(activeTags || []);
setDraftAnyTag(anyTag || false);
@ -614,17 +618,19 @@ export function SearchFilterModal(
setDraftDeep(false);
}
// Sort section (operates on the draft).
// Sort section (operates on the draft). Rank has no direction: `rank` is always the most relevant first
// (`-rank` would be the least relevant first), and a sort chosen after it starts descending.
const sortKey = draftSort ? draftSort.replace(/^-/, '') : (sorts ? sorts[0]['value'] : null);
const desc = draftSort ? draftSort.startsWith('-') : true;
const isRank = sortKey === RANK_ORDER;
const desc = draftSort && !isRank ? draftSort.startsWith('-') : true;
const sortButtons = sorts && sorts.map(o =>
<Button key={o['value']}
color={o['value'] === sortKey ? 'violet' : undefined}
onClick={() => setDraftSort(desc ? `-${o['value']}` : o['value'])}
onClick={() => setDraftSort(o['value'] === RANK_ORDER || !desc ? o['value'] : `-${o['value']}`)}
style={{margin: '0.2em'}}
>{o['text']}</Button>
);
const directionToggle = <Button icon={desc ? IconSortDescending : IconSortAscending}
const directionToggle = isRank ? null : <Button icon={desc ? IconSortDescending : IconSortAscending}
onClick={() => {
const base = sortKey || sorts[0]['value'];
setDraftSort(desc ? base : `-${base}`);
@ -658,7 +664,7 @@ export function SearchFilterModal(
<Stack gap='lg'>
{sorts &&
<SearchFilterSection header='Sort By'>
<div style={{marginBottom: '1em'}}>{directionToggle}</div>
{directionToggle && <div style={{marginBottom: '1em'}}>{directionToggle}</div>}
<div>{sortButtons}</div>
</SearchFilterSection>}

View file

@ -94,6 +94,41 @@ describe('SearchFilterButton', () => {
expect(spy.mock.calls[0][0]).toMatchObject({order: '-size'});
});
it('Rank always sorts the most relevant first, and has no direction', async () => {
const rankOrders = [{value: 'rank', text: 'Rank'}, ...videoOrders];
const spy = jest.fn();
// The previous sort was descending; Rank must not inherit it as `-rank` (least relevant first).
renderButton({sorts: rankOrders}, {order: '-size'}, spy);
fireEvent.click(screen.getByText('Filter'));
fireEvent.click(await screen.findByText('Rank'));
expect(screen.queryByText('Descending')).toBeNull();
expect(screen.queryByText('Ascending')).toBeNull();
fireEvent.click(screen.getByText('Done'));
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0]).toMatchObject({order: 'rank'});
});
it('Done rewrites an old `-rank` link to the most relevant first', async () => {
const rankOrders = [{value: 'rank', text: 'Rank'}, ...videoOrders];
const spy = jest.fn();
renderButton({sorts: rankOrders}, {order: '-rank'}, spy);
fireEvent.click(screen.getByText('Filter'));
fireEvent.click(await screen.findByText('Done'));
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0]).toMatchObject({order: 'rank'});
});
it('a sort chosen after Rank is descending', async () => {
const rankOrders = [{value: 'rank', text: 'Rank'}, ...videoOrders];
const spy = jest.fn();
renderButton({sorts: rankOrders}, {order: 'rank'}, spy);
fireEvent.click(screen.getByText('Filter'));
fireEvent.click(await screen.findByText('Size'));
expect(screen.getByText('Descending')).toBeInTheDocument();
fireEvent.click(screen.getByText('Done'));
expect(spy.mock.calls[0][0]).toMatchObject({order: '-size'});
});
it('Clear All resets results-per-page to the default on close', async () => {
const spy = jest.fn();
renderButton({sorts: videoOrders}, {l: '96'}, spy);