A platform for community discussion. Free, open, simple.
FEATURE: Add toggle for PM gists to Discourse AI (#34107)
Kris383760759b5a
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjsindex 440f0cda88e..fdbbd259ddf 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs@@ -9,6 +9,7 @@ import bodyClass from "discourse/helpers/body-class";import icon from "discourse/helpers/d-icon";import { i18n } from "discourse-i18n";import DMenu from "float-kit/components/d-menu";+import { TABLE_AI_LAYOUT, TABLE_LAYOUT } from "../services/gists";export default class AiGistToggle extends Component {@service gists;@@ -16,12 +17,12 @@ export default class AiGistToggle extends Component {get buttons() {return [{- id: "table",+ id: TABLE_LAYOUT,label: "discourse_ai.summarization.topic_list_layout.button.compact",icon: "discourse-table",},{- id: "table-ai",+ id: TABLE_AI_LAYOUT,label: "discourse_ai.summarization.topic_list_layout.button.expanded",icon: "discourse-table-sparkles",description:@@ -31,7 +32,7 @@ export default class AiGistToggle extends Component {}get selectedOptionId() {- return this.gists.get("preference");+ return this.gists.currentPreference;}get currentButton() {@@ -48,13 +49,13 @@ export default class AiGistToggle extends Component {@actiononSelect(optionId) {- this.gists.setPreference(optionId);+ this.gists.setPreference(optionId, this.gists.isPm);this.dMenu.close();}<template>- {{#if this.gists.shouldShow}}- {{bodyClass (concat "topic-list-layout-" this.gists.preference)}}+ {{#if this.gists.showToggle}}+ {{bodyClass (concat "topic-list-layout-" this.gists.currentPreference)}}<DMenu@modalForMobile={{true}}@autofocus={{true}}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjsindex c2fed1a4178..d01fe693a05 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjs@@ -1,12 +1,15 @@import Component from "@glimmer/component";import { service } from "@ember/service";import { htmlSafe } from "@ember/template";+import { TABLE_AI_LAYOUT } from "../services/gists";export default class AiTopicGist extends Component {@service gists;get shouldShow() {- return this.gists.preference === "table-ai" && this.gists.shouldShow;+ return (+ this.gists.currentPreference === TABLE_AI_LAYOUT && this.gists.showToggle+ );}get hasGist() {diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-list-before-category/ai-topic-gist-placement.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-list-topic-cell-link-bottom-line__before/ai-topic-gist-placement.gjssimilarity index 100%rename from plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-list-before-category/ai-topic-gist-placement.gjsrename to plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-list-topic-cell-link-bottom-line__before/ai-topic-gist-placement.gjsdiff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/user-messages-controls-bottom/pm-list-gist-toggle.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/user-messages-controls-bottom/pm-list-gist-toggle.gjsnew file mode 100644index 00000000000..5358ec8ce87--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/user-messages-controls-bottom/pm-list-gist-toggle.gjs@@ -0,0 +1,13 @@+import Component from "@glimmer/component";+import { service } from "@ember/service";+import AiGistToggle from "../../components/ai-gist-toggle";++export default class AiTopicGist extends Component {+ @service topicThumbnails; // avoid Topic Thumbnails theme component++ get shouldShow() {+ return !this.topicThumbnails?.enabledForRoute;+ }++ <template><AiGistToggle /></template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/gists.js b/plugins/discourse-ai/assets/javascripts/discourse/services/gists.jsindex de939bcd68c..c1e8c9c9f88 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/services/gists.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/gists.js@@ -1,23 +1,50 @@import { tracked } from "@glimmer/tracking";import Service, { service } from "@ember/service";+const TOPIC_LIST_LAYOUT_KEY = "topicListLayout";+const PM_TOPIC_LIST_LAYOUT_KEY = "pmTopicListLayout";++export const TABLE_LAYOUT = "table";+export const TABLE_AI_LAYOUT = "table-ai";+export default class Gists extends Service {@service router;- @tracked preference = localStorage.getItem("topicListLayout");+ @tracked preference = localStorage.getItem(TOPIC_LIST_LAYOUT_KEY);+ @tracked pmPreference = localStorage.getItem(PM_TOPIC_LIST_LAYOUT_KEY);++ get routerAttributes() {+ return this.router.currentRoute.attributes;+ }++ get publicTopics() {+ return this.routerAttributes?.list?.topics;+ }- get shouldShow() {- return this.router.currentRoute.attributes?.list?.topics?.some(- (topic) => topic.ai_topic_gist- );+ get pmTopics() {+ return this.routerAttributes?.topics;}- setPreference(value) {- this.preference = value;- localStorage.setItem("topicListLayout", value);+ get isPm() {+ return !this.publicTopics && this.pmTopics;+ }++ get showToggle() {+ const topicList = this.publicTopics ?? this.pmTopics;+ return topicList?.some((topic) => topic.ai_topic_gist);+ }++ get currentPreference() {+ return this.isPm ? this.pmPreference : this.preference;+ }- if (this.preference === "table-ai") {- localStorage.setItem("aiPreferred", true);+ setPreference(value, isPm = false) {+ if (isPm) {+ this.pmPreference = value;+ localStorage.setItem(PM_TOPIC_LIST_LAYOUT_KEY, value);+ } else {+ this.preference = value;+ localStorage.setItem(TOPIC_LIST_LAYOUT_KEY, value);}}}diff --git a/plugins/discourse-ai/assets/stylesheets/modules/summarization/common/ai-gists.scss b/plugins/discourse-ai/assets/stylesheets/modules/summarization/common/ai-gists.scssindex d7c7a398bdc..eeab9f786fc 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/summarization/common/ai-gists.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/summarization/common/ai-gists.scss@@ -39,8 +39,7 @@.excerpt {width: 100%;line-height: var(--line-height-large);- margin-top: 0.15em;- margin-bottom: 0.15em;+ margin-bottom: var(--space-2);color: currentcolor;&__contents {@@ -72,3 +71,10 @@}}}++.user-navigation-secondary {+ .topic-list-layout-trigger {+ // put PM toggle before new PM button+ order: -1;+ }+}diff --git a/plugins/discourse-ai/spec/system/summarization/gists_toggle_spec.rb b/plugins/discourse-ai/spec/system/summarization/gists_toggle_spec.rbnew file mode 100644index 00000000000..7a609bda3a5--- /dev/null+++ b/plugins/discourse-ai/spec/system/summarization/gists_toggle_spec.rb@@ -0,0 +1,96 @@+# frozen_string_literal: true++describe "Gists Toggle Functionality", type: :system do+ fab!(:admin)+ fab!(:group)+ fab!(:topic_with_gist) { Fabricate(:topic) }+ fab!(:topic_ai_gist) { Fabricate(:topic_ai_gist, target: topic_with_gist) }++ before do+ enable_current_plugin+ assign_fake_provider_to(:ai_default_llm_model)+ SiteSetting.ai_summarization_enabled = true+ SiteSetting.ai_summary_gists_enabled = true++ group.add(admin)+ assign_persona_to(:ai_summary_gists_persona, [group.id])+ sign_in(admin)+ end++ context "when viewing public topic lists" do+ it "shows toggle when topics have gists" do+ visit("/latest")++ expect(find(".topic-list-layout-trigger")).to be_present+ end++ it "shows gists for public topics" do+ visit("/latest")++ find(".topic-list-layout-trigger").click+ find(+ ".dropdown-menu__item .d-button-label",+ text: I18n.t("js.discourse_ai.summarization.topic_list_layout.button.expanded"),+ ).click++ expect(page).to have_css("body.topic-list-layout-table-ai")+ end+ end++ context "when viewing PM topic lists" do+ fab!(:pm_topic) { Fabricate(:private_message_topic, user: admin, recipient: Fabricate(:user)) }+ fab!(:pm_gist) { Fabricate(:topic_ai_gist, target: pm_topic) }++ it "shows toggle when PMs have gists" do+ visit("/u/#{admin.username}/messages/new")++ expect(find(".topic-list-layout-trigger")).to be_present+ end++ it "enables gists for PM topics" do+ visit("/u/#{admin.username}/messages/new")++ find(".topic-list-layout-trigger").click+ find(+ ".dropdown-menu__item .d-button-label",+ text: I18n.t("js.discourse_ai.summarization.topic_list_layout.button.expanded"),+ ).click++ expect(page).to have_css("body.topic-list-layout-table-ai")+ end++ it "PM and public topic toggles are independent from each other" do+ visit("/latest")+ find(".topic-list-layout-trigger").click+ find(+ ".dropdown-menu__item .d-button-label",+ text: I18n.t("js.discourse_ai.summarization.topic_list_layout.button.compact"),+ ).click++ visit("/u/#{admin.username}/messages/new")+ find(".topic-list-layout-trigger").click+ find(+ ".dropdown-menu__item .d-button-label",+ text: I18n.t("js.discourse_ai.summarization.topic_list_layout.button.expanded"),+ ).click++ visit("/latest")+ expect(page).to have_css("body.topic-list-layout-table")+ expect(page).not_to have_css("body.topic-list-layout-table-ai")++ visit("/u/#{admin.username}/messages/new")+ expect(page).to have_css("body.topic-list-layout-table-ai")+ expect(page).not_to have_css("body.topic-list-layout-table")+ end+ end++ context "when no gists are available" do+ before { topic_ai_gist.destroy! }++ it "does not show toggle for topics without gists" do+ visit("/latest")++ expect(page).not_to have_css(".topic-list-layout-trigger")+ end+ end+end