From e459b83c060547bdc3c2787dd5f0b6e749a90a77 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 2 Oct 2023 15:57:17 -0700 Subject: [PATCH] pm_list_dom: Convert module to TypeScript. Signed-off-by: Anders Kaseorg --- tools/test-js-with-node | 2 +- web/src/{pm_list_dom.js => pm_list_dom.ts} | 33 ++++++++++++++++------ web/src/vdom.ts | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) rename web/src/{pm_list_dom.js => pm_list_dom.ts} (50%) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 883f21763f..a142e417f9 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -142,7 +142,7 @@ EXEMPT_FILES = make_set( "web/src/playground_links_popover.js", "web/src/plotly.js.d.ts", "web/src/pm_list.js", - "web/src/pm_list_dom.js", + "web/src/pm_list_dom.ts", "web/src/poll_widget.js", "web/src/popover_menus.js", "web/src/popover_menus_data.js", diff --git a/web/src/pm_list_dom.js b/web/src/pm_list_dom.ts similarity index 50% rename from web/src/pm_list_dom.js rename to web/src/pm_list_dom.ts index f633b74f15..d885c8c7d5 100644 --- a/web/src/pm_list_dom.js +++ b/web/src/pm_list_dom.ts @@ -5,10 +5,19 @@ import render_pm_list_item from "../templates/pm_list_item.hbs"; import * as vdom from "./vdom"; -export function keyed_pm_li(conversation) { - const render = () => render_pm_list_item(conversation); +// TODO/typescript: Move this to pm_list_data +type PMListConversation = { + user_ids_string: string; +}; - const eq = (other) => _.isEqual(conversation, other.conversation); +type PMNode = vdom.Node & { + conversation: PMListConversation; +}; + +export function keyed_pm_li(conversation: PMListConversation): PMNode { + const render = (): string => render_pm_list_item(conversation); + + const eq = (other: PMNode): boolean => _.isEqual(conversation, other.conversation); const key = conversation.user_ids_string; @@ -20,12 +29,20 @@ export function keyed_pm_li(conversation) { }; } -export function more_private_conversations_li(more_conversations_unread_count) { - const render = () => render_more_private_conversations({more_conversations_unread_count}); +type MorePrivateConversationsNode = vdom.Node & { + more_items: boolean; + more_conversations_unread_count: number; +}; + +export function more_private_conversations_li( + more_conversations_unread_count: number, +): MorePrivateConversationsNode { + const render = (): string => + render_more_private_conversations({more_conversations_unread_count}); // Used in vdom.js to check if an element has changed and needs to // be updated in the DOM. - const eq = (other) => + const eq = (other: MorePrivateConversationsNode): boolean => other.more_items && more_conversations_unread_count === other.more_conversations_unread_count; @@ -41,8 +58,8 @@ export function more_private_conversations_li(more_conversations_unread_count) { }; } -export function pm_ul(nodes) { - const attrs = [ +export function pm_ul(nodes: vdom.Node[]): vdom.Tag { + const attrs: [string, string][] = [ ["class", "pm-list"], ["data-name", "private"], ]; diff --git a/web/src/vdom.ts b/web/src/vdom.ts index 2ee174b10f..160e188593 100644 --- a/web/src/vdom.ts +++ b/web/src/vdom.ts @@ -151,7 +151,7 @@ export function update( like `attrs` for attributes. For examples of creating vdom objects, look at - `pm_list_dom.js`. + `pm_list_dom.ts`. */ function do_full_update(): void { const rendered_dom = render_tag(new_dom);