From c4026615ae7c943377855eae6a5c585031316cc7 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Tue, 24 Sep 2024 14:30:15 +0530 Subject: [PATCH] user_group_pill: Accept setting_type as parameter in typeahead_source. We currently use the pills UI only for can_manage_group setting so it is fine to pass setting type as "group" directly to get_realm_user_groups_for_setting, but it would be better to just accept setting_type as parameter in typeahead_source and pass that to get_realm_user_groups_for_setting as other settings will also use the pills UI in future. --- web/src/group_setting_pill.ts | 1 + web/src/pill_typeahead.ts | 3 ++- web/src/settings_components.ts | 3 +++ web/src/user_group_create.ts | 1 + web/src/user_group_edit.js | 1 + web/src/user_group_pill.ts | 6 +++++- web/tests/pill_typeahead.test.js | 1 + 7 files changed, 14 insertions(+), 2 deletions(-) diff --git a/web/src/group_setting_pill.ts b/web/src/group_setting_pill.ts index 0e6fec6c6f..54551ea754 100644 --- a/web/src/group_setting_pill.ts +++ b/web/src/group_setting_pill.ts @@ -114,6 +114,7 @@ export function set_up_pill_typeahead({ $pill_container: JQuery; opts: { setting_name: string; + setting_type: "realm" | "stream" | "group"; group: UserGroup | undefined; }; }): void { diff --git a/web/src/pill_typeahead.ts b/web/src/pill_typeahead.ts index 7071092756..8f52a0ce86 100644 --- a/web/src/pill_typeahead.ts +++ b/web/src/pill_typeahead.ts @@ -134,6 +134,7 @@ export function set_up_group_setting_typeahead( pills: GroupSettingPillContainer, opts: { setting_name: string; + setting_type: "realm" | "stream" | "group"; group: UserGroup | undefined; }, ): void { @@ -146,7 +147,7 @@ export function set_up_group_setting_typeahead( source(_query: string): GroupSettingTypeaheadItem[] { let source: GroupSettingTypeaheadItem[] = []; - source = user_group_pill.typeahead_source(pills, opts.setting_name); + source = user_group_pill.typeahead_source(pills, opts.setting_name, opts.setting_type); source = [...source, ...user_pill.typeahead_source(pills, true)]; return source; diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts index 752d54c67f..323dd7e3bf 100644 --- a/web/src/settings_components.ts +++ b/web/src/settings_components.ts @@ -1444,15 +1444,18 @@ type group_setting_name = "can_manage_group"; export function create_group_setting_widget({ $pill_container, setting_name, + setting_type, group, }: { $pill_container: JQuery; setting_name: group_setting_name; + setting_type: "realm" | "stream" | "group"; group?: UserGroup; }): void { const pill_widget = group_setting_pill.create_pills($pill_container, setting_name); const opts = { setting_name, + setting_type, group, }; group_setting_pill.set_up_pill_typeahead({pill_widget, $pill_container, opts}); diff --git a/web/src/user_group_create.ts b/web/src/user_group_create.ts index 6f7b30cf9c..b5fedf1e9a 100644 --- a/web/src/user_group_create.ts +++ b/web/src/user_group_create.ts @@ -243,6 +243,7 @@ export function set_up_handlers(): void { settings_components.create_group_setting_widget({ $pill_container, setting_name: "can_manage_group", + setting_type: "group", }); user_group_components.setup_permissions_dropdown("can_mention_group", undefined, true); diff --git a/web/src/user_group_edit.js b/web/src/user_group_edit.js index 4b8bf4079d..e4bc91d231 100644 --- a/web/src/user_group_edit.js +++ b/web/src/user_group_edit.js @@ -172,6 +172,7 @@ function show_general_settings(group) { settings_components.create_group_setting_widget({ $pill_container, setting_name: "can_manage_group", + setting_type: "group", group, }); update_general_panel_ui(group); diff --git a/web/src/user_group_pill.ts b/web/src/user_group_pill.ts index 0ffead9d2e..5eb580b853 100644 --- a/web/src/user_group_pill.ts +++ b/web/src/user_group_pill.ts @@ -1,3 +1,5 @@ +import assert from "minimalistic-assert"; + import {$t_html} from "./i18n"; import type {InputPillContainer} from "./input_pill"; import * as people from "./people"; @@ -110,10 +112,12 @@ export function filter_taken_groups( export function typeahead_source( pill_widget: CombinedPillContainer | GroupSettingPillContainer, setting_name?: string, + setting_type?: "realm" | "stream" | "group", ): UserGroupPillData[] { let groups; if (setting_name !== undefined) { - groups = user_groups.get_realm_user_groups_for_setting(setting_name, "group", true); + assert(setting_type !== undefined); + groups = user_groups.get_realm_user_groups_for_setting(setting_name, setting_type, true); } else { groups = user_groups.get_realm_user_groups(); } diff --git a/web/tests/pill_typeahead.test.js b/web/tests/pill_typeahead.test.js index 82b5065d3b..16864f2b1f 100644 --- a/web/tests/pill_typeahead.test.js +++ b/web/tests/pill_typeahead.test.js @@ -708,6 +708,7 @@ run_test("set_up_group_setting_typeahead", ({mock_template, override, override_r const opts = { setting_name: "can_manage_group", + setting_type: "group", group: testers, }; pill_typeahead.set_up_group_setting_typeahead($fake_input, $pill_widget, opts);