From 8654757581056fe00bf00de4983c5fe642881b74 Mon Sep 17 00:00:00 2001 From: Jan Cernik <66427541+jancernik@users.noreply.github.com> Date: Tue, 30 Jan 2024 10:53:32 -0300 Subject: [PATCH] FIX: Hide 'My Threads' if no followed channels have threads (#25470) Co-authored-by: Joffrey JAFFEUX --- .../components/channels-list-public.gjs | 9 +- .../components/user-threads/index.gjs | 6 ++ .../discourse/initializers/chat-sidebar.js | 100 ++++++++++-------- .../stylesheets/common/chat-user-threads.scss | 8 ++ plugins/chat/config/locales/client.en.yml | 1 + .../page_objects/chat_drawer/chat_drawer.rb | 4 + .../system/page_objects/sidebar/sidebar.rb | 7 ++ plugins/chat/spec/system/user_threads_spec.rb | 49 +++++++-- 8 files changed, 130 insertions(+), 54 deletions(-) diff --git a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs index b501f56df39..e84851c27b4 100644 --- a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs @@ -8,6 +8,7 @@ import PluginOutlet from "discourse/components/plugin-outlet"; import concatClass from "discourse/helpers/concat-class"; import dIcon from "discourse-common/helpers/d-icon"; import i18n from "discourse-common/helpers/i18n"; +import and from "truth-helpers/helpers/and"; import ChatChannelRow from "./chat-channel-row"; export default class ChannelsListPublic extends Component { @@ -44,13 +45,19 @@ export default class ChannelsListPublic extends Component { return this.chatTrackingStateManager.hasUnreadThreads; } + get isThreadEnabledInAnyChannel() { + return this.currentUser?.chat_channels?.public_channels?.some( + (channel) => channel.threading_enabled + ); + } + @action toggleChannelSection(section) { this.args.toggleSection(section); } } diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js index 990c2ea0dc7..311fe90fe03 100644 --- a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js +++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js @@ -26,6 +26,7 @@ export default { } this.siteSettings = container.lookup("service:site-settings"); + this.currentUser = container.lookup("service:current-user"); withPluginApi("1.8.0", (api) => { api.addSidebarPanel( @@ -42,62 +43,69 @@ export default { }); withPluginApi("1.3.0", (api) => { - api.addSidebarSection( - (BaseCustomSidebarSection, BaseCustomSidebarSectionLink) => { - const SidebarChatMyThreadsSectionLink = class extends BaseCustomSidebarSectionLink { - route = "chat.threads"; - text = I18n.t("chat.my_threads.title"); - title = I18n.t("chat.my_threads.title"); - name = "user-threads"; - prefixType = "icon"; - prefixValue = "discourse-threads"; - suffixType = "icon"; - suffixCSSClass = "unread"; + const isThreadEnabledInAnyChannel = + this.currentUser?.chat_channels?.public_channels?.some( + (channel) => channel.threading_enabled === true + ); - constructor() { - super(...arguments); + if (isThreadEnabledInAnyChannel) { + api.addSidebarSection( + (BaseCustomSidebarSection, BaseCustomSidebarSectionLink) => { + const SidebarChatMyThreadsSectionLink = class extends BaseCustomSidebarSectionLink { + route = "chat.threads"; + text = I18n.t("chat.my_threads.title"); + title = I18n.t("chat.my_threads.title"); + name = "user-threads"; + prefixType = "icon"; + prefixValue = "discourse-threads"; + suffixType = "icon"; + suffixCSSClass = "unread"; - if (container.isDestroyed) { - return; + constructor() { + super(...arguments); + + if (container.isDestroyed) { + return; + } + + this.chatChannelsManager = container.lookup( + "service:chat-channels-manager" + ); } - this.chatChannelsManager = container.lookup( - "service:chat-channels-manager" - ); - } + get suffixValue() { + return this.chatChannelsManager.publicMessageChannels.some( + (channel) => channel.unreadThreadsCount > 0 + ) + ? "circle" + : ""; + } + }; - get suffixValue() { - return this.chatChannelsManager.publicMessageChannels.some( - (channel) => channel.unreadThreadsCount > 0 - ) - ? "circle" - : ""; - } - }; + const SidebarChatMyThreadsSection = class extends BaseCustomSidebarSection { + // we only show `My Threads` link + hideSectionHeader = true; - const SidebarChatMyThreadsSection = class extends BaseCustomSidebarSection { - // we only show `My Threads` link - hideSectionHeader = true; + name = "user-threads"; - name = "user-threads"; + // sidebar API doesn’t let you have undefined values + // even if you don't show the section’s header + title = ""; - // sidebar API doesn’t let you have undefined values - // even if you don't show the section’s header - title = ""; + get links() { + return [new SidebarChatMyThreadsSectionLink()]; + } - get links() { - return [new SidebarChatMyThreadsSectionLink()]; - } + get text() { + return null; + } + }; - get text() { - return null; - } - }; - - return SidebarChatMyThreadsSection; - }, - CHAT_PANEL - ); + return SidebarChatMyThreadsSection; + }, + CHAT_PANEL + ); + } if (this.siteSettings.enable_public_channels) { api.addSidebarSection( diff --git a/plugins/chat/assets/stylesheets/common/chat-user-threads.scss b/plugins/chat/assets/stylesheets/common/chat-user-threads.scss index 884ae900d09..294bfb72369 100644 --- a/plugins/chat/assets/stylesheets/common/chat-user-threads.scss +++ b/plugins/chat/assets/stylesheets/common/chat-user-threads.scss @@ -31,6 +31,14 @@ } } +.c-user-threads .empty-state-threads { + display: flex; + justify-content: center; + padding: 1.25rem 1rem; + font-size: var(--font-up-1-rem); + color: var(--primary); +} + //sidebar #sidebar-section-content-user-threads { padding-bottom: 0.5rem; diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml index 9d2ccb10b73..bbdb2ae2e6b 100644 --- a/plugins/chat/config/locales/client.en.yml +++ b/plugins/chat/config/locales/client.en.yml @@ -115,6 +115,7 @@ en: direct_message_cta: "Start a personal Chat" direct_message: "You can also start a personal chat with one or more users." title: "No channels found" + my_threads: "You don't have any threads yet. Threads you participate in will be displayed here." email_frequency: description: "We'll only email you if we haven't seen you in the last 15 minutes." never: "Never" diff --git a/plugins/chat/spec/system/page_objects/chat_drawer/chat_drawer.rb b/plugins/chat/spec/system/page_objects/chat_drawer/chat_drawer.rb index f4941072972..a9d20f84c9e 100644 --- a/plugins/chat/spec/system/page_objects/chat_drawer/chat_drawer.rb +++ b/plugins/chat/spec/system/page_objects/chat_drawer/chat_drawer.rb @@ -41,6 +41,10 @@ module PageObjects has_css?(".chat-channel-row.--threads[href='/chat/threads']") end + def has_no_user_threads_section? + has_no_css?(".chat-channel-row.--threads[href='/chat/threads']") + end + def has_unread_user_threads? has_css?(".chat-channel-row.--threads .c-unread-indicator") end diff --git a/plugins/chat/spec/system/page_objects/sidebar/sidebar.rb b/plugins/chat/spec/system/page_objects/sidebar/sidebar.rb index 6e718ae4e3d..c95bb847741 100644 --- a/plugins/chat/spec/system/page_objects/sidebar/sidebar.rb +++ b/plugins/chat/spec/system/page_objects/sidebar/sidebar.rb @@ -44,6 +44,13 @@ module PageObjects ) end + def has_no_user_threads_section? + has_no_css?( + ".sidebar-section-link[data-link-name='user-threads'][href='/chat/threads']", + text: I18n.t("js.chat.my_threads.title"), + ) + end + def has_unread_user_threads? has_css?( ".sidebar-section-link[data-link-name='user-threads'] .sidebar-section-link-suffix.icon.unread", diff --git a/plugins/chat/spec/system/user_threads_spec.rb b/plugins/chat/spec/system/user_threads_spec.rb index e244c413b94..09f985dd436 100644 --- a/plugins/chat/spec/system/user_threads_spec.rb +++ b/plugins/chat/spec/system/user_threads_spec.rb @@ -17,10 +17,27 @@ RSpec.describe "User threads", type: :system do end context "when in sidebar" do - it "shows a link to user threads" do - visit("/") + context "when user is a member of at least one channel with threads" do + before { channel_1.add(current_user) } - expect(sidebar_page).to have_user_threads_section + it "shows a link to user threads" do + visit("/") + + expect(sidebar_page).to have_user_threads_section + end + end + + context "when user is not a member of any channel with threads" do + before do + channel_1.update!(threading_enabled: false) + channel_1.add(current_user) + end + + it "does not show a link to user threads" do + visit("/") + + expect(sidebar_page).to have_no_user_threads_section + end end context "when user has unreads" do @@ -100,11 +117,29 @@ RSpec.describe "User threads", type: :system do end context "when in drawer" do - it "shows a link to user threads" do - visit("/") - chat_page.open_from_header + context "when user is a member of at least one channel with threads" do + before { channel_1.add(current_user) } - expect(drawer_page).to have_user_threads_section + it "shows a link to user threads" do + visit("/") + chat_page.open_from_header + + expect(drawer_page).to have_user_threads_section + end + end + + context "when user is not a member of any channel with threads" do + before do + channel_1.update!(threading_enabled: false) + channel_1.add(current_user) + end + + it "does not show a link to user threads" do + visit("/") + chat_page.open_from_header + + expect(drawer_page).to have_no_user_threads_section + end end context "when user has unreads" do