Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: AI header icon should remember last URL (#34108)

Kris98d594a7b3e5
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/header/icon.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/header/icon.gjs
index 6e97f1fa6ad..5986b8513ce 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat/header/icon.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat/header/icon.gjs
@@ -43,7 +43,7 @@ export default class ChatHeaderIcon extends Component {
!this.chatSeparateSidebarMode.never &&
this.site.desktopView
) {
- return i18n("sidebar.panels.forum.label");
+ return i18n("chat.exit");
}
return i18n("chat.title_capitalized");
diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml
index ca5534460e6..f453d4b5f83 100644
--- a/plugins/chat/config/locales/client.en.yml
+++ b/plugins/chat/config/locales/client.en.yml
@@ -303,7 +303,7 @@ en:
one: "%{count} file"
other: "%{count} files"
you_flagged: "You flagged this message"
- exit: "back"
+ exit: "exit chat"
channel_status:
read_only_header: "Channel is read only"
read_only: "Read Only"
diff --git a/plugins/chat/test/javascripts/components/chat-header-icon-test.gjs b/plugins/chat/test/javascripts/components/chat-header-icon-test.gjs
index 75d127ef9ba..917509ba982 100644
--- a/plugins/chat/test/javascripts/components/chat-header-icon-test.gjs
+++ b/plugins/chat/test/javascripts/components/chat-header-icon-test.gjs
@@ -36,7 +36,7 @@ module("Discourse Chat | Component | chat-header-icon", function (hooks) {
assert
.dom(".icon.btn-flat")
- .hasAttribute("title", i18n("sidebar.panels.forum.label"))
+ .hasAttribute("title", i18n("chat.exit"))
.hasAttribute("href", "/latest");
assert.dom(".d-icon-shuffle").exists();
@@ -68,7 +68,7 @@ module("Discourse Chat | Component | chat-header-icon", function (hooks) {
assert
.dom(".icon.btn-flat")
- .hasAttribute("title", i18n("sidebar.panels.forum.label"))
+ .hasAttribute("title", i18n("chat.exit"))
.hasAttribute("href", "/latest");
assert.dom(".d-icon-shuffle").exists();
assert.dom(".chat-channel-unread-indicator__number").doesNotExist();
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjs
index b00e27de29f..5abb57deadb 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjs
@@ -4,7 +4,7 @@ import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import PluginOutlet from "discourse/components/plugin-outlet";
import lazyHash from "discourse/helpers/lazy-hash";
-import { defaultHomepage } from "discourse/lib/utilities";
+import getURL from "discourse/lib/get-url";
import { i18n } from "discourse-i18n";
import { composeAiBotMessage } from "../lib/ai-bot-helper";
import { AI_CONVERSATIONS_PANEL } from "../services/ai-conversations-sidebar-manager";
@@ -17,6 +17,7 @@ export default class AiBotHeaderIcon extends Component {
@service router;
@service sidebarState;
@service siteSettings;
+ @service aiConversationsSidebarManager;
get bots() {
const availableBots = this.currentUser.ai_enabled_chat_bots
@@ -30,6 +31,14 @@ export default class AiBotHeaderIcon extends Component {
return this.bots.length > 0 && this.siteSettings.ai_bot_add_to_header;
}
+ get title() {
+ if (this.clickShouldRouteOutOfConversations) {
+ return i18n("discourse_ai.ai_bot.exit");
+ }
+
+ return i18n("discourse_ai.ai_bot.shortcut_title");
+ }
+
get icon() {
if (this.clickShouldRouteOutOfConversations) {
return "shuffle";
@@ -45,18 +54,30 @@ export default class AiBotHeaderIcon extends Component {
);
}
- @action
- onClick() {
+ get href() {
if (this.clickShouldRouteOutOfConversations) {
- return this.router.transitionTo(`discovery.${defaultHomepage()}`);
+ return getURL(this.aiConversationsSidebarManager.lastKnownAppURL || "/");
}
if (this.siteSettings.ai_bot_enable_dedicated_ux) {
- this.appEvents.trigger("discourse-ai:bot-header-icon-clicked");
- return this.router.transitionTo("discourse-ai-bot-conversations");
+ return getURL("/discourse-ai/ai-bot/conversations");
}
- composeAiBotMessage(this.bots[0], this.composer);
+ return null;
+ }
+
+ @action
+ onClick() {
+ if (!this.siteSettings.ai_bot_enable_dedicated_ux) {
+ composeAiBotMessage(this.bots[0], this.composer);
+ }
+
+ if (
+ this.siteSettings.ai_bot_enable_dedicated_ux &&
+ !this.clickShouldRouteOutOfConversations
+ ) {
+ this.appEvents.trigger("discourse-ai:bot-header-icon-clicked");
+ }
}
<template>
@@ -67,9 +88,10 @@ export default class AiBotHeaderIcon extends Component {
@outletArgs={{lazyHash onClick=this.onClick icon=this.icon}}
>
<DButton
- @action={{this.onClick}}
+ @href={{this.href}}
+ @action={{unless this.href this.onClick}}
@icon={{this.icon}}
- title={{i18n "discourse_ai.ai_bot.shortcut_title"}}
+ title={{this.title}}
class="ai-bot-button icon btn-flat"
/>
</PluginOutlet>
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
index d98a97993e2..9655377eb5e 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
@@ -7,6 +7,7 @@ import { ajax } from "discourse/lib/ajax";
import discourseDebounce from "discourse/lib/debounce";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
import { ADMIN_PANEL, MAIN_PANEL } from "discourse/lib/sidebar/panels";
+import { defaultHomepage } from "discourse/lib/utilities";
import { i18n } from "discourse-i18n";
import AiBotSidebarEmptyState from "../components/ai-bot-sidebar-empty-state";
@@ -18,6 +19,8 @@ export default class AiConversationsSidebarManager extends Service {
@service appEvents;
@service sidebarState;
@service messageBus;
+ @service routeHistory;
+ @service router;
@tracked topics = [];
@tracked sections = new TrackedArray();
@@ -161,6 +164,14 @@ export default class AiConversationsSidebarManager extends Service {
this._removeScrollListener();
}
+ get lastKnownAppURL() {
+ const lastForumUrl = this.routeHistory.history.find((url) => {
+ return !url.startsWith("/discourse-ai");
+ });
+
+ return lastForumUrl || this.router.urlFor(`discovery.${defaultHomepage()}`);
+ }
+
async fetchMessages() {
if (this.isFetching || !this.hasMore) {
return;
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 619d9e85919..9e45c76d29d 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -801,6 +801,7 @@ en:
cancel_streaming: "Stop reply"
default_pm_prefix: "[Untitled AI bot PM]"
shortcut_title: "Start a PM with an AI bot"
+ exit: "exit AI bot"
share: "Copy AI conversation"
conversation_shared: "Conversation copied"
embed_copied: "Embed copied to clipboard"
diff --git a/plugins/discourse-ai/spec/system/ai_bot/header_toggle_spec.rb b/plugins/discourse-ai/spec/system/ai_bot/header_toggle_spec.rb
new file mode 100644
index 00000000000..2cf6e778844
--- /dev/null
+++ b/plugins/discourse-ai/spec/system/ai_bot/header_toggle_spec.rb
@@ -0,0 +1,65 @@
+# frozen_string_literal: true
+
+RSpec.describe "AI Bot - Header Toggle", type: :system do
+ let(:topic_page) { PageObjects::Pages::Topic.new }
+ let(:ai_pm_homepage) { PageObjects::Components::AiPmHomepage.new }
+ let(:header) { PageObjects::Pages::DiscourseAi::Header.new }
+
+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:group)
+ fab!(:regular_topic) { Fabricate(:topic) }
+
+ fab!(:gpt_4) { Fabricate(:llm_model, name: "gpt-4") }
+ fab!(:gpt_3_5_turbo) { Fabricate(:llm_model, name: "gpt-3.5-turbo") }
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_bot_enabled = true
+ toggle_enabled_bots(bots: [gpt_4, gpt_3_5_turbo])
+ SiteSetting.ai_bot_allowed_groups = group.id.to_s
+ SiteSetting.ai_bot_enable_dedicated_ux = true
+ SiteSetting.ai_bot_add_to_header = true
+ SiteSetting.navigation_menu = "sidebar"
+
+ group.add(user)
+ group.save
+
+ allowed_persona = AiPersona.last
+ allowed_persona.update!(allowed_group_ids: [group.id], enabled: true)
+
+ sign_in(user)
+ end
+
+ it "remembers the last forum URL and returns to it when toggling" do
+ visit "/hot"
+
+ header.click_bot_button
+ expect(ai_pm_homepage).to have_homepage
+ expect(header).to have_icon_in_bot_button(icon: "shuffle")
+
+ header.click_bot_button
+ expect(header).to have_icon_in_bot_button(icon: "robot")
+ expect(page).to have_current_path("/hot")
+ end
+
+ it "updates stored forum URL when navigating between forum pages" do
+ visit "/hot"
+
+ header.click_bot_button
+ expect(ai_pm_homepage).to have_homepage
+
+ header.click_bot_button
+ expect(header).to have_icon_in_bot_button(icon: "robot")
+ expect(page).to have_current_path("/hot")
+
+ visit "/categories"
+ expect(page).to have_current_path("/categories")
+
+ header.click_bot_button
+ expect(ai_pm_homepage).to have_homepage
+
+ header.click_bot_button
+ expect(header).to have_icon_in_bot_button(icon: "robot")
+ expect(page).to have_current_path("/categories")
+ end
+end
diff --git a/plugins/discourse-ai/spec/system/page_objects/pages/discourse_ai/header.rb b/plugins/discourse-ai/spec/system/page_objects/pages/discourse_ai/header.rb
index 9a3e956a5af..5a5904f7eb7 100644
--- a/plugins/discourse-ai/spec/system/page_objects/pages/discourse_ai/header.rb
+++ b/plugins/discourse-ai/spec/system/page_objects/pages/discourse_ai/header.rb
@@ -5,11 +5,11 @@ module PageObjects
module DiscourseAi
class Header < ::PageObjects::Pages::Header
def click_bot_button
- find(".ai-bot-button").click
+ find("header .ai-bot-button").click
end
def has_icon_in_bot_button?(icon:)
- page.has_css?(".ai-bot-button .d-icon-#{icon}")
+ page.has_css?("header .ai-bot-button .d-icon-#{icon}")
end
end
end