A platform for community discussion. Free, open, simple.
UX: Modifications for starred channels behavior (#36897)
Keegan George348defcabf15
diff --git a/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjs b/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjsindex 02598eea532..29f10d1fca0 100644--- a/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjs@@ -48,10 +48,11 @@ export default class ChannelsListDirect extends Component {}get channelList() {- if (!this.inSidebar) {+ if (this.inSidebar) {return this.chatChannelsManager.truncatedUnstarredDirectMessageChannels;}- return this.chatChannelsManager.truncatedDirectMessageChannels;+ // In mobile/drawer, show all channels including starred, sorted by activity+ return this.chatChannelsManager.truncatedDirectMessageChannelsByActivity;}@actiondiff --git a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjsindex b7bbf0ec4e8..5256050fbe9 100644--- a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs@@ -33,10 +33,11 @@ export default class ChannelsListPublic extends Component {}get channelList() {- if (!this.inSidebar) {+ if (this.inSidebar) {return this.chatChannelsManager.unstarredPublicMessageChannelsByActivity;}- return this.chatChannelsManager.publicMessageChannelsByActivity;+ // In mobile/drawer, show all channels including starred, sorted by activity+ return this.chatChannelsManager.allPublicChannelsByActivity;}@actiondiff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.jsindex c37017838b8..2101d32b7fe 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js@@ -179,6 +179,18 @@ export default class ChatChannelsManager extends Service {return this.#sortChannelsByActivity([...this.publicMessageChannels]);}+ /**+ * Returns all public message channels sorted by activity (unreads first).+ * Used in mobile/drawer mode where starred channels appear in both tabs.+ *+ * @returns {ChatChannel[]} Array of public channels sorted by activity+ */+ get allPublicChannelsByActivity() {+ return this.#sortChannelsByActivityUnreadsFirst([+ ...this.publicMessageChannels,+ ]);+ }+/*** Returns public message channels that are not starred, sorted by activity.* Falls back to all public channels by activity if starring is disabled.@@ -215,6 +227,28 @@ export default class ChatChannelsManager extends Service {return this.directMessageChannels.slice(0, DIRECT_MESSAGE_CHANNELS_LIMIT);}+ /**+ * Returns all direct message channels sorted by activity (unreads first).+ * Used in mobile/drawer mode where starred channels appear in both tabs.+ *+ * @returns {ChatChannel[]} Array of DM channels sorted by activity+ */+ get directMessageChannelsByActivity() {+ return this.#sortDirectMessageChannelsByActivity(+ this.channels.filter((channel) => {+ const membership = channel.currentUserMembership;+ return channel.isDirectMessageChannel && membership?.following;+ })+ );+ }++ get truncatedDirectMessageChannelsByActivity() {+ return this.directMessageChannelsByActivity.slice(+ 0,+ DIRECT_MESSAGE_CHANNELS_LIMIT+ );+ }+/*** Returns all channels (public and DM) that the current user has starred.* Public channels are sorted alphabetically by slug, DMs by title.@@ -471,46 +505,56 @@ export default class ChatChannelsManager extends Service {#sortChannelsByActivity(channels) {return channels.sort(this.#withStarredPriority("slug", (a, b) => {- const stats = {- a: {- urgent:- a.tracking.mentionCount + a.tracking.watchedThreadsUnreadCount,- unread:- a.tracking.unreadCount + a.unreadThreadsCountSinceLastViewed,- },- b: {- urgent:- b.tracking.mentionCount + b.tracking.watchedThreadsUnreadCount,- unread:- b.tracking.unreadCount + b.unreadThreadsCountSinceLastViewed,- },- };-- // if both channels have urgent count, sort by slug- // otherwise prioritize channel with urgent count- if (stats.a.urgent > 0 && stats.b.urgent > 0) {- return a.slug?.localeCompare?.(b.slug);- }-- if (stats.a.urgent > 0 || stats.b.urgent > 0) {- return stats.a.urgent > stats.b.urgent ? -1 : 1;- }-- // if both channels have unread messages or threads, sort by slug- // otherwise prioritize channel with unread count- if (stats.a.unread > 0 && stats.b.unread > 0) {- return a.slug?.localeCompare?.(b.slug);- }-- if (stats.a.unread > 0 || stats.b.unread > 0) {- return stats.a.unread > stats.b.unread ? -1 : 1;- }-- return a.slug?.localeCompare?.(b.slug);+ return this.#compareChannelsByActivity(a, b);}));}+ /**+ * Sorts public channels by activity without starred priority.+ * Unreads come first regardless of starred status.+ */+ #sortChannelsByActivityUnreadsFirst(channels) {+ return channels.sort((a, b) => {+ return this.#compareChannelsByActivity(a, b);+ });+ }++ #compareChannelsByActivity(a, b) {+ const stats = {+ a: {+ urgent: a.tracking.mentionCount + a.tracking.watchedThreadsUnreadCount,+ unread: a.tracking.unreadCount + a.unreadThreadsCountSinceLastViewed,+ },+ b: {+ urgent: b.tracking.mentionCount + b.tracking.watchedThreadsUnreadCount,+ unread: b.tracking.unreadCount + b.unreadThreadsCountSinceLastViewed,+ },+ };++ // if both channels have urgent count, sort by slug+ // otherwise prioritize channel with urgent count+ if (stats.a.urgent > 0 && stats.b.urgent > 0) {+ return a.slug?.localeCompare?.(b.slug);+ }++ if (stats.a.urgent > 0 || stats.b.urgent > 0) {+ return stats.a.urgent > stats.b.urgent ? -1 : 1;+ }++ // if both channels have unread messages or threads, sort by slug+ // otherwise prioritize channel with unread count+ if (stats.a.unread > 0 && stats.b.unread > 0) {+ return a.slug?.localeCompare?.(b.slug);+ }++ if (stats.a.unread > 0 || stats.b.unread > 0) {+ return stats.a.unread > stats.b.unread ? -1 : 1;+ }++ return a.slug?.localeCompare?.(b.slug);+ }+#sortChannelsByProperty(channels, property) {return channels.sort(this.#withStarredPriority(property, (a, b) => {@@ -522,56 +566,69 @@ export default class ChatChannelsManager extends Service {#sortDirectMessageChannels(channels) {return channels.sort(this.#withStarredPriority("title", (a, b) => {- if (!a.lastMessage.id) {- return 1;- }-- if (!b.lastMessage.id) {- return -1;- }-- const aUrgent =- a.tracking.unreadCount +- a.tracking.mentionCount +- a.tracking.watchedThreadsUnreadCount;-- const bUrgent =- b.tracking.unreadCount +- b.tracking.mentionCount +- b.tracking.watchedThreadsUnreadCount;-- const aUnread = a.unreadThreadsCountSinceLastViewed;- const bUnread = b.unreadThreadsCountSinceLastViewed;-- // if both channels have urgent count, sort by last message date- if (aUrgent > 0 && bUrgent > 0) {- return new Date(a.lastMessage.createdAt) >- new Date(b.lastMessage.createdAt)- ? -1- : 1;- }-- // otherwise prioritize channel with urgent count- if (aUrgent > 0 || bUrgent > 0) {- return aUrgent > bUrgent ? -1 : 1;- }-- // if both channels have unread threads, sort by last thread reply date- if (aUnread > 0 && bUnread > 0) {- return a.lastUnreadThreadDate > b.lastUnreadThreadDate ? -1 : 1;- }-- // otherwise prioritize channel with unread thread count- if (aUnread > 0 || bUnread > 0) {- return aUnread > bUnread ? -1 : 1;- }-- // read channels are sorted by last message date- return new Date(a.lastMessage.createdAt) >- new Date(b.lastMessage.createdAt)- ? -1- : 1;+ return this.#compareDirectMessageChannelsByActivity(a, b);}));}++ /**+ * Sorts DM channels by activity without starred priority.+ * Unreads come first regardless of starred status.+ */+ #sortDirectMessageChannelsByActivity(channels) {+ return channels.sort((a, b) => {+ return this.#compareDirectMessageChannelsByActivity(a, b);+ });+ }++ #compareDirectMessageChannelsByActivity(a, b) {+ if (!a.lastMessage.id) {+ return 1;+ }++ if (!b.lastMessage.id) {+ return -1;+ }++ const aUrgent =+ a.tracking.unreadCount ++ a.tracking.mentionCount ++ a.tracking.watchedThreadsUnreadCount;++ const bUrgent =+ b.tracking.unreadCount ++ b.tracking.mentionCount ++ b.tracking.watchedThreadsUnreadCount;++ const aUnread = a.unreadThreadsCountSinceLastViewed;+ const bUnread = b.unreadThreadsCountSinceLastViewed;++ // if both channels have urgent count, sort by last message date+ if (aUrgent > 0 && bUrgent > 0) {+ return new Date(a.lastMessage.createdAt) >+ new Date(b.lastMessage.createdAt)+ ? -1+ : 1;+ }++ // otherwise prioritize channel with urgent count+ if (aUrgent > 0 || bUrgent > 0) {+ return aUrgent > bUrgent ? -1 : 1;+ }++ // if both channels have unread threads, sort by last thread reply date+ if (aUnread > 0 && bUnread > 0) {+ return a.lastUnreadThreadDate > b.lastUnreadThreadDate ? -1 : 1;+ }++ // otherwise prioritize channel with unread thread count+ if (aUnread > 0 || bUnread > 0) {+ return aUnread > bUnread ? -1 : 1;+ }++ // read channels are sorted by last message date+ return new Date(a.lastMessage.createdAt) > new Date(b.lastMessage.createdAt)+ ? -1+ : 1;+ }}diff --git a/plugins/chat/spec/system/drawer/starred_channels_spec.rb b/plugins/chat/spec/system/drawer/starred_channels_spec.rbindex 51e1ac5951f..32ea4e28a54 100644--- a/plugins/chat/spec/system/drawer/starred_channels_spec.rb+++ b/plugins/chat/spec/system/drawer/starred_channels_spec.rb@@ -70,6 +70,97 @@ RSpec.describe "Drawer - starred channels", type: :system doendend+ context "when a channel is starred" do+ fab!(:user_1) { Fabricate(:user, username: "alice") }++ before { channel_1.membership_for(current_user).update!(starred: true) }++ it "shows the starred channel in both the starred tab AND the channels tab" do+ visit("/")+ chat_page.open_from_header+ drawer_page.click_starred_channels++ expect(page).to have_css(+ ".starred-channels .chat-channel-row[data-chat-channel-id='#{channel_1.id}']",+ )++ drawer_page.click_channels++ expect(page).to have_css(+ ".public-channels .chat-channel-row[data-chat-channel-id='#{channel_1.id}']",+ )+ end++ it "shows unread indicator on channel row in both tabs when channel has unreads" do+ Fabricate(:chat_message, chat_channel: channel_1, user: user_1)+ channel_1.membership_for(current_user).update!(last_viewed_at: 1.minute.ago)++ visit("/")+ chat_page.open_from_header+ drawer_page.click_starred_channels++ expect(page).to have_css(+ ".starred-channels .chat-channel-row[data-chat-channel-id='#{channel_1.id}'] .chat-channel-unread-indicator",+ )++ drawer_page.click_channels++ expect(page).to have_css(+ ".public-channels .chat-channel-row[data-chat-channel-id='#{channel_1.id}'] .chat-channel-unread-indicator",+ )+ end++ it "only shows footer unread indicator on starred tab, not on channels tab" do+ Fabricate(+ :chat_message,+ chat_channel: channel_1,+ user: user_1,+ message: "hey @#{current_user.username}",+ )+ channel_1.membership_for(current_user).update!(last_viewed_at: 1.minute.ago)++ visit("/")+ chat_page.open_from_header++ expect(page).to have_css("#c-footer-starred .c-unread-indicator")+ expect(page).to have_no_css("#c-footer-channels .c-unread-indicator")+ end+ end++ context "when a DM channel is starred" do+ fab!(:user_1) { Fabricate(:user, username: "alice") }+ fab!(:dm_channel) { Fabricate(:direct_message_channel, users: [current_user, user_1]) }++ before { dm_channel.membership_for(current_user).update!(starred: true) }++ it "shows the starred DM in both the starred tab AND the DMs tab" do+ visit("/")+ chat_page.open_from_header+ drawer_page.click_starred_channels++ expect(page).to have_css(+ ".starred-channels .chat-channel-row[data-chat-channel-id='#{dm_channel.id}']",+ )++ drawer_page.click_direct_messages++ expect(page).to have_css(+ ".direct-message-channels .chat-channel-row[data-chat-channel-id='#{dm_channel.id}']",+ )+ end++ it "only shows footer unread indicator on starred tab, not on DMs tab" do+ Fabricate(:chat_message, chat_channel: dm_channel, user: user_1)+ dm_channel.membership_for(current_user).update!(last_viewed_at: 1.minute.ago)++ visit("/")+ chat_page.open_from_header++ expect(page).to have_css("#c-footer-starred .c-unread-indicator")+ expect(page).to have_no_css("#c-footer-direct-messages .c-unread-indicator")+ end+ end+context "when navigating back from a channel" dobefore { channel_1.membership_for(current_user).update!(starred: true) }