A platform for community discussion. Free, open, simple.
DEV: Improve chat sidebar keyboard navigation (#36500)
Keegan Georgebe6820c03455
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat.js b/plugins/chat/assets/javascripts/discourse/services/chat.jsindex f70676b84f5..46c41612791 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat.js@@ -261,93 +261,131 @@ export default class Chat extends Service {return; // Chat isn't open. Return and do nothing!}- let publicChannels, directChannels;-- if (unreadOnly) {- publicChannels =- this.chatChannelsManager.publicMessageChannelsWithActivity;- directChannels =- this.chatChannelsManager.directMessageChannelsWithActivity;-- // If the active channel has no unread messages, we need to manually insert it into- // the list, so we can find the next/previous unread channel.- if (!activeChannel.hasUnread) {- const allChannels = activeChannel.isDirectMessageChannel- ? this.chatChannelsManager.directMessageChannels- : this.chatChannelsManager.publicMessageChannels;-- // Find the ID of the channel before the active channel, which is unread- let checkChannelIndex =- allChannels.findIndex((c) => c.id === activeChannel.id) - 1;-- // If we get back to the start of the list, we can stop- while (checkChannelIndex >= 0) {- if (allChannels[checkChannelIndex].hasUnread) {- break;- }- checkChannelIndex--;- }+ // Build unified channel list matching sidebar order+ const allChannels = unreadOnly+ ? this.#getOrderedChannelsWithActivity(activeChannel)+ : this.#getOrderedChannels();- // Insert the active channel after unread channel we found (or at the start of the list)- if (activeChannel.isDirectMessageChannel) {- const unreadChannelIndex =- checkChannelIndex < 0- ? 0- : directChannels.findIndex(- (c) => c.id === allChannels[checkChannelIndex].id- );- directChannels.splice(unreadChannelIndex + 1, 0, activeChannel);- } else {- const unreadChannelIndex =- checkChannelIndex < 0- ? -1- : publicChannels.findIndex(- (c) => c.id === allChannels[checkChannelIndex].id- );- publicChannels.splice(unreadChannelIndex + 1, 0, activeChannel);- }- }- } else {- publicChannels = this.chatChannelsManager.publicMessageChannels;- directChannels = this.chatChannelsManager.directMessageChannels;- }-- let currentList, otherList;- if (activeChannel.isDirectMessageChannel) {- currentList = directChannels;- otherList = publicChannels;- } else {- currentList = publicChannels;- otherList = directChannels;+ if (allChannels.length === 0) {+ return;}const directionUp = direction === "up";- const currentChannelIndex = currentList.findIndex(+ const currentIndex = allChannels.findIndex((c) => c.id === activeChannel.id);- let nextChannelInSameList =- currentList[currentChannelIndex + (directionUp ? -1 : 1)];- if (nextChannelInSameList) {- // You're navigating in the same list of channels, just use index +- 1+ let nextIndex;+ if (currentIndex === -1) {+ // Active channel not in list, go to start/end+ nextIndex = directionUp ? allChannels.length - 1 : 0;+ } else {+ nextIndex = currentIndex + (directionUp ? -1 : 1);+ // Wrap around+ if (nextIndex < 0) {+ nextIndex = allChannels.length - 1;+ } else if (nextIndex >= allChannels.length) {+ nextIndex = 0;+ }+ }++ const nextChannel = allChannels[nextIndex];+ if (nextChannel && nextChannel.id !== activeChannel.id) {return this.router.transitionTo("chat.channel",- ...nextChannelInSameList.routeModels+ ...nextChannel.routeModels);}+ }- // You need to go to the next list of channels, if it exists.- const nextList = otherList.length ? otherList : currentList;- const nextChannel = directionUp- ? nextList[nextList.length - 1]- : nextList[0];+ /**+ * Returns channels in sidebar display order.+ * When starred channels are enabled, returns: starred, public, DMs.+ * Otherwise returns: public, DMs.+ *+ * @returns {Array} Ordered array of chat channels matching sidebar order+ */+ #getOrderedChannels() {+ const manager = this.chatChannelsManager;++ if (this.siteSettings.star_chat_channels) {+ return [+ ...manager.starredChannels,+ ...manager.unstarredPublicMessageChannels,+ ...manager.truncatedUnstarredDirectMessageChannels,+ ];+ }- if (nextChannel.id !== activeChannel.id) {- return this.router.transitionTo(- "chat.channel",- ...nextChannel.routeModels+ return [+ ...manager.publicMessageChannels,+ ...manager.truncatedDirectMessageChannels,+ ];+ }++ /**+ * Returns channels with unread activity in sidebar display order.+ * If the active channel has no unreads, inserts it at its proper position+ * relative to other channels with activity.+ *+ * @param {ChatChannel} activeChannel - The currently active channel+ * @returns {Array} Ordered array of channels with activity+ */+ #getOrderedChannelsWithActivity(activeChannel) {+ const manager = this.chatChannelsManager;++ let allChannels;+ if (this.siteSettings.star_chat_channels) {+ // Filter each section for channels with activity+ const starredWithActivity = manager.starredChannels.filter(+ (c) => c.hasUnread);+ const publicWithActivity = manager.unstarredPublicMessageChannels.filter(+ (c) => c.hasUnread+ );+ const dmsWithActivity =+ manager.truncatedUnstarredDirectMessageChannels.filter(+ (c) => c.hasUnread+ );++ allChannels = [+ ...starredWithActivity,+ ...publicWithActivity,+ ...dmsWithActivity,+ ];+ } else {+ allChannels = [+ ...manager.publicMessageChannelsWithActivity,+ ...manager.directMessageChannelsWithActivity,+ ];}++ // If the active channel has no unread messages, insert it at its proper position+ if (!activeChannel.hasUnread) {+ const orderedChannels = this.#getOrderedChannels();+ const activeIndexInOrdered = orderedChannels.findIndex(+ (c) => c.id === activeChannel.id+ );++ if (activeIndexInOrdered !== -1) {+ // Find the last unread channel before the active channel's position+ let insertAfterIndex = -1;+ for (let i = activeIndexInOrdered - 1; i >= 0; i--) {+ const channelAtPos = orderedChannels[i];+ const indexInActivity = allChannels.findIndex(+ (c) => c.id === channelAtPos.id+ );+ if (indexInActivity !== -1) {+ insertAfterIndex = indexInActivity;+ break;+ }+ }++ // Insert the active channel after the found unread channel (or at start)+ allChannels.splice(insertAfterIndex + 1, 0, activeChannel);+ }+ }++ return allChannels;}_fireOpenFloatAppEvent(channel, messageId = null) {diff --git a/plugins/chat/spec/system/shortcuts/sidebar_spec.rb b/plugins/chat/spec/system/shortcuts/sidebar_spec.rbindex ffab8dff91d..260238bcac3 100644--- a/plugins/chat/spec/system/shortcuts/sidebar_spec.rb+++ b/plugins/chat/spec/system/shortcuts/sidebar_spec.rb@@ -46,6 +46,73 @@ RSpec.describe "Shortcuts | sidebar", type: :system doexpect(sidebar_page).to have_active_channel(dm_channel_1)endend++ context "when star_chat_channels is enabled" do+ fab!(:alpha_channel) { Fabricate(:chat_channel, name: "Alpha Channel") }+ fab!(:beta_channel) { Fabricate(:chat_channel, name: "Beta Channel") }+ fab!(:other_user, :user)+ fab!(:dm_channel_2) { Fabricate(:direct_message_channel, users: [current_user, other_user]) }++ before do+ SiteSetting.star_chat_channels = true+ # Unfollow channel_1 from parent context to make tests independent+ channel_1.membership_for(current_user).update!(following: false)+ alpha_channel.add(current_user)+ beta_channel.add(current_user)+ end++ it "navigates through starred, then public, then DMs in sidebar order" do+ # Star a DM channel - this should appear in the starred section (before public channels)+ dm_channel_1.membership_for(current_user).update!(starred: true)++ chat.visit_channel(dm_channel_1)+ expect(sidebar_page).to have_active_channel(dm_channel_1)++ # Alt+Down: starred DM -> first unstarred public channel (alpha_channel)+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(alpha_channel)++ # Alt+Down: alpha_channel -> beta_channel+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(beta_channel)++ # Alt+Down: beta_channel -> unstarred DM (dm_channel_2)+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(dm_channel_2)++ # Alt+Down: dm_channel_2 -> wrap to starred (dm_channel_1)+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(dm_channel_1)++ # Alt+Up: starred DM -> wrap to last unstarred DM+ find("body").send_keys(%i[alt up])+ expect(sidebar_page).to have_active_channel(dm_channel_2)+ end++ it "navigates correctly when a public channel is starred" do+ # Star alpha_channel - it should appear in the starred section+ alpha_channel.membership_for(current_user).update!(starred: true)++ chat.visit_channel(alpha_channel)+ expect(sidebar_page).to have_active_channel(alpha_channel)++ # Alt+Down: starred public -> unstarred public (beta_channel)+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(beta_channel)++ # Alt+Down: beta_channel -> dm_channel_1+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(dm_channel_1)++ # Alt+Down: dm_channel_1 -> dm_channel_2+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(dm_channel_2)++ # Alt+Down: dm_channel_2 -> wrap to starred (alpha_channel)+ find("body").send_keys(%i[alt down])+ expect(sidebar_page).to have_active_channel(alpha_channel)+ end+ endendcontext "when using Alt+Shift+Up/Down arrows" do