narrow_history: Convert module to typescript.

This commit is contained in:
evykassirer
2023-12-24 15:30:42 -08:00
committed by Anders Kaseorg
parent e0a10c2d94
commit b20694aa07
3 changed files with 11 additions and 7 deletions

View File

@@ -149,7 +149,7 @@ EXEMPT_FILES = make_set(
"web/src/modals.ts", "web/src/modals.ts",
"web/src/muted_users_ui.js", "web/src/muted_users_ui.js",
"web/src/narrow.js", "web/src/narrow.js",
"web/src/narrow_history.js", "web/src/narrow_history.ts",
"web/src/narrow_title.ts", "web/src/narrow_title.ts",
"web/src/navbar_alerts.js", "web/src/navbar_alerts.js",
"web/src/navbar_help_menu.js", "web/src/navbar_help_menu.js",

View File

@@ -3,6 +3,7 @@ import assert from "minimalistic-assert";
import * as blueslip from "./blueslip"; import * as blueslip from "./blueslip";
import * as inbox_util from "./inbox_util"; import * as inbox_util from "./inbox_util";
import type {MessageListData} from "./message_list_data";
import type {Message} from "./message_store"; import type {Message} from "./message_store";
import * as recent_view_util from "./recent_view_util"; import * as recent_view_util from "./recent_view_util";
import * as ui_util from "./ui_util"; import * as ui_util from "./ui_util";
@@ -24,6 +25,7 @@ type MessageList = {
all_messages: () => Message[]; all_messages: () => Message[];
get: (id: number) => Message | undefined; get: (id: number) => Message | undefined;
pre_narrow_offset?: number; pre_narrow_offset?: number;
data: MessageListData;
}; };
export let home: MessageList | undefined; export let home: MessageList | undefined;

View File

@@ -7,8 +7,8 @@ import * as narrow_state from "./narrow_state";
// Saves the selected message of the narrow in the browser // Saves the selected message of the narrow in the browser
// history, so that we are able to restore it if the user // history, so that we are able to restore it if the user
// navigates back to this page. // navigates back to this page.
function _save_narrow_state() { function _save_narrow_state(): void {
if (!narrow_state.active()) { if (!narrow_state.active() || message_lists.current === undefined) {
return; return;
} }
@@ -19,17 +19,19 @@ function _save_narrow_state() {
return; return;
} }
const narrow_data = {};
const narrow_pointer = message_lists.current.selected_id(); const narrow_pointer = message_lists.current.selected_id();
if (narrow_pointer === -1) { if (narrow_pointer === -1) {
return; return;
} }
narrow_data.narrow_pointer = narrow_pointer;
const $narrow_row = message_lists.current.selected_row(); const $narrow_row = message_lists.current.selected_row();
if ($narrow_row.length === 0) { if ($narrow_row.length === 0) {
return; return;
} }
narrow_data.narrow_offset = $narrow_row.get_offset_to_window().top; const narrow_offset = $narrow_row.get_offset_to_window().top;
const narrow_data = {
narrow_pointer,
narrow_offset,
};
history.replaceState(narrow_data, "", window.location.href); history.replaceState(narrow_data, "", window.location.href);
} }
@@ -37,7 +39,7 @@ function _save_narrow_state() {
export const save_narrow_state = _.throttle(_save_narrow_state, 500); export const save_narrow_state = _.throttle(_save_narrow_state, 500);
// This causes the save to happen right away. // This causes the save to happen right away.
export function save_narrow_state_and_flush() { export function save_narrow_state_and_flush(): void {
save_narrow_state(); save_narrow_state();
save_narrow_state.flush(); save_narrow_state.flush();
} }