scheduled_messages: Convert module to TypeScript.

This commit is contained in:
Varun Singh 2024-01-10 21:43:59 +05:30 committed by Tim Abbott
parent ac42de07eb
commit c0a0c0e834
2 changed files with 47 additions and 20 deletions

View File

@ -185,7 +185,7 @@ EXEMPT_FILES = make_set(
"web/src/resize.js",
"web/src/resize_handler.js",
"web/src/rows.ts",
"web/src/scheduled_messages.js",
"web/src/scheduled_messages.ts",
"web/src/scheduled_messages_feed_ui.js",
"web/src/scheduled_messages_overlay_ui.js",
"web/src/scheduled_messages_popover.js",

View File

@ -2,15 +2,35 @@ import * as channel from "./channel";
import {$t} from "./i18n";
import * as timerender from "./timerender";
type ScheduledMessage = {
scheduled_message_id: number;
type: string;
to: number[] | number;
topic: string;
content: string;
rendered_content: string;
scheduled_delivery_timestamp: number;
failed: boolean;
};
type TimeKey =
| "today_nine_am"
| "today_four_pm"
| "tomorrow_nine_am"
| "tomorrow_four_pm"
| "monday_nine_am";
type SendOption = {[key in TimeKey]?: {text: string; stamp: number}};
export const MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS = 5 * 60;
// scheduled_messages_data is a dictionary where key=scheduled_message_id and value=scheduled_messages
export const scheduled_messages_data = new Map();
// scheduled_messages_data is a map where key=scheduled_message_id and value=scheduled_messages
export const scheduled_messages_data = new Map<number, ScheduledMessage>();
let selected_send_later_timestamp;
let selected_send_later_timestamp: number | undefined;
function compute_send_times(now = new Date()) {
const send_times = {};
function compute_send_times(now = new Date()): Record<TimeKey, number> {
const send_times: Record<string, number> = {};
const today = new Date(now);
const tomorrow = new Date(new Date(now).setDate(now.getDate() + 1));
@ -33,19 +53,19 @@ function compute_send_times(now = new Date()) {
return send_times;
}
export function add_scheduled_messages(scheduled_messages) {
export function add_scheduled_messages(scheduled_messages: ScheduledMessage[]): void {
for (const scheduled_message of scheduled_messages) {
scheduled_messages_data.set(scheduled_message.scheduled_message_id, scheduled_message);
}
}
export function remove_scheduled_message(scheduled_message_id) {
export function remove_scheduled_message(scheduled_message_id: number): void {
if (scheduled_messages_data.has(scheduled_message_id)) {
scheduled_messages_data.delete(scheduled_message_id);
}
}
export function update_scheduled_message(scheduled_message) {
export function update_scheduled_message(scheduled_message: ScheduledMessage): void {
if (!scheduled_messages_data.has(scheduled_message.scheduled_message_id)) {
return;
}
@ -53,18 +73,23 @@ export function update_scheduled_message(scheduled_message) {
scheduled_messages_data.set(scheduled_message.scheduled_message_id, scheduled_message);
}
export function delete_scheduled_message(scheduled_msg_id, success = () => {}) {
channel.del({
export function delete_scheduled_message(scheduled_msg_id: number, success?: () => void): void {
void channel.del({
url: "/json/scheduled_messages/" + scheduled_msg_id,
success,
});
}
export function get_count() {
export function get_count(): number {
return scheduled_messages_data.size;
}
export function get_filtered_send_opts(date) {
export function get_filtered_send_opts(date: Date): {
possible_send_later_today: SendOption | false;
send_later_tomorrow: SendOption;
possible_send_later_monday: SendOption | false;
send_later_custom: {text: string};
} {
const send_times = compute_send_times(date);
const day = date.getDay(); // Starts with 0 for Sunday.
@ -142,8 +167,8 @@ export function get_filtered_send_opts(date) {
text: $t({defaultMessage: "Custom"}),
};
let possible_send_later_today = {};
let possible_send_later_monday = {};
let possible_send_later_today: SendOption | false = {};
let possible_send_later_monday: SendOption | false = {};
const minutes_into_day = date.getHours() * 60 + date.getMinutes();
// Show Today send options based on time of day
@ -171,28 +196,30 @@ export function get_filtered_send_opts(date) {
};
}
export function get_selected_send_later_timestamp() {
export function get_selected_send_later_timestamp(): number | undefined {
if (!selected_send_later_timestamp) {
return undefined;
}
return selected_send_later_timestamp;
}
export function get_formatted_selected_send_later_time() {
export function get_formatted_selected_send_later_time(): string | undefined {
if (!selected_send_later_timestamp) {
return undefined;
}
return timerender.get_full_datetime(new Date(selected_send_later_timestamp * 1000), "time");
}
export function set_selected_schedule_timestamp(timestamp) {
export function set_selected_schedule_timestamp(timestamp: number): void {
selected_send_later_timestamp = timestamp;
}
export function reset_selected_schedule_timestamp() {
export function reset_selected_schedule_timestamp(): void {
selected_send_later_timestamp = undefined;
}
export function initialize(scheduled_messages_params) {
export function initialize(scheduled_messages_params: {
scheduled_messages: ScheduledMessage[];
}): void {
add_scheduled_messages(scheduled_messages_params.scheduled_messages);
}