Files
zulip/web/src/user_group_create_members_data.ts
Anders Kaseorg e96ede0ef6 page_params: Split out state data for current user.
For spectators, the chunk of page_params that originates from
do_events_register isn’t assigned until ui_init.js.  That means the
TypeScript type of page_params is mostly a lie during module load
time: reading a parameter too early silently results in undefined
rather than the declared type, with unpredictable results later on.

We want to make such an early read into an immediate runtime error,
for both users and spectators consistently, and pave the way for
runtime validation of the page_params type.  As a first step, split
out the subset of fields that pertain to the current user.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-02-15 10:22:52 -08:00

51 lines
1.5 KiB
TypeScript

import * as people from "./people";
import type {User} from "./people";
import {current_user} from "./state_data";
let user_id_set: Set<number>;
export function initialize_with_current_user(): void {
user_id_set = new Set([current_user.user_id]);
}
export function sorted_user_ids(): number[] {
const users = people.get_users_from_ids([...user_id_set]);
people.sort_but_pin_current_user_on_top(users);
return users.map((user) => user.user_id);
}
export function get_all_user_ids(): number[] {
const potential_members = people.get_realm_users();
const user_ids = potential_members.map((user) => user.user_id);
// sort for determinism
user_ids.sort((a, b) => a - b);
return user_ids;
}
export function get_principals(): number[] {
// Return list of user ids which were selected by user.
return [...user_id_set];
}
export function get_potential_members(): User[] {
const potential_members = people.get_realm_users();
return potential_members.filter((user) => !user_id_set.has(user.user_id));
}
export function add_user_ids(user_ids: number[]): void {
for (const user_id of user_ids) {
if (!user_id_set.has(user_id)) {
const user = people.maybe_get_user_by_id(user_id);
if (user) {
user_id_set.add(user_id);
}
}
}
}
export function remove_user_ids(user_ids: number[]): void {
for (const user_id of user_ids) {
user_id_set.delete(user_id);
}
}