settings_components: Generate RealmSettingProperty type from a schema.

This will be useful for later when we'll need the schema for
converting settings_org to typescript.

This commit also renames RealmSettingProperty to more accurately
reflect the type of a single property key.
This commit is contained in:
evykassirer
2024-10-08 16:42:00 -07:00
committed by Tim Abbott
parent 69db3c422a
commit 67f4866201
2 changed files with 10 additions and 6 deletions

View File

@@ -24,7 +24,7 @@ import * as scroll_util from "./scroll_util";
import * as settings_config from "./settings_config"; import * as settings_config from "./settings_config";
import * as settings_data from "./settings_data"; import * as settings_data from "./settings_data";
import type {CustomProfileField, GroupSettingValue} from "./state_data"; import type {CustomProfileField, GroupSettingValue} from "./state_data";
import {current_user, realm} from "./state_data"; import {current_user, realm, realm_schema} from "./state_data";
import * as stream_data from "./stream_data"; import * as stream_data from "./stream_data";
import type {StreamSubscription} from "./sub_store"; import type {StreamSubscription} from "./sub_store";
import type {GroupSettingPillContainer} from "./typeahead_helper"; import type {GroupSettingPillContainer} from "./typeahead_helper";
@@ -96,7 +96,11 @@ export function get_realm_time_limits_in_minutes(property: MessageTimeLimitSetti
} }
type RealmSetting = typeof realm; type RealmSetting = typeof realm;
type RealmSettingProperties = keyof RealmSetting | "realm_org_join_restrictions"; export const realm_setting_property_schema = z.union([
realm_schema.keyof(),
z.literal("realm_org_join_restrictions"),
]);
type RealmSettingProperty = z.infer<typeof realm_setting_property_schema>;
type RealmUserSettingDefaultType = typeof realm_user_settings_defaults; type RealmUserSettingDefaultType = typeof realm_user_settings_defaults;
type RealmUserSettingDefaultProperties = type RealmUserSettingDefaultProperties =
@@ -108,7 +112,7 @@ type StreamSettingProperties = keyof StreamSubscription | "stream_privacy" | "is
type valueof<T> = T[keyof T]; type valueof<T> = T[keyof T];
export function get_realm_settings_property_value( export function get_realm_settings_property_value(
property_name: RealmSettingProperties, property_name: RealmSettingProperty,
): valueof<RealmSetting> { ): valueof<RealmSetting> {
if (property_name === "realm_org_join_restrictions") { if (property_name === "realm_org_join_restrictions") {
if (realm.realm_emails_restricted_to_domains) { if (realm.realm_emails_restricted_to_domains) {
@@ -794,7 +798,7 @@ export function check_realm_settings_property_changed(elem: HTMLElement): boolea
const $elem = $(elem); const $elem = $(elem);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const property_name = extract_property_name($elem) as RealmSettingProperties; const property_name = extract_property_name($elem) as RealmSettingProperty;
const current_val = get_realm_settings_property_value(property_name); const current_val = get_realm_settings_property_value(property_name);
let proposed_val; let proposed_val;
switch (property_name) { switch (property_name) {
@@ -1063,7 +1067,7 @@ export function populate_data_for_realm_settings_request(
if (realm_group_settings_using_new_api_format.has(property_name)) { if (realm_group_settings_using_new_api_format.has(property_name)) {
const old_value = get_realm_settings_property_value( const old_value = get_realm_settings_property_value(
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
("realm_" + property_name) as RealmSettingProperties, ("realm_" + property_name) as RealmSettingProperty,
); );
data[property_name] = JSON.stringify({ data[property_name] = JSON.stringify({
new: input_value, new: input_value,

View File

@@ -250,7 +250,7 @@ const custom_profile_field_types_schema = z.object({
export type CustomProfileFieldTypes = z.infer<typeof custom_profile_field_types_schema>; export type CustomProfileFieldTypes = z.infer<typeof custom_profile_field_types_schema>;
// Sync this with zerver.lib.events.do_events_register. // Sync this with zerver.lib.events.do_events_register.
const realm_schema = z.object({ export const realm_schema = z.object({
custom_profile_fields: z.array(custom_profile_field_schema), custom_profile_fields: z.array(custom_profile_field_schema),
custom_profile_field_types: custom_profile_field_types_schema, custom_profile_field_types: custom_profile_field_types_schema,
demo_organization_scheduled_deletion_date: z.optional(z.number()), demo_organization_scheduled_deletion_date: z.optional(z.number()),