search: Empty input on open, and show current narrow search suggestions.

Fixes #30911
This commit is contained in:
evykassirer 2024-07-17 20:06:04 -07:00 committed by Tim Abbott
parent 26429a81e0
commit d5883cfbb2
3 changed files with 65 additions and 19 deletions

View File

@ -179,8 +179,6 @@ async function search_and_check(
): Promise<void> {
await page.click(".search_icon");
await page.waitForSelector(".navbar-search.expanded", {visible: true});
// Close the "in: home" pill
await page.click(".navbar-search .pill-close-button");
await common.select_item_via_typeahead(page, "#search_query", search_str, item_to_select);
// Enter to trigger search
await page.keyboard.press("Enter");
@ -193,8 +191,6 @@ async function search_and_check(
async function search_silent_user(page: Page, str: string, item: string): Promise<void> {
await page.click(".search_icon");
await page.waitForSelector(".navbar-search.expanded", {visible: true});
// Close the "in: home" pill
await page.click(".navbar-search .pill-close-button");
await common.select_item_via_typeahead(page, "#search_query", str, item);
// Enter to trigger search
await page.keyboard.press("Enter");

View File

@ -6,7 +6,6 @@ import render_search_list_item from "../templates/search_list_item.hbs";
import {Typeahead} from "./bootstrap_typeahead";
import type {TypeaheadInputElement} from "./bootstrap_typeahead";
import {Filter} from "./filter";
import type {RemovePillTrigger} from "./input_pill";
import * as keydown_util from "./keydown_util";
import * as narrow_state from "./narrow_state";
import * as popovers from "./popovers";
@ -168,7 +167,13 @@ export function initialize(opts: {on_narrow_search: OnNarrowSearch}): void {
assert(search_pill_widget !== null);
const query_from_pills =
search_pill.get_current_search_string_for_widget(search_pill_widget);
const suggestions = search_suggestion.get_suggestions(query_from_pills, query);
const add_current_filter =
query_from_pills === "" && narrow_state.filter() !== undefined;
const suggestions = search_suggestion.get_suggestions(
query_from_pills,
query,
add_current_filter,
);
// Update our global search_map hash
search_map = suggestions.lookup_table;
return suggestions.strings;
@ -361,7 +366,7 @@ export function initialize(opts: {on_narrow_search: OnNarrowSearch}): void {
}
export function initiate_search(): void {
open_search_bar_and_close_narrow_description();
open_search_bar_and_close_narrow_description(true);
focus_search_input_at_end();
// Open the typeahead after opening the search bar, so that we don't
@ -372,15 +377,17 @@ export function initiate_search(): void {
// we rely entirely on this function to ensure
// the searchbar has the right text/pills.
function reset_searchbox(): void {
function reset_searchbox(clear = false): void {
assert(search_pill_widget !== null);
search_pill_widget.clear(true);
search_input_has_changed = false;
search_pill.set_search_bar_contents(
narrow_state.search_terms(),
search_pill_widget,
set_search_bar_text,
);
if (!clear) {
search_pill.set_search_bar_contents(
narrow_state.search_terms(),
search_pill_widget,
set_search_bar_text,
);
}
}
function exit_search(opts: {keep_search_narrow_open: boolean}): void {
@ -399,8 +406,8 @@ function exit_search(opts: {keep_search_narrow_open: boolean}): void {
$(".app").trigger("focus");
}
export function open_search_bar_and_close_narrow_description(): void {
reset_searchbox();
export function open_search_bar_and_close_narrow_description(clear = false): void {
reset_searchbox(clear);
$(".navbar-search").addClass("expanded");
$("#message_view_header").addClass("hidden");
popovers.hide_all();

View File

@ -842,13 +842,51 @@ function suggestion_search_string(suggestion_line: SuggestionLine): string {
return search_strings.join(" ");
}
function suggestions_for_current_filter(): SuggestionLine[] {
if (narrow_state.stream_name() && narrow_state.topic() !== "") {
return [
get_default_suggestion_line([
{
operator: "channel",
operand: narrow_state.stream_name()!,
},
]),
get_default_suggestion_line(narrow_state.search_terms()),
];
}
if (narrow_state.pm_emails_string()) {
return [
get_default_suggestion_line([
{
operator: "is",
operand: "dm",
},
]),
get_default_suggestion_line(narrow_state.search_terms()),
];
}
return [get_default_suggestion_line(narrow_state.search_terms())];
}
class Attacher {
result: SuggestionLine[] = [];
prev = new Set<string>();
base: SuggestionLine;
add_current_filter: boolean;
constructor(base: SuggestionLine) {
constructor(base: SuggestionLine, search_query_is_empty: boolean, add_current_filter: boolean) {
this.base = base;
this.add_current_filter = add_current_filter;
// Sometimes we add suggestions with the current filter in case
// the user wants to search within the current filter. For an empty
// search query, we put the current filter suggestions at the start
// of the list.
if (search_query_is_empty && this.add_current_filter) {
this.add_current_filter = false;
for (const current_filter_line of suggestions_for_current_filter()) {
this.push(current_filter_line);
}
}
}
push(suggestion_line: SuggestionLine): void {
@ -926,7 +964,11 @@ class Attacher {
}
}
export function get_search_result(query_from_pills: string, query_from_text: string): Suggestion[] {
export function get_search_result(
query_from_pills: string,
query_from_text: string,
add_current_filter = false,
): Suggestion[] {
let suggestion_line: SuggestionLine;
// search_terms correspond to the terms for the query in the input.
@ -971,7 +1013,7 @@ export function get_search_result(query_from_pills: string, query_from_text: str
const base_terms = [...pill_search_terms, ...text_search_terms.slice(0, -1)];
const base = get_default_suggestion_line(base_terms);
const attacher = new Attacher(base);
const attacher = new Attacher(base, all_search_terms.length === 0, add_current_filter);
// Display the default first, unless it has invalid terms.
if (last.operator === "search") {
@ -1055,11 +1097,12 @@ export function get_search_result(query_from_pills: string, query_from_text: str
export function get_suggestions(
query_from_pills: string,
query_from_text: string,
add_current_filter = false,
): {
strings: string[];
lookup_table: Map<string, Suggestion>;
} {
const result = get_search_result(query_from_pills, query_from_text);
const result = get_search_result(query_from_pills, query_from_text, add_current_filter);
return finalize_search_result(result);
}