Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 86f82ea

Browse files
authored
FIX: Do not re-render conversations sidebar on navigation (#1274)
1 parent d2002f8 commit 86f82ea

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

assets/javascripts/discourse/components/ai-bot-header-icon.gjs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import Component from "@glimmer/component";
22
import { action } from "@ember/object";
33
import { service } from "@ember/service";
44
import DButton from "discourse/components/d-button";
5+
import { defaultHomepage } from "discourse/lib/utilities";
56
import { i18n } from "discourse-i18n";
67
import { composeAiBotMessage } from "../lib/ai-bot-helper";
8+
import { AI_CONVERSATIONS_PANEL } from "../services/ai-conversations-sidebar-manager";
79

810
export default class AiBotHeaderIcon extends Component {
9-
@service currentUser;
10-
@service siteSettings;
1111
@service composer;
12+
@service currentUser;
1213
@service router;
14+
@service sidebarState;
15+
@service siteSettings;
1316

1417
get bots() {
1518
const availableBots = this.currentUser.ai_enabled_chat_bots
@@ -23,20 +26,39 @@ export default class AiBotHeaderIcon extends Component {
2326
return this.bots.length > 0 && this.siteSettings.ai_bot_add_to_header;
2427
}
2528

29+
get icon() {
30+
if (this.clickShouldRouteOutOfConversations) {
31+
return "shuffle";
32+
}
33+
return "robot";
34+
}
35+
36+
get clickShouldRouteOutOfConversations() {
37+
return (
38+
this.siteSettings.ai_enable_experimental_bot_ux &&
39+
this.sidebarState.currentPanel?.key === AI_CONVERSATIONS_PANEL
40+
);
41+
}
42+
2643
@action
27-
compose() {
44+
onClick() {
45+
if (this.clickShouldRouteOutOfConversations) {
46+
return this.router.transitionTo(`discovery.${defaultHomepage()}`);
47+
}
48+
2849
if (this.siteSettings.ai_enable_experimental_bot_ux) {
2950
return this.router.transitionTo("discourse-ai-bot-conversations");
3051
}
52+
3153
composeAiBotMessage(this.bots[0], this.composer);
3254
}
3355

3456
<template>
3557
{{#if this.showHeaderButton}}
3658
<li>
3759
<DButton
38-
@action={{this.compose}}
39-
@icon="robot"
60+
@action={{this.onClick}}
61+
@icon={{this.icon}}
4062
title={{i18n "discourse_ai.ai_bot.shortcut_title"}}
4163
class="ai-bot-button icon btn-flat"
4264
/>

assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ export default class AiConversationsSidebarManager extends Service {
1010
@tracked newTopicForceSidebar = false;
1111

1212
forceCustomSidebar() {
13-
// Set the panel to your custom panel
13+
// Return early if we already have the correct panel, so we don't
14+
// re-render it.
15+
if (this.sidebarState.currentPanel?.key === AI_CONVERSATIONS_PANEL) {
16+
return;
17+
}
18+
1419
this.sidebarState.setPanel(AI_CONVERSATIONS_PANEL);
1520

1621
// Use separated mode to ensure independence from hamburger menu

spec/system/ai_bot/homepage_spec.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
let(:topic_page) { PageObjects::Pages::Topic.new }
55
let(:composer) { PageObjects::Components::Composer.new }
66
let(:ai_pm_homepage) { PageObjects::Components::AiPmHomepage.new }
7+
let(:header) { PageObjects::Pages::DiscourseAi::Header.new }
78
let(:sidebar) { PageObjects::Components::NavigationMenu::Sidebar.new }
89
let(:header_dropdown) { PageObjects::Components::NavigationMenu::HeaderDropdown.new }
910
let(:dialog) { PageObjects::Components::Dialog.new }
@@ -92,14 +93,14 @@
9293
context "when `ai_enable_experimental_bot_ux` is enabled" do
9394
it "renders landing page on bot click" do
9495
visit "/"
95-
find(".ai-bot-button").click
96+
header.click_bot_button
9697
expect(ai_pm_homepage).to have_homepage
9798
expect(sidebar).to be_visible
9899
end
99100

100101
it "displays error when message is too short" do
101102
visit "/"
102-
find(".ai-bot-button").click
103+
header.click_bot_button
103104

104105
ai_pm_homepage.input.fill_in(with: "a")
105106
ai_pm_homepage.submit
@@ -111,15 +112,15 @@
111112
it "renders sidebar even when navigation menu is set to header" do
112113
SiteSetting.navigation_menu = "header dropdown"
113114
visit "/"
114-
find(".ai-bot-button").click
115+
header.click_bot_button
115116
expect(ai_pm_homepage).to have_homepage
116117
expect(sidebar).to be_visible
117118
expect(header_dropdown).to be_visible
118119
end
119120

120121
it "hides default content in the sidebar" do
121122
visit "/"
122-
find(".ai-bot-button").click
123+
header.click_bot_button
123124

124125
expect(ai_pm_homepage).to have_homepage
125126
expect(sidebar).to have_no_tags_section
@@ -132,7 +133,7 @@
132133

133134
it "shows the bot conversation in the sidebar" do
134135
visit "/"
135-
find(".ai-bot-button").click
136+
header.click_bot_button
136137

137138
expect(ai_pm_homepage).to have_homepage
138139
expect(sidebar).to have_section("ai-conversations-history")
@@ -142,7 +143,7 @@
142143

143144
it "navigates to the bot conversation when clicked" do
144145
visit "/"
145-
find(".ai-bot-button").click
146+
header.click_bot_button
146147

147148
expect(ai_pm_homepage).to have_homepage
148149
sidebar.find(
@@ -151,6 +152,24 @@
151152
expect(topic_page).to have_topic_title(pm.title)
152153
end
153154

155+
it "displays the shuffle icon when on homepage or bot PM" do
156+
visit "/"
157+
expect(header).to have_icon_in_bot_button(icon: "robot")
158+
header.click_bot_button
159+
160+
expect(header).to have_icon_in_bot_button(icon: "shuffle")
161+
162+
# Go to a PM and assert that the icon is still shuffle
163+
sidebar.find(
164+
".sidebar-section[data-section-name='ai-conversations-history'] a.sidebar-section-link",
165+
).click
166+
expect(header).to have_icon_in_bot_button(icon: "shuffle")
167+
168+
# Go back home and assert that the icon is now robot again
169+
header.click_bot_button
170+
expect(header).to have_icon_in_bot_button(icon: "robot")
171+
end
172+
154173
it "displays sidebar and 'new question' on the topic page" do
155174
topic_page.visit_topic(pm)
156175
expect(sidebar).to be_visible
@@ -193,7 +212,7 @@
193212
sign_in(user_2)
194213

195214
visit "/"
196-
find(".ai-bot-button").click
215+
header.click_bot_button
197216
expect(ai_pm_homepage).to have_homepage
198217
expect(sidebar).to have_no_section_link(pm.title)
199218
end
@@ -204,7 +223,7 @@
204223

205224
it "opens composer on bot click" do
206225
visit "/"
207-
find(".ai-bot-button").click
226+
header.click_bot_button
208227

209228
expect(ai_pm_homepage).to have_no_homepage
210229
expect(composer).to be_opened
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module PageObjects
4+
module Pages
5+
module DiscourseAi
6+
class Header < ::PageObjects::Pages::Header
7+
def click_bot_button
8+
find(".ai-bot-button").click
9+
end
10+
11+
def has_icon_in_bot_button?(icon:)
12+
page.has_css?(".ai-bot-button .d-icon-#{icon}")
13+
end
14+
end
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)