message_list_view: Calculate date divider variables before assignment.

This is part of a bigger refactor to calculate message container
attributes just from the message, so that we can create the message
container all at once instead of piecewise, to be able to convert
to typescript.
This commit is contained in:
evykassirer 2024-08-25 13:46:20 -07:00 committed by Tim Abbott
parent fa4d5bec2c
commit 3dd0ef6bf9
1 changed files with 29 additions and 13 deletions

View File

@ -142,18 +142,31 @@ function clear_message_date_divider(message_container) {
}
function update_message_date_divider(opts) {
const prev_msg_container = opts.prev_msg_container;
const curr_msg_container = opts.curr_msg_container;
Object.assign(
opts.curr_msg_container,
get_message_date_divider_data({
prev_message: opts.prev_msg_container?.msg,
curr_message: opts.curr_msg_container.msg,
}),
);
}
if (!prev_msg_container || same_day(curr_msg_container?.msg, prev_msg_container?.msg)) {
clear_message_date_divider(curr_msg_container);
return;
function get_message_date_divider_data(opts) {
const prev_message = opts.prev_message;
const curr_message = opts.curr_message;
if (!prev_message || same_day(curr_message, prev_message)) {
return {
want_date_divider: false,
date_divider_html: undefined,
};
}
const curr_time = new Date(curr_message.timestamp * 1000);
const curr_time = new Date(curr_msg_container.msg.timestamp * 1000);
curr_msg_container.want_date_divider = true;
curr_msg_container.date_divider_html = util.the(timerender.render_date(curr_time)).outerHTML;
return {
want_date_divider: true,
date_divider_html: util.the(timerender.render_date(curr_time)).outerHTML,
};
}
function get_timestr(message) {
@ -606,17 +619,20 @@ export class MessageListView {
prev.msg.historical === message_container.msg.historical
) {
add_message_container_to_group(message_container);
update_message_date_divider({
prev_msg_container: prev,
curr_msg_container: message_container,
const date_divider_data = get_message_date_divider_data({
prev_message: prev.msg,
curr_message: message_container.msg,
});
message_container.want_date_divider = date_divider_data.want_date_divider;
message_container.date_divider_html = date_divider_data.date_divider_html;
} else {
finish_group();
current_group = start_group();
add_message_container_to_group(message_container);
update_group_date(current_group, message_container, prev);
clear_message_date_divider(message_container);
message_container.want_date_divider = false;
message_container.date_divider_html = undefined;
message_container.include_recipient = true;
message_container.subscribed = false;