Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

scroll_to_bottom_button: Go to next unread conversation at the bottom.

Aman Agrawalad8c492ebce3
diff --git a/starlight_help/src/content/include/_ReadingConversations.mdx b/starlight_help/src/content/include/_ReadingConversations.mdx
index 9a895eb65d7..2296e7fda96 100644
--- a/starlight_help/src/content/include/_ReadingConversations.mdx
+++ b/starlight_help/src/content/include/_ReadingConversations.mdx
@@ -9,6 +9,7 @@ import LeftSidebarConversations from "../include/_LeftSidebarConversations.mdx";
import RecentConversations from "../include/_RecentConversations.mdx";
import ChevronDownIcon from "~icons/fa/chevron-down";
+import ArrowRightIcon from "~icons/zulip-icon/arrow-right";
<ConversationDefinition />
@@ -40,6 +41,10 @@ import ChevronDownIcon from "~icons/fa/chevron-down";
[mark all messages as read](/help/marking-messages-as-read) by
jumping to the bottom with the **Scroll to bottom**
(<ChevronDownIcon />) button or the <kbd>End</kbd> shortcut.
+ 1. *(optional)* When you reach the bottom of the conversation, click
+ the **Go to next unread topic** or
+ **Go to next unread direct message** (<ArrowRightIcon />) button,
+ if shown, to move on to the next unread conversation.
</Steps>
<KeyboardTip>
diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.ts
index e2028f6f429..2f6cfff44de 100644
--- a/web/src/message_fetch.ts
+++ b/web/src/message_fetch.ts
@@ -499,6 +499,9 @@ export function load_messages(opts: MessageFetchOptions, attempt = 1): void {
// for many common errors, and those have nicer HTML formatting,
// we certainly don't for every possible 400 error.
message_feed_loading.hide_indicators();
+ if (opts.msg_list?.is_current_message_list()) {
+ scroll_to_bottom_button.hide_after_failed_fetch();
+ }
if (
message_lists.current !== undefined &&
diff --git a/web/src/message_scroll.ts b/web/src/message_scroll.ts
index f38645974ee..9291d82145d 100644
--- a/web/src/message_scroll.ts
+++ b/web/src/message_scroll.ts
@@ -79,7 +79,7 @@ function scroll_finish(): void {
// Don't present the "scroll to bottom" widget if the current
// scroll was triggered by the keyboard.
if (!message_scroll_state.keyboard_triggered_current_scroll) {
- scroll_to_bottom_button.show_scroll_to_bottom_button();
+ scroll_to_bottom_button.handle_non_keyboard_scroll();
}
message_scroll_state.set_keyboard_triggered_current_scroll(false);
diff --git a/web/src/message_view.ts b/web/src/message_view.ts
index 1ff25c8ed1f..298ea10129f 100644
--- a/web/src/message_view.ts
+++ b/web/src/message_view.ts
@@ -163,9 +163,12 @@ type TargetMessageIdInfo = {
export function preserves_topics_kept_unread_by_user(trigger: string | undefined): boolean {
return (
trigger !== undefined &&
- ["next_topic_unread_hotkey", "old_unreads_missing", "retarget message location"].includes(
- trigger,
- )
+ [
+ "next_topic_unread_hotkey",
+ "next_unread_topic_button",
+ "old_unreads_missing",
+ "retarget message location",
+ ].includes(trigger)
);
}
diff --git a/web/src/message_viewport.ts b/web/src/message_viewport.ts
index 3ef46a8bd2e..0eb373b8a7c 100644
--- a/web/src/message_viewport.ts
+++ b/web/src/message_viewport.ts
@@ -438,6 +438,14 @@ export function stop_auto_scrolling(): void {
}
}
+export function is_animating_scroll(): boolean {
+ return $scroll_container.promise().state() === "pending";
+}
+
+export function after_animated_scroll(callback: () => void): void {
+ void $scroll_container.promise().done(callback);
+}
+
export function system_initiated_animate_scroll(
scroll_amount: number,
update_selection_on_scroll = false,
diff --git a/web/src/scroll_to_bottom_button.ts b/web/src/scroll_to_bottom_button.ts
index b60ec2905e3..43172e2df8a 100644
--- a/web/src/scroll_to_bottom_button.ts
+++ b/web/src/scroll_to_bottom_button.ts
@@ -6,9 +6,33 @@ import * as message_lists from "./message_lists.ts";
import * as message_scroll_state from "./message_scroll_state.ts";
import * as message_viewport from "./message_viewport.ts";
import * as narrow_state from "./narrow_state.ts";
+import * as stream_list from "./stream_list.ts";
+import * as tippyjs from "./tippyjs.ts";
+import * as topic_generator from "./topic_generator.ts";
+import * as ui_util from "./ui_util.ts";
import {the} from "./util.ts";
+type ButtonMode = "scroll_to_bottom" | "next_unread_topic" | "next_unread_dm_conversation";
+
+const button_mode_config: Record<ButtonMode, {icon: string; tooltip_template_id: string}> = {
+ scroll_to_bottom: {
+ icon: "chevron-down",
+ tooltip_template_id: "scroll-to-bottom-button-tooltip-template",
+ },
+ next_unread_topic: {
+ icon: "arrow-right",
+ tooltip_template_id: "next-unread-topic-button-tooltip-template",
+ },
+ next_unread_dm_conversation: {
+ icon: "arrow-right",
+ tooltip_template_id: "next-unread-dm-conversation-button-tooltip-template",
+ },
+};
+
+let mode: ButtonMode = "scroll_to_bottom";
let hide_timer: ReturnType<typeof setInterval> | undefined;
+let scrolled_away_from_bottom_of_list_id: number | undefined;
+let waiting_for_animated_scroll = false;
function above_bottom_of_view(): boolean {
assert(message_lists.current !== undefined);
@@ -22,6 +46,29 @@ function above_bottom_of_view(): boolean {
);
}
+function get_tooltip(): tippy.Instance | undefined {
+ return the($<tippy.ReferenceElement>("#scroll-to-bottom-button-clickable-area"))._tippy;
+}
+
+function set_mode(new_mode: ButtonMode): void {
+ if (mode === new_mode) {
+ return;
+ }
+
+ const $icon = $("#scroll-to-bottom-button .zulip-icon");
+ $icon.removeClass(`zulip-icon-${button_mode_config[mode].icon}`);
+ $icon.addClass(`zulip-icon-${button_mode_config[new_mode].icon}`);
+ const $clickable_area = $("#scroll-to-bottom-button-clickable-area");
+ $clickable_area.attr(
+ "data-tooltip-template-id",
+ button_mode_config[new_mode].tooltip_template_id,
+ );
+ // A tooltip that is already open would otherwise keep describing
+ // the old mode.
+ get_tooltip()?.setContent(tippyjs.get_tooltip_content(the($clickable_area)));
+ mode = new_mode;
+}
+
function stop_hide_timer(): void {
clearInterval(hide_timer);
hide_timer = undefined;
@@ -32,7 +79,8 @@ function hide(): void {
$("#scroll-to-bottom-button-container").removeClass("show");
}
-function show(): void {
+function show(new_mode: ButtonMode): void {
+ set_mode(new_mode);
stop_hide_timer();
$("#scroll-to-bottom-button-container").addClass("show");
}
@@ -41,6 +89,10 @@ function is_shown(): boolean {
return $("#scroll-to-bottom-button-container").hasClass("show");
}
+function showing_next_unread_conversation(): boolean {
+ return is_shown() && mode !== "scroll_to_bottom";
+}
+
function mouse_is_over_button(): boolean {
return the($("#scroll-to-bottom-button-container")).matches(":hover");
}
@@ -58,6 +110,34 @@ function hide_once_not_hovered(): void {
}, 3000);
}
+function next_unread_conversation_mode(): ButtonMode | undefined {
+ assert(message_lists.current !== undefined);
+ const filter = message_lists.current.data.filter;
+ if (filter.can_show_next_unread_topic_conversation_button()) {
+ const has_next_unread_topic = topic_generator.has_next_unread_topic(
+ narrow_state.stream_id(),
+ narrow_state.topic(),
+ stream_list.get_sorted_channel_ids_for_next_unread_navigation(),
+ );
+ return has_next_unread_topic ? "next_unread_topic" : undefined;
+ }
+ if (filter.can_show_next_unread_dm_conversation_button()) {
+ const has_next_unread_dm_conversation =
+ topic_generator.get_next_unread_pm_string(narrow_state.pm_ids_string()) !== undefined;
+ return has_next_unread_dm_conversation ? "next_unread_dm_conversation" : undefined;
+ }
+ return undefined;
+}
+
+function show_next_unread_conversation_or_hide(): void {
+ const next_mode = next_unread_conversation_mode();
+ if (next_mode === undefined) {
+ hide();
+ } else {
+ show(next_mode);
+ }
+}
+
// Safe to call whenever the scroll position or the message feed may
// have changed.
export function update(): void {
@@ -73,25 +153,75 @@ export function update(): void {
return;
}
- if (!above_bottom_of_view()) {
- hide();
+ if (message_viewport.is_animating_scroll()) {
+ // An animated scroll may not have moved the feed yet, and
+ // fires no scroll event if there is nowhere to scroll to.
+ if (!waiting_for_animated_scroll) {
+ waiting_for_animated_scroll = true;
+ message_viewport.after_animated_scroll(() => {
+ waiting_for_animated_scroll = false;
+ update();
+ });
+ }
return;
}
- if (is_shown()) {
- hide_once_not_hovered();
+ const scrolled_away = scrolled_away_from_bottom_of_list_id === message_lists.current.id;
+ scrolled_away_from_bottom_of_list_id = undefined;
+
+ if (above_bottom_of_view()) {
+ if (showing_next_unread_conversation()) {
+ const mouse_in_use = scrolled_away || mouse_is_over_button();
+ if (mouse_in_use) {
+ show("scroll_to_bottom");
+ } else {
+ // The icon stays an arrow while the button fades out.
+ hide();
+ }
+ }
+ if (is_shown()) {
+ hide_once_not_hovered();
+ }
+ } else if (!message_lists.current.data.fetch_status.has_found_newest()) {
+ // We are called again once the newest messages are fetched.
+ // Until then, an arrow that is already showing stays if this
+ // view offers one, so that it does not flicker between views.
+ if (showing_next_unread_conversation()) {
+ show_next_unread_conversation_or_hide();
+ } else {
+ hide();
+ }
+ } else if (message_lists.current.visibly_empty()) {
+ hide();
+ } else {
+ show_next_unread_conversation_or_hide();
}
}
-export function show_scroll_to_bottom_button(): void {
+export function handle_non_keyboard_scroll(): void {
+ assert(message_lists.current !== undefined);
if (!above_bottom_of_view()) {
return;
}
- show();
+ if (showing_next_unread_conversation()) {
+ // The feed may only be passing through this position, e.g.,
+ // while autoscrolling to show a newly arrived message, so we
+ // let update() decide what to show once the scroll finishes.
+ scrolled_away_from_bottom_of_list_id = message_lists.current.id;
+ return;
+ }
+
+ show("scroll_to_bottom");
}
-export function initialize(on_click: {scroll_to_bottom: () => void}): void {
+// update() keeps an arrow while the newest messages are awaited, and
+// after a failed fetch they never arrive.
+export function hide_after_failed_fetch(): void {
+ hide();
+}
+
+export function initialize(on_click: Record<ButtonMode, () => void>): void {
$("body").on("click", "#scroll-to-bottom-button-clickable-area", (e) => {
e.preventDefault();
e.stopPropagation();
@@ -99,7 +229,10 @@ export function initialize(on_click: {scroll_to_bottom: () => void}): void {
// Since it take a few milliseconds for this button complete disappear transition,
// it is possible for user to click it before it hides when switching narrows.
if (narrow_state.is_message_feed_visible()) {
- on_click.scroll_to_bottom();
+ // Enter would otherwise click the button again, rather
+ // than open the compose box.
+ ui_util.blur_active_element();
+ on_click[mode]();
}
});
@@ -108,6 +241,12 @@ export function initialize(on_click: {scroll_to_bottom: () => void}): void {
return;
}
+ if (mode !== "scroll_to_bottom") {
+ // Typing a reply should not take away the button for
+ // moving on to the next conversation.
+ return;
+ }
+
// Hide scroll to bottom button on any keypress.
// Keyboard users are very less likely to use this button.
hide();
@@ -120,9 +259,7 @@ export function initialize(on_click: {scroll_to_bottom: () => void}): void {
$show_scroll_to_bottom_button.on("transitionend", (e) => {
assert(e.originalEvent instanceof TransitionEvent);
if (e.originalEvent.propertyName === "visibility") {
- const tooltip = the(
- $<tippy.ReferenceElement>("#scroll-to-bottom-button-clickable-area"),
- )._tippy;
+ const tooltip = get_tooltip();
// make sure the tooltip exists and the class is not currently showing
if (tooltip && !is_shown()) {
tooltip.destroy();
diff --git a/web/src/ui_init.js b/web/src/ui_init.js
index f6b51e7e11c..77f02dd146b 100644
--- a/web/src/ui_init.js
+++ b/web/src/ui_init.js
@@ -426,6 +426,7 @@ function initialize_unread_ui() {
narrow_title.update_unread_counts(counts),
);
unread_ui.register_update_unread_counts_hook(inbox_ui.update);
+ unread_ui.register_update_unread_counts_hook(scroll_to_bottom_button.update);
unread_ui.initialize({mark_narrow_as_read: unread_ops.mark_narrow_as_read});
}
@@ -679,7 +680,18 @@ export async function initialize_everything(state_data) {
server_events.finished_initial_fetch();
});
message_scroll.initialize();
- scroll_to_bottom_button.initialize({scroll_to_bottom: navigate.to_end});
+ scroll_to_bottom_button.initialize({
+ scroll_to_bottom: navigate.to_end,
+ next_unread_topic() {
+ message_view.narrow_to_next_topic({
+ trigger: "next_unread_topic_button",
+ only_followed_topics: false,
+ });
+ },
+ next_unread_dm_conversation() {
+ message_view.narrow_to_next_pm_string({trigger: "next_unread_dm_conversation_button"});
+ },
+ });
markdown.initialize(markdown_config.get_helpers());
linkifiers.initialize(realm.realm_linkifiers);
realm_playground.initialize({
@@ -728,6 +740,9 @@ export async function initialize_everything(state_data) {
// especially when user is away from screen and the window is focused.
if (activity.received_new_messages && activity.new_user_input) {
unread_ops.process_visible();
+ // Messages that arrive already read move the bottom of
+ // the feed without changing unread counts.
+ scroll_to_bottom_button.update();
activity.set_received_new_messages(false);
}
});
diff --git a/web/templates/tooltip_templates.hbs b/web/templates/tooltip_templates.hbs
index a7b1ea40f82..a496af1ce29 100644
--- a/web/templates/tooltip_templates.hbs
+++ b/web/templates/tooltip_templates.hbs
@@ -10,6 +10,14 @@
{{t 'Scroll to bottom' }}
{{tooltip_hotkey_hints "End"}}
</template>
+<template id="next-unread-topic-button-tooltip-template">
+ {{t 'Go to next unread topic' }}
+ {{tooltip_hotkey_hints "N"}}
+</template>
+<template id="next-unread-dm-conversation-button-tooltip-template">
+ {{t 'Go to next unread direct message' }}
+ {{tooltip_hotkey_hints "P"}}
+</template>
<template id="compose_disable_stream_reply_button_tooltip_template">
{{t 'You do not have permission to post in this channel.' }}
</template>
diff --git a/web/tests/message_view.test.cjs b/web/tests/message_view.test.cjs
index 094d9219208..2a3b246c4f8 100644
--- a/web/tests/message_view.test.cjs
+++ b/web/tests/message_view.test.cjs
@@ -1310,13 +1310,17 @@ run_test("narrow_compute_title", () => {
run_test("preserves_topics_kept_unread_by_user", () => {
for (const trigger of [
"next_topic_unread_hotkey",
+ "next_unread_topic_button",
"old_unreads_missing",
"retarget message location",
]) {
assert.ok(message_view.preserves_topics_kept_unread_by_user(trigger));
}
- // `p` moves to a direct message conversation.
+ // `p` and its button move to a direct message conversation.
assert.ok(!message_view.preserves_topics_kept_unread_by_user("hotkey"));
+ assert.ok(
+ !message_view.preserves_topics_kept_unread_by_user("next_unread_dm_conversation_button"),
+ );
assert.ok(!message_view.preserves_topics_kept_unread_by_user(undefined));
});
diff --git a/web/tests/scroll_to_bottom_button.test.cjs b/web/tests/scroll_to_bottom_button.test.cjs
index c5ffa8f967a..5247faf3ac0 100644
--- a/web/tests/scroll_to_bottom_button.test.cjs
+++ b/web/tests/scroll_to_bottom_button.test.cjs
@@ -3,7 +3,7 @@
const assert = require("node:assert/strict");
const {clock, mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");
-const {run_test} = require("./lib/test.cjs");
+const {run_test, noop} = require("./lib/test.cjs");
const {$} = require("./lib/zjquery.cjs");
set_global("document", "document-stub");
@@ -18,26 +18,95 @@ const message_lists = mock_esm("../src/message_lists", {current: undefined});
const message_scroll_state = mock_esm("../src/message_scroll_state", {
actively_scrolling: false,
});
-const message_viewport = mock_esm("../src/message_viewport");
-const narrow_state = mock_esm("../src/narrow_state");
+const message_viewport = mock_esm("../src/message_viewport", {
+ is_animating_scroll: () => false,
+});
+const narrow_state = mock_esm("../src/narrow_state", {
+ stream_id: () => 101,
+ topic: () => "current topic",
+ pm_ids_string: () => "31,32",
+});
+const stream_list = mock_esm("../src/stream_list", {
+ get_sorted_channel_ids_for_next_unread_navigation: () => [
+ {channel_id: 101, is_collapsed: false},
+ ],
+});
+const tippyjs = mock_esm("../src/tippyjs");
+const topic_generator = mock_esm("../src/topic_generator");
+const ui_util = mock_esm("../src/ui_util");
const scroll_to_bottom_button = zrequire("scroll_to_bottom_button");
const $container = () => $("#scroll-to-bottom-button-container");
+const $icon = () => $("#scroll-to-bottom-button .zulip-icon");
const $clickable_area = () => $("#scroll-to-bottom-button-clickable-area");
+const appearance = {
+ scroll_to_bottom: {
+ icon: "chevron-down",
+ tooltip_template_id: "scroll-to-bottom-button-tooltip-template",
+ },
+ next_unread_topic: {
+ icon: "arrow-right",
+ tooltip_template_id: "next-unread-topic-button-tooltip-template",
+ },
+ next_unread_dm_conversation: {
+ icon: "arrow-right",
+ tooltip_template_id: "next-unread-dm-conversation-button-tooltip-template",
+ },
+};
+
function set_feed(
override,
- {bottom_visible = true, fetched_end_rendered = true, newest = "found", empty = false} = {},
+ {
+ bottom_visible = true,
+ fetched_end_rendered = true,
+ newest = "found",
+ empty = false,
+ view = "other",
+ other_unread_conversation = true,
+ id = 1,
+ } = {},
) {
override(message_viewport, "bottom_rendered_message_visible", () => bottom_visible, {
unused: false,
});
+ override(
+ topic_generator,
+ "has_next_unread_topic",
+ (stream_id, topic, channels_info) => {
+ assert.equal(stream_id, narrow_state.stream_id());
+ assert.equal(topic, narrow_state.topic());
+ assert.deepEqual(
+ channels_info,
+ stream_list.get_sorted_channel_ids_for_next_unread_navigation(),
+ );
+ return other_unread_conversation;
+ },
+ {unused: false},
+ );
+ override(
+ topic_generator,
+ "get_next_unread_pm_string",
+ (user_ids_string) => {
+ assert.equal(user_ids_string, narrow_state.pm_ids_string());
+ return other_unread_conversation ? "33" : undefined;
+ },
+ {unused: false},
+ );
override(message_lists, "current", {
+ id,
visibly_empty: () => empty,
view: {is_fetched_end_rendered: () => fetched_end_rendered},
data: {
- fetch_status: {can_load_newer_messages: () => newest === "not_fetched"},
+ fetch_status: {
+ has_found_newest: () => newest === "found",
+ can_load_newer_messages: () => newest === "not_fetched",
+ },
+ filter: {
+ can_show_next_unread_topic_conversation_button: () => view === "topic",
+ can_show_next_unread_dm_conversation_button: () => view === "dm",
+ },
},
});
}
@@ -46,9 +115,26 @@ function is_shown() {
return $container().hasClass("show");
}
+function assert_shows(mode) {
+ assert.ok(is_shown());
+ for (const icon of ["chevron-down", "arrow-right"]) {
+ assert.equal($icon().hasClass(`zulip-icon-${icon}`), icon === appearance[mode].icon);
+ }
+ assert.equal(
+ $clickable_area().attr("data-tooltip-template-id"),
+ appearance[mode].tooltip_template_id,
+ );
+}
+
function mouse_scroll() {
- scroll_to_bottom_button.show_scroll_to_bottom_button();
+ scroll_to_bottom_button.handle_non_keyboard_scroll();
+ scroll_to_bottom_button.update();
+}
+
+function show_arrow(override) {
+ set_feed(override, {view: "topic"});
scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
}
function keydown(modifiers = {}) {
@@ -82,12 +168,31 @@ function test(label, f) {
run_test(label, (helpers) => {
clock.reset();
$container().set_matches(":hover", false);
+ $icon().addClass("zulip-icon-chevron-down");
+ $clickable_area().attr(
+ "data-tooltip-template-id",
+ appearance.scroll_to_bottom.tooltip_template_id,
+ );
clicks = [];
- scroll_to_bottom_button.initialize({
- scroll_to_bottom() {
- clicks.push("scroll_to_bottom");
- },
- });
+ scroll_to_bottom_button.initialize(
+ Object.fromEntries(
+ Object.keys(appearance).map((mode) => [
+ mode,
+ () => {
+ clicks.push(mode);
+ },
+ ]),
+ ),
+ );
+
+ // The module keeps its state between tests. A mouse scroll
+ // above the bottom followed by a keypress resets it, apart
+ // from a pending animated scroll, which tests must finish.
+ set_feed(helpers.override, {bottom_visible: false});
+ mouse_scroll();
+ keydown();
+ assert.ok(!is_shown());
+
f(helpers);
});
}
@@ -123,6 +228,34 @@ test("not updated until the scroll has finished", ({override}) => {
assert.ok(!is_shown());
});
+test("not updated until an animated scroll is over", ({override}) => {
+ set_feed(override, {bottom_visible: false});
+ mouse_scroll();
+
+ set_feed(override);
+ const callbacks = [];
+ override(message_viewport, "is_animating_scroll", () => true);
+ override(message_viewport, "after_animated_scroll", (callback) => {
+ callbacks.push(callback);
+ });
+ scroll_to_bottom_button.update();
+ scroll_to_bottom_button.update();
+ assert.ok(is_shown());
+ assert.equal(callbacks.length, 1);
+
+ override(message_viewport, "is_animating_scroll", () => false);
+ callbacks[0]();
+ assert.ok(!is_shown());
+
+ // The next animated scroll is waited for again.
+ override(message_viewport, "is_animating_scroll", () => true);
+ scroll_to_bottom_button.update();
+ assert.equal(callbacks.length, 2);
+
+ override(message_viewport, "is_animating_scroll", () => false);
+ callbacks[1]();
+});
+
test("hidden at the bottom of the feed", ({override}) => {
set_feed(override);
@@ -137,7 +270,7 @@ test("hidden at the bottom of the feed", ({override}) => {
test("hidden in an empty feed", ({override}) => {
set_feed(override, {bottom_visible: false, empty: true});
- scroll_to_bottom_button.show_scroll_to_bottom_button();
+ scroll_to_bottom_button.handle_non_keyboard_scroll();
assert.ok(!is_shown());
$container().addClass("show");
@@ -169,7 +302,7 @@ test("not shown while the newest messages are being fetched", ({override}) => {
test("shown by a mouse scroll above the bottom", ({override}) => {
set_feed(override, {bottom_visible: false});
- scroll_to_bottom_button.show_scroll_to_bottom_button();
+ scroll_to_bottom_button.handle_non_keyboard_scroll();
assert.ok(is_shown());
scroll_to_bottom_button.update();
@@ -217,7 +350,7 @@ test("stays while the mouse is over it", ({override}) => {
test("the three seconds start when the scroll finishes", ({override}) => {
set_feed(override, {bottom_visible: false});
- scroll_to_bottom_button.show_scroll_to_bottom_button();
+ scroll_to_bottom_button.handle_non_keyboard_scroll();
clock.tick(5000);
assert.ok(is_shown());
@@ -277,11 +410,36 @@ test("a keyboard scroll does not make a later hide come early", ({override}) =>
test("a click scrolls to the bottom", ({override}) => {
override(narrow_state, "is_message_feed_visible", () => true);
+ override(ui_util, "blur_active_element", noop);
click();
assert.deepEqual(clicks, ["scroll_to_bottom"]);
});
+test("a click on the arrow goes to the next unread conversation", ({override}) => {
+ override(narrow_state, "is_message_feed_visible", () => true);
+ override(ui_util, "blur_active_element", noop);
+
+ show_arrow(override);
+ click();
+ assert.deepEqual(clicks, ["next_unread_topic"]);
+
+ set_feed(override, {view: "dm"});
+ scroll_to_bottom_button.update();
+ click();
+ assert.deepEqual(clicks, ["next_unread_topic", "next_unread_dm_conversation"]);
+});
+
+test("a click takes focus off the button before acting", ({override}) => {
+ override(narrow_state, "is_message_feed_visible", () => true);
+ override(ui_util, "blur_active_element", () => {
+ clicks.push("blur");
+ });
+
+ click();
+ assert.deepEqual(clicks, ["blur", "scroll_to_bottom"]);
+});
+
test("a click does nothing once the message feed is hidden", ({override}) => {
override(narrow_state, "is_message_feed_visible", () => false);
@@ -324,3 +482,305 @@ test("tooltip is destroyed once the button has faded out", () => {
transitionend({originalEvent: new TransitionEvent("visibility")});
assert.ok(tooltip_destroyed);
});
+
+test("next unread topic at the bottom of a topic or channel view", ({override}) => {
+ set_feed(override, {view: "topic"});
+
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+});
+
+test("next unread DM conversation at the bottom of a DM view", ({override}) => {
+ set_feed(override, {view: "dm"});
+
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_dm_conversation");
+});
+
+test("no arrow without another unread topic", ({override}) => {
+ set_feed(override, {view: "topic", other_unread_conversation: false});
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("no arrow without another unread DM conversation", ({override}) => {
+ set_feed(override, {view: "dm", other_unread_conversation: false});
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("arrow comes and goes with other unread conversations", ({override}) => {
+ show_arrow(override);
+
+ set_feed(override, {view: "topic", other_unread_conversation: false});
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+
+ show_arrow(override);
+});
+
+test("no arrow in an empty view", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "topic", empty: true});
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("scroll to bottom becomes the arrow at the bottom", ({override}) => {
+ set_feed(override, {view: "topic", bottom_visible: false});
+ mouse_scroll();
+ assert_shows("scroll_to_bottom");
+
+ $container().set_matches(":hover", true);
+ set_feed(override, {view: "topic"});
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+
+ $container().set_matches(":hover", false);
+ clock.tick(3000);
+ assert_shows("next_unread_topic");
+ assert.equal(clock.countTimers(), 0);
+});
+
+test("scroll to bottom hides at the bottom if nothing else is unread", ({override}) => {
+ set_feed(override, {view: "topic", bottom_visible: false});
+ mouse_scroll();
+ assert_shows("scroll_to_bottom");
+
+ set_feed(override, {view: "topic", other_unread_conversation: false});
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("an open tooltip follows the mode", ({override}) => {
+ set_feed(override, {view: "topic"});
+ let tooltip_content;
+ $clickable_area()[0]._tippy = {
+ setContent(content) {
+ tooltip_content = content;
+ },
+ };
+ override(tippyjs, "get_tooltip_content", (reference) =>
+ reference.getAttribute("data-tooltip-template-id"),
+ );
+
+ scroll_to_bottom_button.update();
+ assert.equal(tooltip_content, appearance.next_unread_topic.tooltip_template_id);
+
+ // Its content is only set again when the mode changes.
+ tooltip_content = undefined;
+ scroll_to_bottom_button.update();
+ assert.equal(tooltip_content, undefined);
+});
+
+test("arrow stays through keypresses", ({override}) => {
+ show_arrow(override);
+
+ keydown();
+ assert_shows("next_unread_topic");
+});
+
+test("arrow stays through a scroll that ends at the bottom", ({override}) => {
+ show_arrow(override);
+
+ // A newly arrived message is below the screen until the feed
+ // has scrolled to it.
+ set_feed(override, {view: "topic", bottom_visible: false});
+ override(message_scroll_state, "actively_scrolling", true);
+ mouse_scroll();
+ assert_shows("next_unread_topic");
+
+ set_feed(override, {view: "topic"});
+ override(message_scroll_state, "actively_scrolling", false);
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+
+ // The finished scroll is forgotten.
+ set_feed(override, {view: "topic", bottom_visible: false});
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("arrow stays while the feed has yet to scroll to a new message", ({override}) => {
+ show_arrow(override);
+
+ set_feed(override, {view: "topic", bottom_visible: false});
+ let update_after_animated_scroll;
+ override(message_viewport, "is_animating_scroll", () => true);
+ override(message_viewport, "after_animated_scroll", (callback) => {
+ update_after_animated_scroll = callback;
+ });
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+
+ set_feed(override, {view: "topic"});
+ override(message_viewport, "is_animating_scroll", () => false);
+ update_after_animated_scroll();
+ assert_shows("next_unread_topic");
+});
+
+test("arrow becomes scroll to bottom after a mouse scroll away", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "topic", bottom_visible: false});
+
+ override(message_scroll_state, "actively_scrolling", true);
+ mouse_scroll();
+ assert_shows("next_unread_topic");
+
+ override(message_scroll_state, "actively_scrolling", false);
+ scroll_to_bottom_button.update();
+ assert_shows("scroll_to_bottom");
+
+ clock.tick(3000);
+ assert.ok(!is_shown());
+});
+
+test("arrow hides after a keyboard scroll away", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "topic", bottom_visible: false});
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+ // The icon is not swapped while the button fades out.
+ assert.ok($icon().hasClass("zulip-icon-arrow-right"));
+
+ scroll_to_bottom_button.handle_non_keyboard_scroll();
+ assert_shows("scroll_to_bottom");
+});
+
+test("a mouse scroll in the previous view does not count", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "topic", bottom_visible: false});
+ override(message_scroll_state, "actively_scrolling", true);
+ mouse_scroll();
+
+ override(message_scroll_state, "actively_scrolling", false);
+ set_feed(override, {view: "topic", bottom_visible: false, id: 2});
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("a mouse scroll only counts for the update that follows it", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "topic", bottom_visible: false});
+ override(message_scroll_state, "actively_scrolling", true);
+ mouse_scroll();
+
+ override(message_scroll_state, "actively_scrolling", false);
+ set_feed(override, {view: "topic", newest: "loading"});
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+
+ set_feed(override, {view: "topic", bottom_visible: false});
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("arrow under the mouse becomes scroll to bottom", ({override}) => {
+ show_arrow(override);
+ $container().set_matches(":hover", true);
+ set_feed(override, {view: "topic", bottom_visible: false, id: 2});
+
+ scroll_to_bottom_button.update();
+ assert_shows("scroll_to_bottom");
+
+ $container().set_matches(":hover", false);
+ clock.tick(3000);
+ assert.ok(!is_shown());
+});
+
+test("arrow stays while a conversation that ends on the screen loads", ({override}) => {
+ show_arrow(override);
+
+ // Opening the view scrolls the feed.
+ set_feed(override, {view: "topic", newest: "loading", id: 2});
+ mouse_scroll();
+ assert_shows("next_unread_topic");
+
+ set_feed(override, {view: "topic", id: 2});
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+});
+
+test("arrow stays while an empty view loads", ({override}) => {
+ show_arrow(override);
+
+ set_feed(override, {view: "topic", newest: "loading", empty: true, id: 2});
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+
+ set_feed(override, {view: "topic", newest: "not_fetched", empty: true, id: 2});
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+});
+
+test("arrow hides once a conversation turns out to continue below the screen", ({override}) => {
+ show_arrow(override);
+
+ set_feed(override, {view: "topic", newest: "loading", id: 2});
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+
+ set_feed(override, {view: "topic", bottom_visible: false, id: 2});
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("arrow hides in a loading view that already continues below the screen", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "topic", newest: "loading", bottom_visible: false, id: 2});
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("arrow takes the mode of the view that is loading", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "dm", newest: "loading", id: 2});
+
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_dm_conversation");
+});
+
+test("arrow hides while a view that offers none loads", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {newest: "loading", id: 2});
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("hidden button stays hidden until the view has loaded", ({override}) => {
+ set_feed(override, {view: "topic", newest: "loading"});
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+
+ show_arrow(override);
+});
+
+test("scroll to bottom hides in a view that is loading", ({override}) => {
+ set_feed(override, {view: "topic", bottom_visible: false});
+ mouse_scroll();
+ assert_shows("scroll_to_bottom");
+
+ set_feed(override, {view: "topic", newest: "loading", empty: true, id: 2});
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});
+
+test("arrow hides after a fetch that failed for good", ({override}) => {
+ show_arrow(override);
+ set_feed(override, {view: "topic", newest: "loading", empty: true, id: 2});
+ scroll_to_bottom_button.update();
+ assert_shows("next_unread_topic");
+
+ scroll_to_bottom_button.hide_after_failed_fetch();
+ assert.ok(!is_shown());
+
+ scroll_to_bottom_button.update();
+ assert.ok(!is_shown());
+});