Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_feed: Show the top loading indicator for at least 750ms.
Shubham Padia31c1af990e07
diff --git a/web/src/message_feed_loading.ts b/web/src/message_feed_loading.tsindex b9251a948d3..aed9f9dbcfe 100644--- a/web/src/message_feed_loading.ts+++ b/web/src/message_feed_loading.ts@@ -1,6 +1,10 @@import {$} from "jquery";import * as loading from "./loading.ts";+import * as util from "./util.ts";++export const MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS = 750;+export const TOP_OF_FEED_INDICATOR_FADE_OUT_MS = 200;let top_of_feed_indicator_showing = false;let loading_newer_messages_indicator_showing = false;@@ -10,6 +14,14 @@ let loading_newer_messages_indicator_showing = false;// fetch for older messages. It stays up while either applies.let initial_page_load_pending = false;let fetching_older_messages = false;+let top_of_feed_indicator_shown_at = 0;+let pending_hide_top_of_feed_indicator_timer: ReturnType<typeof setTimeout> | undefined;+let top_of_feed_indicator_fade_out_timer: ReturnType<typeof setTimeout> | undefined;+let run_when_top_of_feed_indicator_hidden_callbacks: (() => void)[] = [];++function cancel_pending_hide_top_of_feed_indicator(): void {+ clearTimeout(pending_hide_top_of_feed_indicator_timer);+}function top_of_feed_in_view(): boolean {// Same check as message_viewport.at_rendered_top, duplicated here@@ -17,31 +29,93 @@ function top_of_feed_in_view(): boolean {return window.scrollY <= 0;}+function show_top_of_feed_indicator_now(): void {+ cancel_pending_hide_top_of_feed_indicator();+ if (!top_of_feed_indicator_showing) {+ top_of_feed_indicator_shown_at = Date.now();+ if (top_of_feed_indicator_fade_out_timer !== undefined) {+ clearTimeout(top_of_feed_indicator_fade_out_timer);+ top_of_feed_indicator_fade_out_timer = undefined;+ }+ $("#top-of-feed-loading-indicator").removeClass("fade-out").addClass("loading");+ top_of_feed_indicator_showing = true;+ }+}++function hide_top_of_feed_indicator_now(): void {+ cancel_pending_hide_top_of_feed_indicator();+ if (top_of_feed_indicator_showing) {+ $("#top-of-feed-loading-indicator").removeClass("loading").addClass("fade-out");+ top_of_feed_indicator_fade_out_timer = setTimeout(() => {+ $("#top-of-feed-loading-indicator").removeClass("fade-out");+ top_of_feed_indicator_fade_out_timer = undefined;+ }, TOP_OF_FEED_INDICATOR_FADE_OUT_MS);+ top_of_feed_indicator_showing = false;+ }+ const callbacks = run_when_top_of_feed_indicator_hidden_callbacks;+ run_when_top_of_feed_indicator_hidden_callbacks = [];+ for (const callback of callbacks) {+ callback();+ }+}+function update_top_of_feed_indicator(): void {// The initial page load always shows the indicator. Loading older// messages only shows it when the user is at the top of the feed.const should_show_indicator =initial_page_load_pending || (fetching_older_messages && top_of_feed_in_view());- if (should_show_indicator === top_of_feed_indicator_showing) {+ if (should_show_indicator) {+ show_top_of_feed_indicator_now();return;}- if (should_show_indicator) {- $("#top-of-feed-loading-indicator").addClass("loading");- top_of_feed_indicator_showing = true;- } else {- $("#top-of-feed-loading-indicator").removeClass("loading");- top_of_feed_indicator_showing = false;+ if (!top_of_feed_indicator_showing) {+ return;+ }+ if (!top_of_feed_in_view()) {+ hide_top_of_feed_indicator_now();+ return;}+ const remaining_ms = util.get_remaining_time(+ top_of_feed_indicator_shown_at,+ MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS,+ );+ if (remaining_ms > 0) {+ cancel_pending_hide_top_of_feed_indicator();+ pending_hide_top_of_feed_indicator_timer = setTimeout(+ hide_top_of_feed_indicator_now,+ remaining_ms,+ );+ return;+ }+ hide_top_of_feed_indicator_now();}export function update_for_scroll_position(): void {// Scrolling can bring the top of the feed into or out of view while- // older messages are being fetched.- if (fetching_older_messages) {+ // older messages are being fetched, or while the line is still up+ // for its minimum display time after one.+ if (fetching_older_messages || top_of_feed_indicator_showing) {update_top_of_feed_indicator();}}+export function run_when_top_of_feed_indicator_hidden(callback: () => void): void {+ // Notices that say there is nothing more to load should not appear+ // while the indicator is still being kept on screen; they wait+ // until it goes away. Don't wait for the initial page load+ // indicator, which tracks the initial message fetch rather than+ // the current narrow's fetch.+ if (!top_of_feed_indicator_showing || initial_page_load_pending) {+ callback();+ return;+ }+ run_when_top_of_feed_indicator_hidden_callbacks.push(callback);+}++export function cancel_run_when_top_of_feed_indicator_hidden(): void {+ run_when_top_of_feed_indicator_hidden_callbacks = [];+}+export function show_loading_initial_page(): void {initial_page_load_pending = true;update_top_of_feed_indicator();@@ -81,6 +155,13 @@ export function hide_loading_newer(): void {}export function hide_indicators(): void {- hide_loading_older();+ // Called when resetting the UI for a new narrow, so hide right away,+ // unless the initial page load is still pending. Anything waiting+ // on the old narrow's indicator is stale.+ cancel_run_when_top_of_feed_indicator_hidden();+ fetching_older_messages = false;+ if (!initial_page_load_pending) {+ hide_top_of_feed_indicator_now();+ }hide_loading_newer();}diff --git a/web/src/message_feed_top_notices.ts b/web/src/message_feed_top_notices.tsindex 85ab66d4543..74dffb88898 100644--- a/web/src/message_feed_top_notices.ts+++ b/web/src/message_feed_top_notices.ts@@ -2,6 +2,7 @@ import {$} from "jquery";import assert from "minimalistic-assert";import * as hash_util from "./hash_util.ts";+import * as message_feed_loading from "./message_feed_loading.ts";import type {MessageList} from "./message_list.ts";import * as message_lists from "./message_lists.ts";import * as narrow_state from "./narrow_state.ts";@@ -53,25 +54,35 @@ export function update_top_of_narrow_notices(msg_list: MessageList): void {return;}- if (msg_list.data.fetch_status.has_found_oldest()) {- const filter = narrow_state.filter();- // Potentially display the notice that lets users know- // that not all messages were searched. One could- // imagine including `filter.is_keyword_search()` in these- // conditions, but there's a very legitimate use case- // for moderation of searching for all messages sent- // by a potential spammer user.- if (filter?.may_have_incomplete_message_history(false)) {- show_end_of_results_notice();+ message_feed_loading.run_when_top_of_feed_indicator_hidden(() => {+ if (msg_list !== message_lists.current) {+ return;}- }- if (msg_list.data.fetch_status.history_limited()) {- show_history_limit_notice();- }+ if (msg_list.data.fetch_status.has_found_oldest()) {+ const filter = narrow_state.filter();+ // Potentially display the notice that lets users know+ // that not all messages were searched. One could+ // imagine including `filter.is_keyword_search()` in these+ // conditions, but there's a very legitimate use case+ // for moderation of searching for all messages sent+ // by a potential spammer user.+ if (filter?.may_have_incomplete_message_history(false)) {+ show_end_of_results_notice();+ }+ }++ if (msg_list.data.fetch_status.history_limited()) {+ show_history_limit_notice();+ }+ });}export function hide_top_of_narrow_notices(): void {+ // A notice waiting for the loading indicator to go away must not+ // show up after this; the empty-narrow banner, for one, hides the+ // notices so that it can take their place.+ message_feed_loading.cancel_run_when_top_of_feed_indicator_hidden();hide_end_of_results_notice();hide_history_limit_notice();}diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.tsindex 2f6cfff44de..4a7d31c6f13 100644--- a/web/src/message_fetch.ts+++ b/web/src/message_fetch.ts@@ -141,7 +141,12 @@ export function fetch_more_if_required_for_current_msg_list(if (has_found_oldest && has_found_newest && message_lists.current.visibly_empty()) {// Even after loading more messages, we have// no messages to display in this narrow.- narrow_banner.show_empty_narrow_message(message_lists.current.data.filter);+ const msg_list = message_lists.current;+ message_feed_loading.run_when_top_of_feed_indicator_hidden(() => {+ if (msg_list === message_lists.current && msg_list.visibly_empty()) {+ narrow_banner.show_empty_narrow_message(msg_list.data.filter);+ }+ });message_lists.current.update_trailing_bookend();compose_closed_ui.maybe_update_buttons_for_dm_recipient();compose_validate.validate_and_update_send_button_status();diff --git a/web/styles/zulip.css b/web/styles/zulip.cssindex 05b72475f29..2a68e71c252 100644--- a/web/styles/zulip.css+++ b/web/styles/zulip.css@@ -1312,7 +1312,7 @@ nav {/* The pulse only moves while the line is up, so that each timethe line shows up the sweep starts over from the left edge. */- &.loading::after {+ &:is(.loading, .fade-out)::after {animation: top-of-feed-loading-pulse 750ms ease-in-out infinite;@media (prefers-reduced-motion) {diff --git a/web/tests/message_feed_loading.test.cjs b/web/tests/message_feed_loading.test.cjsindex 2b624ce9f39..8ac3275c9ac 100644--- a/web/tests/message_feed_loading.test.cjs+++ b/web/tests/message_feed_loading.test.cjs@@ -2,11 +2,12 @@const assert = require("node:assert/strict");-const {zrequire} = require("./lib/namespace.cjs");+const {clock, zrequire} = require("./lib/namespace.cjs");const {run_test} = require("./lib/test.cjs");const {$} = require("./lib/zjquery.cjs");const message_feed_loading = zrequire("message_feed_loading");+const {MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS} = message_feed_loading;function is_loading() {return $("#top-of-feed-loading-indicator").hasClass("loading");@@ -23,6 +24,87 @@ function test(label, f) {});}+test("minimum display time", () => {+ message_feed_loading.show_loading_older();+ assert.ok(is_loading());++ // A fetch that finishes quickly leaves the indicator up until the+ // minimum display time has elapsed.+ clock.tick(100);+ message_feed_loading.hide_loading_older();+ assert.ok(is_loading());+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS - 100 - 1);+ assert.ok(is_loading());+ clock.tick(1);+ assert.ok(!is_loading());++ // A slow fetch hides the indicator right away.+ message_feed_loading.show_loading_older();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);+ message_feed_loading.hide_loading_older();+ assert.ok(!is_loading());+});++test("new fetch during pending hide", () => {+ message_feed_loading.show_loading_older();+ message_feed_loading.hide_loading_older();+ clock.tick(100);++ // Another fetch starts before the minimum time is up. The first+ // fetch's pending hide must not take the indicator down while+ // this one is still in flight, however long it takes.+ message_feed_loading.show_loading_older();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS * 3);+ assert.ok(is_loading());+ message_feed_loading.hide_loading_older();+ assert.ok(!is_loading());+});++test("hide_indicators hides immediately", () => {+ message_feed_loading.show_loading_older();+ message_feed_loading.hide_loading_older();+ message_feed_loading.hide_indicators();+ assert.ok(!is_loading());++ // The pending hide from before the reset must not affect a fetch+ // in the new narrow.+ message_feed_loading.show_loading_older();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS * 3);+ assert.ok(is_loading());+ message_feed_loading.hide_loading_older();+ assert.ok(!is_loading());+});++test("run_when_top_of_feed_indicator_hidden", () => {+ let runs = 0;+ const callback = () => {+ runs += 1;+ };++ // Nothing is showing, so the callback runs right away.+ message_feed_loading.run_when_top_of_feed_indicator_hidden(callback);+ assert.equal(runs, 1);++ // While the indicator is being kept on screen, the callback waits+ // until it is hidden.+ message_feed_loading.show_loading_older();+ message_feed_loading.hide_loading_older();+ message_feed_loading.run_when_top_of_feed_indicator_hidden(callback);+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS - 1);+ assert.equal(runs, 1);+ clock.tick(1);+ assert.ok(!is_loading());+ assert.equal(runs, 2);++ // Resetting for a new narrow drops callbacks from the old one.+ message_feed_loading.show_loading_older();+ message_feed_loading.hide_loading_older();+ message_feed_loading.run_when_top_of_feed_indicator_hidden(callback);+ message_feed_loading.hide_indicators();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);+ assert.equal(runs, 2);+});+test("initial page load and older fetches share the indicator", () => {message_feed_loading.show_loading_initial_page();assert.ok(is_loading());@@ -31,6 +113,7 @@ test("initial page load and older fetches share the indicator", () => {// initial page load does not take the indicator down.message_feed_loading.show_loading_older();message_feed_loading.hide_loading_older();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);assert.ok(is_loading());// Nor does resetting the UI for a new narrow.@@ -44,15 +127,46 @@ test("initial page load and older fetches share the indicator", () => {message_feed_loading.show_loading_older();assert.ok(is_loading());message_feed_loading.hide_loading_older();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);assert.ok(!is_loading());});+test("run_when_top_of_feed_indicator_hidden during the initial page load", () => {+ let runs = 0;+ const callback = () => {+ runs += 1;+ };++ // The initial message fetch can finish long after the current+ // narrow's own fetch, so callbacks do not wait for it, even if+ // the narrow fetched older messages in the meantime.+ message_feed_loading.show_loading_initial_page();+ message_feed_loading.run_when_top_of_feed_indicator_hidden(callback);+ assert.equal(runs, 1);+ message_feed_loading.show_loading_older();+ message_feed_loading.hide_loading_older();+ message_feed_loading.run_when_top_of_feed_indicator_hidden(callback);+ assert.equal(runs, 2);+ assert.ok(is_loading());++ // Once the page has loaded, callbacks wait for older fetches again.+ message_feed_loading.hide_loading_initial_page();+ message_feed_loading.show_loading_older();+ message_feed_loading.run_when_top_of_feed_indicator_hidden(callback);+ assert.equal(runs, 2);+ message_feed_loading.hide_loading_older();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);+ assert.equal(runs, 3);+});+test("page load finishing during a fetch keeps the indicator", () => {message_feed_loading.show_loading_initial_page();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);message_feed_loading.show_loading_older();message_feed_loading.hide_loading_initial_page();assert.ok(is_loading());message_feed_loading.hide_loading_older();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);assert.ok(!is_loading());});@@ -62,30 +176,46 @@ test("older fetches are only shown while the top of the feed is in view", () =>assert.ok(!is_loading());// Scrolling the top of the feed into view during the fetch shows- // the indicator.+ // the indicator, and its minimum display time starts then.+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS * 3);set_top_of_feed_in_view(true);message_feed_loading.update_for_scroll_position();assert.ok(is_loading());message_feed_loading.hide_loading_older();+ assert.ok(is_loading());+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);assert.ok(!is_loading());- // Scrolling it out of view during a fetch hides the indicator.+ // Scrolling it out of view hides the indicator right away, during+ // a fetch or while the indicator is still up for its minimum time+ // after one.message_feed_loading.show_loading_older();assert.ok(is_loading());set_top_of_feed_in_view(false);message_feed_loading.update_for_scroll_position();assert.ok(!is_loading());message_feed_loading.hide_loading_older();+ set_top_of_feed_in_view(true);+ message_feed_loading.show_loading_older();+ message_feed_loading.hide_loading_older();+ assert.ok(is_loading());+ set_top_of_feed_in_view(false);+ message_feed_loading.update_for_scroll_position();+ assert.ok(!is_loading());// Scrolling with no fetch in flight changes nothing.set_top_of_feed_in_view(true);message_feed_loading.update_for_scroll_position();assert.ok(!is_loading());- // The initial page load is shown wherever the feed is scrolled to.+ // The initial page load is shown wherever the feed is scrolled to,+ // and scrolling does not hide it.set_top_of_feed_in_view(false);message_feed_loading.show_loading_initial_page();assert.ok(is_loading());+ message_feed_loading.update_for_scroll_position();+ assert.ok(is_loading());message_feed_loading.hide_loading_initial_page();+ clock.tick(MIN_TOP_OF_FEED_INDICATOR_DISPLAY_MS);assert.ok(!is_loading());});