compose-closed-ui: Clarify object and type for reply to button.

Renames ComposeClosedMessage to ReplyRecipientInformation, and
exports the type from compose_closed_ui.ts so that the functions
that construct these objects from the recent conversations and
inbox views have the type available.

Also, renames the variables for these objects to not be "message",
so that it's clear that these are not Message objects.

(cherry picked from commit b48134a03e)
This commit is contained in:
Lauryn Menard
2025-03-24 18:24:08 +01:00
committed by Tim Abbott
parent 1345944688
commit 2efef3a0e6
3 changed files with 30 additions and 29 deletions

View File

@@ -33,13 +33,15 @@ function get_stream_recipient_label(stream_id: number, topic: string): Recipient
return undefined;
}
type ComposeClosedMessage = {
export type ReplyRecipientInformation = {
stream_id?: number | undefined;
topic?: string;
topic?: string | undefined;
display_reply_to?: string | undefined;
};
export function get_recipient_label(message?: ComposeClosedMessage): RecipientLabel | undefined {
export function get_recipient_label(
recipient_information?: ReplyRecipientInformation,
): RecipientLabel | undefined {
// TODO: This code path is bit of a type-checking disaster; we mix
// actual message objects with fake objects containing just a
// couple fields, both those constructed here and potentially
@@ -48,7 +50,7 @@ export function get_recipient_label(message?: ComposeClosedMessage): RecipientLa
return undefined;
}
if (message === undefined) {
if (recipient_information === undefined) {
if (message_lists.current.visibly_empty()) {
// For empty narrows where there's a clear reply target,
// i.e. stream+topic or a single direct message conversation,
@@ -62,15 +64,21 @@ export function get_recipient_label(message?: ComposeClosedMessage): RecipientLa
return {label_text: message_store.get_pm_full_names(user_ids)};
}
} else {
message = message_lists.current.selected_message();
recipient_information = message_lists.current.selected_message();
}
}
if (message) {
if (message.stream_id !== undefined && message.topic !== undefined) {
return get_stream_recipient_label(message.stream_id, message.topic);
} else if (message.display_reply_to) {
return {label_text: message.display_reply_to};
if (recipient_information) {
if (
recipient_information.stream_id !== undefined &&
recipient_information.topic !== undefined
) {
return get_stream_recipient_label(
recipient_information.stream_id,
recipient_information.topic,
);
} else if (recipient_information.display_reply_to) {
return {label_text: recipient_information.display_reply_to};
}
}
return undefined;
@@ -171,8 +179,10 @@ export function set_standard_text_for_reply_button(): void {
set_reply_button_label($t({defaultMessage: "Compose message"}));
}
export function update_recipient_text_for_reply_button(message?: ComposeClosedMessage): void {
const recipient_label = get_recipient_label(message);
export function update_recipient_text_for_reply_button(
recipient_information?: ReplyRecipientInformation,
): void {
const recipient_label = get_recipient_label(recipient_information);
if (recipient_label !== undefined) {
const empty_string_topic_display_name = util.get_final_topic_display_name("");
const rendered_recipient_label = render_reply_recipient_label({

View File

@@ -875,30 +875,25 @@ export function revive_current_focus(): void {
}
function update_closed_compose_text($row: JQuery, is_header_row: boolean): void {
// TODO: This fake "message" object is designed to allow using the
// get_recipient_label helper inside compose_closed_ui. Surely
// there's a more readable way to write this code.
// Similar code is present in recent view.
if (is_header_row) {
compose_closed_ui.set_standard_text_for_reply_button();
return;
}
let message;
let reply_recipient_information: compose_closed_ui.ReplyRecipientInformation;
const is_dm = $row.parent("#inbox-direct-messages-container").length > 0;
if (is_dm) {
message = {
reply_recipient_information = {
display_reply_to: $row.find(".recipients_name").text(),
};
} else {
const $stream = $row.parent(".inbox-topic-container").prev(".inbox-header");
message = {
reply_recipient_information = {
stream_id: Number($stream.attr("data-stream-id")),
topic: $row.find(".inbox-topic-name a").text(),
};
}
compose_closed_ui.update_recipient_text_for_reply_button(message);
compose_closed_ui.update_recipient_text_for_reply_button(reply_recipient_information);
}
export function get_focused_row_message(): {message?: Message | undefined} & (

View File

@@ -340,24 +340,20 @@ function set_table_focus(row: number, col: number, using_keyboard = false): bool
}
}
// TODO: This fake "message" object is designed to allow using the
// get_recipient_label helper inside compose_closed_ui. Surely
// there's a more readable way to write this code.
// Similar code is present in Inbox.
let message;
let reply_recipient_information: compose_closed_ui.ReplyRecipientInformation;
if (type === "private") {
message = {
reply_recipient_information = {
display_reply_to: $topic_row.find(".recent_topic_name a").text(),
};
} else {
const stream_name = $topic_row.find(".recent_topic_stream a").text();
const stream = stream_data.get_sub_by_name(stream_name);
message = {
reply_recipient_information = {
stream_id: stream?.stream_id,
topic: $topic_row.find(".recent_topic_name a").text(),
};
}
compose_closed_ui.update_recipient_text_for_reply_button(message);
compose_closed_ui.update_recipient_text_for_reply_button(reply_recipient_information);
return true;
}