stream_pill: Convert module to TypeScript.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-09-22 09:30:23 -07:00 committed by GitHub
parent f3fc0c8c7a
commit fb765bd057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 10 deletions

View File

@ -1,12 +1,24 @@
import type {InputPillContainer, InputPillItem} from "./input_pill";
import * as peer_data from "./peer_data";
import * as stream_data from "./stream_data";
import type {StreamSubscription} from "./sub_store";
function display_pill(sub) {
type StreamPill = {
stream_id: number;
stream_name: string;
};
type StreamPillWidget = InputPillContainer<StreamPill>;
function display_pill(sub: StreamSubscription): string {
const sub_count = peer_data.get_subscriber_count(sub.stream_id);
return "#" + sub.name + ": " + sub_count + " users";
}
export function create_item_from_stream_name(stream_name, current_items) {
export function create_item_from_stream_name(
stream_name: string,
current_items: InputPillItem<StreamPill>[],
): InputPillItem<StreamPill> | undefined {
stream_name = stream_name.trim();
if (!stream_name.startsWith("#")) {
return undefined;
@ -33,12 +45,12 @@ export function create_item_from_stream_name(stream_name, current_items) {
return item;
}
export function get_stream_name_from_item(item) {
export function get_stream_name_from_item(item: InputPillItem<StreamPill>): string {
return item.stream_name;
}
function get_user_ids_from_subs(items) {
let user_ids = [];
function get_user_ids_from_subs(items: InputPillItem<StreamPill>[]): number[] {
let user_ids: number[] = [];
for (const item of items) {
// only some of our items have streams (for copy-from-stream)
if (item.stream_id !== undefined) {
@ -48,7 +60,7 @@ function get_user_ids_from_subs(items) {
return user_ids;
}
export function get_user_ids(pill_widget) {
export function get_user_ids(pill_widget: StreamPillWidget): number[] {
const items = pill_widget.items();
let user_ids = get_user_ids_from_subs(items);
user_ids = [...new Set(user_ids)];
@ -58,7 +70,7 @@ export function get_user_ids(pill_widget) {
return user_ids;
}
export function append_stream(stream, pill_widget) {
export function append_stream(stream: StreamSubscription, pill_widget: StreamPillWidget): void {
pill_widget.appendValidatedData({
type: "stream",
display_value: display_pill(stream),
@ -68,7 +80,7 @@ export function append_stream(stream, pill_widget) {
pill_widget.clear_text();
}
export function get_stream_ids(pill_widget) {
export function get_stream_ids(pill_widget: StreamPillWidget): number[] {
const items = pill_widget.items();
let stream_ids = items.map((item) => item.stream_id);
stream_ids = stream_ids.filter(Boolean);
@ -76,13 +88,16 @@ export function get_stream_ids(pill_widget) {
return stream_ids;
}
export function filter_taken_streams(items, pill_widget) {
export function filter_taken_streams(
items: StreamSubscription[],
pill_widget: StreamPillWidget,
): StreamSubscription[] {
const taken_stream_ids = get_stream_ids(pill_widget);
items = items.filter((item) => !taken_stream_ids.includes(item.stream_id));
return items;
}
export function typeahead_source(pill_widget) {
export function typeahead_source(pill_widget: StreamPillWidget): StreamSubscription[] {
const potential_streams = stream_data.get_unsorted_subs();
return filter_taken_streams(potential_streams, pill_widget);
}