From 7db6de340ff45bfaef158640c7ab55f20a320770 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Fri, 11 Oct 2024 16:36:58 +0530 Subject: [PATCH] settings: Clean schemas and types used for group settings. Removed "type"/"Type" from schema and type names used for group settings because we want to use "type"/"Type" only for "type of a type" cases. So, this commit renames- - anonymous_group_type => anonymous_group_schema - group_setting_type_schema => group_setting_value_schema - GroupSettingType => GroupSettingValue We also had duplicate definitions for GroupSettingType, in state_data.ts and in settings_components.ts. So, removed it from settings_components.ts. --- web/src/settings_components.ts | 10 ++++------ web/src/settings_data.ts | 4 ++-- web/src/settings_notifications.ts | 2 +- web/src/state_data.ts | 10 +++++----- web/src/user_groups.ts | 4 ++-- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts index de6ff14ded..029a56b5e8 100644 --- a/web/src/settings_components.ts +++ b/web/src/settings_components.ts @@ -23,7 +23,7 @@ import {realm_user_settings_defaults} from "./realm_user_settings_defaults"; import * as scroll_util from "./scroll_util"; import * as settings_config from "./settings_config"; import * as settings_data from "./settings_data"; -import type {CustomProfileField, group_setting_type_schema} from "./state_data"; +import type {CustomProfileField, GroupSettingValue} from "./state_data"; import {current_user, realm} from "./state_data"; import * as stream_data from "./stream_data"; import type {StreamSubscription} from "./sub_store"; @@ -651,7 +651,7 @@ function get_input_type($input_elem: JQuery, input_type?: string): string { export function get_input_element_value( input_elem: HTMLElement, input_type?: string, -): boolean | number | string | null | undefined | GroupSettingType { +): boolean | number | string | null | undefined | GroupSettingValue { const $input_elem = $(input_elem); input_type = get_input_type($input_elem, input_type); let input_value; @@ -882,7 +882,7 @@ export function check_stream_settings_property_changed( export function get_group_setting_widget_value( pill_widget: GroupSettingPillContainer, -): GroupSettingType { +): GroupSettingValue { const setting_pills = pill_widget.items(); const direct_subgroups: number[] = []; const direct_members: number[] = []; @@ -1422,7 +1422,7 @@ export function get_group_setting_widget(setting_name: string): GroupSettingPill export function set_group_setting_widget_value( pill_widget: GroupSettingPillContainer, - property_value: GroupSettingType, + property_value: GroupSettingValue, ): void { pill_widget.clear(); @@ -1447,8 +1447,6 @@ export function set_group_setting_widget_value( } } -export type GroupSettingType = z.output; - type group_setting_name = "can_manage_group" | "can_join_group"; export function create_group_setting_widget({ diff --git a/web/src/settings_data.ts b/web/src/settings_data.ts index 220fb9a48c..64e7d89741 100644 --- a/web/src/settings_data.ts +++ b/web/src/settings_data.ts @@ -4,7 +4,7 @@ import * as group_permission_settings from "./group_permission_settings"; import {page_params} from "./page_params"; import * as settings_config from "./settings_config"; import {current_user, realm} from "./state_data"; -import type {GroupSettingType} from "./state_data"; +import type {GroupSettingValue} from "./state_data"; import * as user_groups from "./user_groups"; import {user_settings} from "./user_settings"; @@ -110,7 +110,7 @@ function user_has_permission(policy_value: number): boolean { } export function user_has_permission_for_group_setting( - setting_group_id: GroupSettingType, + setting_group_id: GroupSettingValue, setting_name: string, setting_type: "realm" | "stream" | "group", ): boolean { diff --git a/web/src/settings_notifications.ts b/web/src/settings_notifications.ts index d331f9076e..c9475c3003 100644 --- a/web/src/settings_notifications.ts +++ b/web/src/settings_notifications.ts @@ -283,7 +283,7 @@ export function set_up(settings_panel: SettingsPanel): void { return; } - // This filters out the GroupSettingType + // This filters out the GroupSettingValue const setting_value = z .union([z.string(), z.number(), z.boolean()]) .parse(settings_components.get_input_element_value(this)); diff --git a/web/src/state_data.ts b/web/src/state_data.ts index 4e5bee748d..9ad8b3a7a1 100644 --- a/web/src/state_data.ts +++ b/web/src/state_data.ts @@ -127,14 +127,14 @@ export const server_emoji_schema = z.object({ export const realm_emoji_map_schema = z.record(server_emoji_schema); -export const anonymous_group_type = z.object({ +export const anonymous_group_schema = z.object({ direct_subgroups: z.array(z.number()), direct_members: z.array(z.number()), }); -export const group_setting_type_schema = z.union([z.number(), anonymous_group_type]); +export const group_setting_value_schema = z.union([z.number(), anonymous_group_schema]); -export type GroupSettingType = z.infer; +export type GroupSettingValue = z.infer; export const user_group_schema = z.object({ description: z.string(), @@ -145,8 +145,8 @@ export const user_group_schema = z.object({ members: z.array(z.number()), is_system_group: z.boolean(), direct_subgroup_ids: z.array(z.number()), - can_join_group: group_setting_type_schema, - can_manage_group: group_setting_type_schema, + can_join_group: group_setting_value_schema, + can_manage_group: group_setting_value_schema, can_mention_group: z.number(), deactivated: z.boolean(), }); diff --git a/web/src/user_groups.ts b/web/src/user_groups.ts index 4e573a49b1..e12be17fd6 100644 --- a/web/src/user_groups.ts +++ b/web/src/user_groups.ts @@ -9,7 +9,7 @@ import {page_params} from "./page_params"; import * as settings_config from "./settings_config"; import type { GroupPermissionSetting, - GroupSettingType, + GroupSettingValue, StateData, user_group_schema, } from "./state_data"; @@ -325,7 +325,7 @@ export function is_user_in_group(user_group_id: number, user_id: number): boolea } export function is_user_in_setting_group( - setting_group: GroupSettingType, + setting_group: GroupSettingValue, user_id: number, ): boolean { if (typeof setting_group === "number") {