Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

topic: Auto-open topic typeahead when creation is disallowed.

Aman Agrawal4bf90b799a93
diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.ts
index c714a45397a..8337bf6100a 100644
--- a/web/src/composebox_typeahead.ts
+++ b/web/src/composebox_typeahead.ts
@@ -1576,6 +1576,35 @@ function compose_trigger_selection(event: JQuery.KeyDownEvent): boolean {
return false;
}
+// Used to hide the auto-opened topic typeahead once the input already
+// names an existing topic and no other topic still matches the query,
+// since there is then nothing left to suggest. We never suppress an
+// empty query (the typeahead should still list the existing topics)
+// nor when there are non-topic suggestions such as people to show.
+export function should_suppress_topic_typeahead(
+ query: string,
+ items: (string | UserPillData)[],
+): boolean {
+ if (!query) {
+ return false;
+ }
+ if (items.some((item) => typeof item !== "string")) {
+ return false;
+ }
+ const topics = items.filter((item): item is string => typeof item === "string");
+ const normalize = (topic: string): string =>
+ util.get_final_topic_display_name(topic).trim().toLowerCase();
+ const normalized_query = normalize(query);
+ if (!topics.some((topic) => normalize(topic) === normalized_query)) {
+ return false;
+ }
+ // Suppress only if every topic is either the one the user already
+ // typed, or wouldn't be displayed for this query anyway; otherwise
+ // the user may be typing toward another suggestion.
+ const matcher = get_topic_matcher(query);
+ return topics.every((topic) => normalize(topic) === normalized_query || !matcher(topic));
+}
+
export function initialize_topic_edit_typeahead(
form_field: JQuery<HTMLInputElement>,
stream_name: string,
@@ -1588,6 +1617,8 @@ export function initialize_topic_edit_typeahead(
};
return new Typeahead(bootstrap_typeahead_input, {
dropup,
+ helpOnEmptyStrings: () => !stream_data.is_topic_creation_enabled(stream_id),
+ showOnFocus: () => !stream_data.is_topic_creation_enabled(stream_id),
item_html(_query: string): (item: string) => string {
return function (item: string): string {
const is_empty_string_topic = item === "";
@@ -1615,8 +1646,12 @@ export function initialize_topic_edit_typeahead(
}
return sorted;
},
- source(): string[] {
- return topics_seen_for(stream_id);
+ source(query: string): string[] {
+ const topics = topics_seen_for(stream_id);
+ if (should_suppress_topic_typeahead(query, topics)) {
+ return [];
+ }
+ return topics;
},
items: max_num_items,
getCustomItemClassname() {
@@ -1764,13 +1799,19 @@ export function initialize({
};
stream_message_topic_typeahead = new Typeahead(stream_message_typeahead_input, {
dropup: true,
+ helpOnEmptyStrings: () => !stream_data.is_topic_creation_enabled(compose_state.stream_id()),
+ showOnFocus: () => !stream_data.is_topic_creation_enabled(compose_state.stream_id()),
source(query: string): (UserPillData | string)[] {
let people_candidates: UserPillData[] = [];
if (query && query.length > 3) {
people_candidates = get_person_suggestion_for_topic_typeahead(query);
}
const topics = topics_seen_for(compose_state.stream_id());
- return [...people_candidates, ...topics];
+ const candidates = [...people_candidates, ...topics];
+ if (should_suppress_topic_typeahead(query, candidates)) {
+ return [];
+ }
+ return candidates;
},
items: max_num_items,
item_html(_query: string): (item: string | UserPillData) => string {
diff --git a/web/src/stream_data.ts b/web/src/stream_data.ts
index de7eca3b69a..d2ef26937dd 100644
--- a/web/src/stream_data.ts
+++ b/web/src/stream_data.ts
@@ -962,6 +962,15 @@ export function rewire_can_create_new_topics_in_stream(
can_create_new_topics_in_stream = value;
}
+// Returns true when no channel is selected, so callers can pass the
+// compose state's channel id directly.
+export function is_topic_creation_enabled(stream_id: number | undefined): boolean {
+ if (stream_id === undefined) {
+ return true;
+ }
+ return can_create_new_topics_in_stream(stream_id);
+}
+
export function user_can_move_messages_out_of_channel(stream: StreamSubscription): boolean {
if (page_params.is_spectator) {
return false;
diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjs
index 9e506c41eb6..2c62e2b2382 100644
--- a/web/tests/composebox_typeahead.test.cjs
+++ b/web/tests/composebox_typeahead.test.cjs
@@ -842,6 +842,99 @@ test("topics_seen_for", ({override}) => {
assert.deepEqual(ct.topics_seen_for(""), []);
});
+test("should_suppress_topic_typeahead", () => {
+ // An empty query is never suppressed, even when the empty topic (which
+ // renders as a non-empty display name) is among the items.
+ assert.ok(!ct.should_suppress_topic_typeahead("", ["", "design"]));
+
+ // An exact match against an existing topic suppresses the typeahead,
+ // normalizing case and surrounding whitespace.
+ assert.ok(ct.should_suppress_topic_typeahead("design", ["design", "other"]));
+ assert.ok(ct.should_suppress_topic_typeahead(" DESIGN ", ["Design"]));
+
+ // An exact match is not suppressed while another topic still matches
+ // the query, since the user may be typing toward it.
+ assert.ok(!ct.should_suppress_topic_typeahead("design", ["design", "designs"]));
+ assert.ok(!ct.should_suppress_topic_typeahead("design", ["design", "my design doc"]));
+
+ // Another topic keeps the typeahead open only if the typeahead would
+ // actually display it: a multi-word query must match other topics at
+ // a word boundary, not as a mid-word substring ("design office"
+ // contains "sign off" but is not a suggestion for it).
+ assert.ok(!ct.should_suppress_topic_typeahead("sign off", ["sign off", "sign offsite"]));
+ assert.ok(ct.should_suppress_topic_typeahead("sign off", ["sign off", "design office"]));
+
+ // A query that names no existing topic is not suppressed.
+ assert.ok(!ct.should_suppress_topic_typeahead("new topic", ["design"]));
+
+ // People suggestions keep the typeahead open even on an exact match.
+ assert.ok(!ct.should_suppress_topic_typeahead("design", ["design", cordelia_item]));
+});
+
+test("topic typeahead auto-opens when topic creation is disabled", ({
+ override,
+ override_rewire,
+}) => {
+ let topic_edit_config;
+ override(bootstrap_typeahead, "Typeahead", (_input_element, config) => {
+ topic_edit_config = config;
+ });
+
+ let topic_creation_allowed = true;
+ override_rewire(stream_data, "can_create_new_topics_in_stream", () => topic_creation_allowed);
+
+ ct.initialize_topic_edit_typeahead($.create("fake-topic-edit-input"), "Denmark", false);
+
+ // The same typeahead instance reflects the channel's live permission,
+ // so it auto-opens on focus (showOnFocus) and shows for an empty query
+ // (helpOnEmptyStrings) exactly when topic creation is disabled.
+ topic_creation_allowed = false;
+ assert.ok(topic_edit_config.showOnFocus());
+ assert.ok(topic_edit_config.helpOnEmptyStrings());
+
+ topic_creation_allowed = true;
+ assert.ok(!topic_edit_config.showOnFocus());
+ assert.ok(!topic_edit_config.helpOnEmptyStrings());
+
+ // With no channel resolved, topic creation is treated as allowed, so
+ // the typeahead does not auto-open.
+ ct.initialize_topic_edit_typeahead($.create("fake-missing-stream-input"), "Nonexistent", false);
+ topic_creation_allowed = false;
+ assert.ok(!topic_edit_config.showOnFocus());
+ assert.ok(!topic_edit_config.helpOnEmptyStrings());
+});
+
+test("topic typeahead source suppresses an exact-match topic", ({override}) => {
+ let topic_edit_config;
+ override(bootstrap_typeahead, "Typeahead", (_input_element, config) => {
+ topic_edit_config = config;
+ });
+ override(stream_topic_history_util, "get_server_history", noop);
+ // Higher message_ids are more recent, so "design" sorts before
+ // "design docs" in the source's output.
+ for (const [message_id, topic_name] of [
+ [2, "design"],
+ [1, "design docs"],
+ ]) {
+ stream_topic_history.add_message({
+ stream_id: netherland_stream.stream_id,
+ message_id,
+ topic_name,
+ });
+ }
+
+ ct.initialize_topic_edit_typeahead($.create("fake-source-input"), "The Netherlands", false);
+
+ // "design docs" still has "design" as a substring, so an exact match on
+ // "design" keeps the existing topics listed.
+ assert.deepEqual(topic_edit_config.source("design"), ["design", "design docs"]);
+ // Matching the longest topic exactly leaves nothing to suggest, so the
+ // source returns no items and the typeahead stays hidden.
+ assert.deepEqual(topic_edit_config.source("design docs"), []);
+ // A query that names no existing topic lists the topics as usual.
+ assert.deepEqual(topic_edit_config.source("new"), ["design", "design docs"]);
+});
+
test("content_typeahead_selected", ({override}) => {
const input_element = {
$element: {},
diff --git a/web/tests/stream_data.test.cjs b/web/tests/stream_data.test.cjs
index ad9f8cc8896..e4d04674ef0 100644
--- a/web/tests/stream_data.test.cjs
+++ b/web/tests/stream_data.test.cjs
@@ -1561,6 +1561,16 @@ test("can_create_topics_in_stream", ({override}) => {
page_params.is_spectator = false;
sub.is_archived = true;
assert.equal(stream_data.can_create_new_topics_in_stream(sub.stream_id), false);
+
+ // is_topic_creation_enabled tracks can_create_new_topics_in_stream,
+ // and treats "no channel selected" (undefined) as enabled.
+ sub.is_archived = false;
+ sub.can_create_topic_group = admins_group.id;
+ override(current_user, "user_id", admin_user_id);
+ assert.equal(stream_data.is_topic_creation_enabled(sub.stream_id), true);
+ override(current_user, "user_id", moderator_user_id);
+ assert.equal(stream_data.is_topic_creation_enabled(sub.stream_id), false);
+ assert.equal(stream_data.is_topic_creation_enabled(undefined), true);
});
test("can_move_messages_out_of_channel", ({override}) => {