Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
recent_view: Add "Channel" header as a sort-by-channel button.
Aman Agrawalf59ed4e3213f
diff --git a/web/src/list_widget.ts b/web/src/list_widget.tsindex e30c884dd98..7dc9946da81 100644--- a/web/src/list_widget.ts+++ b/web/src/list_widget.ts@@ -498,7 +498,7 @@ export function create<Key, Item = Key>("click.list_widget_sort","[data-sort]",function (this: HTMLElement) {- handle_sort($(this), widget);+ handle_sort($(this), widget, opts.$parent_container);},);}@@ -659,7 +659,11 @@ export function create<Key, Item = Key>(return widget;}-export function handle_sort<Key, Item>($th: JQuery, list: ListWidget<Key, Item>): void {+export function handle_sort<Key, Item>(+ $th: JQuery,+ list: ListWidget<Key, Item>,+ $parent_container?: JQuery,+): void {/*one would specify sort parameters like this:- name => sort alphabetic.@@ -686,7 +690,13 @@ export function handle_sort<Key, Item>($th: JQuery, list: ListWidget<Key, Item>)$th.removeClass("descend");}} else {- $th.siblings(".active").removeClass("active");+ if ($parent_container) {+ // Remove `active` class for other elements with `[data-sort]`.+ // This helps support HTML structures where the sorting `<th>` elements are not siblings.+ $parent_container.find("[data-sort].active").not($th).removeClass("active");+ } else {+ $th.siblings(".active").removeClass("active");+ }$th.addClass("active");}diff --git a/web/src/recent_view_ui.ts b/web/src/recent_view_ui.tsindex 0fa95005a79..4f235418f36 100644--- a/web/src/recent_view_ui.ts+++ b/web/src/recent_view_ui.ts@@ -1262,6 +1262,42 @@ function sort_comparator(a: string, b: string): number {return -1;}+function channel_sort(a: Row, b: Row): number {+ if (a.type === b.type) {+ const a_msg = message_store.get(a.last_msg_id);+ assert(a_msg !== undefined);+ const b_msg = message_store.get(b.last_msg_id);+ assert(b_msg !== undefined);++ if (a_msg.type === "stream") {+ assert(b_msg.type === "stream");+ const channel_cmp = sort_comparator(+ stream_data.get_stream_name_from_id(a_msg.stream_id),+ stream_data.get_stream_name_from_id(b_msg.stream_id),+ );+ if (channel_cmp !== 0) {+ return channel_cmp;+ }+ } else {+ assert(a_msg.type === "private");+ assert(b_msg.type === "private");+ const dm_cmp = sort_comparator(a_msg.display_reply_to, b_msg.display_reply_to);+ if (dm_cmp !== 0) {+ return dm_cmp;+ }+ }+ // Secondary sort: always most recent first, regardless of channel+ // sort direction. list_widget negates the entire return value when+ // in reverse mode, so we compensate here so that recency always+ // sorts most-recent-first within a channel.+ const is_descend = $(".recent-view-channel-sort-header").hasClass("descend");+ const recency = b.last_msg_id - a.last_msg_id;+ return is_descend ? -recency : recency;+ }+ // if type is not same sort between "private" and "stream"+ return sort_comparator(a.type, b.type);+}+function conversation_sort(a: Row, b: Row): number {if (a.type === b.type) {const a_msg = message_store.get(a.last_msg_id);@@ -1481,6 +1517,7 @@ export function complete_rerender(coming_from_other_views = false): void {},},sort_fields: {+ channel_sort,conversation_sort,unread_sort,...list_widget.generic_sort_functions("numeric", ["last_msg_id"]),diff --git a/web/styles/recent_view.css b/web/styles/recent_view.cssindex f7a01d6a3d4..4e8ad42b92c 100644--- a/web/styles/recent_view.css+++ b/web/styles/recent_view.css@@ -493,6 +493,9 @@/* Override to match td.recent_topic_name's 10px padding-left,converting to 15px/1em units used by thead th. */padding-left: 0.6667em; /* 10px at 15px/1em */+ /* th no longer has data-sort; the individual sort spans handle+ cursor and hover. */+ cursor: default;.recent-view-header-wrapper {/* Match the row grid so "Channel" and "Conversation" sort@@ -509,6 +512,38 @@auto 1fr;align-items: center;}++ .recent-view-channel-sort-header,+ .recent-view-conversation-sort-header {+ display: flex;+ align-items: center;+ gap: 4px;+ cursor: pointer;+ /* Negative margin compensates the padding so the pill hover+ highlight doesn't push adjacent elements out of alignment. */+ padding: 0.133em 0.267em; /* 2px 4px at 15px/1em */+ margin: -0.133em -0.267em;+ border-radius: 3px;+ transition: background-color 100ms ease-in-out;++ &:hover {+ background-color: var(+ --color-background-recent-view-table-thead-sort-header+ );++ .table-sortable-arrow {+ opacity: 0.7;+ }+ }++ &.active .table-sortable-arrow {+ opacity: 1;+ }++ &.descend .table-sortable-arrow {+ transform: rotate(0deg);+ }+ }}.recent-view-unread-header {diff --git a/web/templates/recent_view_table.hbs b/web/templates/recent_view_table.hbsindex 9debb769f17..6f2dde4bf26 100644--- a/web/templates/recent_view_table.hbs+++ b/web/templates/recent_view_table.hbs@@ -11,12 +11,17 @@<table class="table table-responsive"><thead id="recent-view-table-headers"><tr>- <th class="recent-view-topic-header" data-sort="conversation_sort">+ <th class="recent-view-topic-header"><div class="recent-view-header-wrapper">- <span>{{t 'Channel' }}</span>+ <span class="recent-view-channel-sort-header" data-sort="channel_sort">+ {{t 'Channel' }}+ <i class="table-sortable-arrow zulip-icon zulip-icon-sort-arrow-down"></i>+ </span><i class="zulip-icon zulip-icon-chevron-right"></i>- <span>{{t 'Conversation' }}</span>- <i class="table-sortable-arrow zulip-icon zulip-icon-sort-arrow-down"></i>+ <span class="recent-view-conversation-sort-header" data-sort="conversation_sort">+ {{t 'Conversation' }}+ <i class="table-sortable-arrow zulip-icon zulip-icon-sort-arrow-down"></i>+ </span></div></th><th data-sort="unread_sort" data-tippy-content="{{t 'Sort by unread message count' }}" class="recent-view-unread-header unread_sort tippy-zulip-delayed-tooltip hidden-for-spectators">diff --git a/web/tests/list_widget.test.cjs b/web/tests/list_widget.test.cjsindex f4cc2ff8fb4..31449e9b18b 100644--- a/web/tests/list_widget.test.cjs+++ b/web/tests/list_widget.test.cjs@@ -77,7 +77,22 @@ function make_scroll_container() {}function make_sort_container() {- const $sort_container = {cleared: false};+ const $sort_container = {cleared: false, siblings_deactivated: false};++ $sort_container.find = (selector) => {+ assert.equal(selector, "[data-sort].active");+ return {+ not($excluded) {+ assert.ok($excluded !== undefined);+ return {+ removeClass(cls) {+ assert.equal(cls, "active");+ $sort_container.siblings_deactivated = true;+ },+ };+ },+ };+ };$sort_container.on = (ev, sel, f) => {assert.equal(ev, "click.list_widget_sort");@@ -468,7 +483,7 @@ run_test("sorting", () => {$sort_container.f.apply($button);assert.ok(cleared);- assert.ok($button.siblings_deactivated);+ assert.ok($sort_container.siblings_deactivated);expected_html = html_for([alice, bob, cal, dave, ellen]);assert.deepEqual($container.$appended_data.html(), expected_html);@@ -500,12 +515,12 @@ run_test("sorting", () => {$button = sort_button(button_opts);cleared = false;- $button.siblings_deactivated = false;+ $sort_container.siblings_deactivated = false;$sort_container.f.apply($button);assert.ok(cleared);- assert.ok($button.siblings_deactivated);+ assert.ok($sort_container.siblings_deactivated);expected_html = html_for([dave, cal, bob, alice, ellen]);assert.deepEqual($container.$appended_data.html(), expected_html);@@ -587,7 +602,7 @@ run_test("Apply consecutive sorts", () => {$sort_container.f.apply($button);assert.ok(cleared);- assert.ok($button.siblings_deactivated);+ assert.ok($sort_container.siblings_deactivated);expected_html = html_for([alice, bob_2, bob, cal, cal_2, dave, ellen]);assert.deepEqual($container.$appended_data.html(), expected_html);@@ -662,6 +677,35 @@ run_test("custom sort", () => {assert.deepEqual($container.$appended_data.html(), "(6, 7)(4, 11)(1, 43)");});+run_test("handle_sort without parent_container", () => {+ // When handle_sort is called without $parent_container, it falls back+ // to using $th.siblings(".active") to deactivate other sort headers.+ const $container = make_container();+ const $scroll_container = make_scroll_container();+ $container.empty = noop;++ const widget = ListWidget.create($container, [{name: "bob"}, {name: "alice"}], {+ name: "fallback-sort-test",+ modifier_html: (item) => div(item.name),+ get_item: (item) => item,+ sort_fields: {+ ...ListWidget.generic_sort_functions("alphabetic", ["name"]),+ },+ $simplebar_container: $scroll_container,+ });++ const $button = sort_button({+ sort_type: "alphabetic",+ prop_name: "name",+ list_name: "fallback-sort-test",+ active: false,+ });++ ListWidget.handle_sort($button, widget);+ assert.ok($button.siblings_deactivated);+ assert.deepEqual($container.$appended_data.html(), "<div>alice</div><div>bob</div>");+});+run_test("clear_event_handlers", () => {const $container = make_container();const $scroll_container = make_scroll_container();diff --git a/web/tests/recent_view.test.cjs b/web/tests/recent_view.test.cjsindex 9430a31228b..06ca580f297 100644--- a/web/tests/recent_view.test.cjs+++ b/web/tests/recent_view.test.cjs@@ -71,6 +71,9 @@ const ListWidget = mock_esm("../src/list_widget", {// Just for coverage, the mechanisms// are tested in list_widget.test.cjsif (mapped_topic_values.length >= 2) {+ opts.sort_fields.channel_sort(mapped_topic_values[0], mapped_topic_values[1]);+ opts.sort_fields.channel_sort(mapped_topic_values[1], mapped_topic_values[0]);+ opts.sort_fields.channel_sort(mapped_topic_values[0], mapped_topic_values[0]);opts.sort_fields.conversation_sort(mapped_topic_values[0], mapped_topic_values[1]);opts.sort_fields.conversation_sort(mapped_topic_values[1], mapped_topic_values[0]);opts.sort_fields.conversation_sort(mapped_topic_values[0], mapped_topic_values[0]);