A platform for community discussion. Free, open, simple.
DEV: add transformer for icons in drafts dropdown (#42999)
David Battersbyd636e2370b70
diff --git a/frontend/discourse/app/components/topic-drafts-dropdown.gjs b/frontend/discourse/app/components/topic-drafts-dropdown.gjsindex cd5e4d1dd6c..a5cf9eb2295 100644--- a/frontend/discourse/app/components/topic-drafts-dropdown.gjs+++ b/frontend/discourse/app/components/topic-drafts-dropdown.gjs@@ -4,6 +4,7 @@ import { fn } from "@ember/helper";import { action } from "@ember/object";import { service } from "@ember/service";import getURL from "discourse/lib/get-url";+import { applyValueTransformer } from "discourse/lib/transformer";import DiscourseURL from "discourse/lib/url";import {NEW_PRIVATE_MESSAGE_KEY,@@ -46,13 +47,17 @@ export default class TopicDraftsDropdown extends Component {}draftIcon(item) {+ let icon;+if (item.draft_key.startsWith(NEW_TOPIC_KEY)) {- return "layer-group";+ icon = "layer-group";} else if (item.draft_key.startsWith(NEW_PRIVATE_MESSAGE_KEY)) {- return "envelope";+ icon = "envelope";} else {- return "reply";+ icon = "reply";}++ return applyValueTransformer("draft-icon", icon, { draft: item });}@actiondiff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex faff1bd89d3..ba2a0e39e6b 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -76,6 +76,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["create-topic-button-draft-menu-class","create-topic-icon","create-topic-label",+ "draft-icon","flag-button-disabled-state","flag-button-dynamic-class","flag-button-render-decision",diff --git a/frontend/discourse/tests/acceptance/transformers/draft-icon-test.js b/frontend/discourse/tests/acceptance/transformers/draft-icon-test.jsnew file mode 100644index 00000000000..5d6a6b13cf6--- /dev/null+++ b/frontend/discourse/tests/acceptance/transformers/draft-icon-test.js@@ -0,0 +1,59 @@+import { click, visit, waitFor } from "@ember/test-helpers";+import { test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";+import {+ acceptance,+ updateCurrentUser,+} from "discourse/tests/helpers/qunit-helpers";++acceptance("draft-icon transformer", function (needs) {+ needs.user();++ needs.pretender((server, helper) => {+ server.get("/drafts.json", () =>+ helper.response({+ drafts: [+ {+ draft_key: "new_private_message",+ data: '{"title":"A message draft"}',+ },+ { draft_key: "new_topic", data: '{"title":"A topic draft"}' },+ ],+ })+ );+ });++ async function openDraftsMenu() {+ updateCurrentUser({ draft_count: 2 });++ await visit("/");+ await click("button.topic-drafts-menu-trigger");+ await waitFor(".topic-drafts-menu-content");+ }++ test("renders the default icons", async function (assert) {+ await openDraftsMenu();++ assert.dom(".topic-drafts-item:first-child svg.d-icon-envelope").exists();+ assert.dom(".topic-drafts-item:last-child svg.d-icon-layer-group").exists();+ });++ test("uses the icon returned by the transformer for the draft it receives", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer("draft-icon", ({ value, context }) => {+ return context.draft.draft_key === "new_private_message"+ ? "star"+ : value;+ });+ });++ await openDraftsMenu();++ assert+ .dom(".topic-drafts-item:first-child svg.d-icon-star")+ .exists("the message draft is transformed");+ assert+ .dom(".topic-drafts-item:last-child svg.d-icon-layer-group")+ .exists("the topic draft keeps the default icon");+ });+});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-draft-icon.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-draft-icon.jsnew file mode 100644index 00000000000..6ad30e7d214--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-draft-icon.js@@ -0,0 +1,16 @@+import { apiInitializer } from "discourse/lib/api";+import { isAiBotRecipient } from "../lib/ai-bot-helper";++export default apiInitializer((api) => {+ const currentUser = api.getCurrentUser();++ if (!currentUser?.ai_enabled_chat_bots?.length) {+ return;+ }++ api.registerValueTransformer("draft-icon", ({ value, context }) => {+ return isAiBotRecipient(context.draft?.data?.recipients, currentUser)+ ? "robot"+ : value;+ });+});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-bot-helper.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-bot-helper.jsindex f85cad1fb92..625e57545dd 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-bot-helper.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-bot-helper.js@@ -57,3 +57,17 @@ export function showShareConversationModal(modal, topicId) {}).catch(popupAjaxError);}++export function isAiBotRecipient(recipients, currentUser) {+ if (!recipients) {+ return false;+ }++ const usernames = recipients+ .split(",")+ .map((username) => username.trim().toLowerCase());++ return !!currentUser?.ai_enabled_chat_bots?.some((bot) =>+ usernames.includes(bot.username.toLowerCase())+ );+}diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-draft-icon-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-draft-icon-test.jsnew file mode 100644index 00000000000..5d4a9724889--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-draft-icon-test.js@@ -0,0 +1,59 @@+import { click, visit, waitFor } from "@ember/test-helpers";+import { test } from "qunit";+import {+ acceptance,+ updateCurrentUser,+} from "discourse/tests/helpers/qunit-helpers";++acceptance("AI Bot - Drafts dropdown icon", function (needs) {+ let recipients;++ needs.user({+ ai_enabled_chat_bots: [{ id: -110, username: "gpt4_bot" }],+ });++ needs.settings({+ discourse_ai_enabled: true,+ ai_bot_enabled: true,+ });++ needs.pretender((server, helper) => {+ server.get("/drafts.json", () =>+ helper.response({+ drafts: [+ {+ draft_key: "new_private_message",+ data: JSON.stringify({ recipients }),+ },+ ],+ })+ );+ });++ async function openDraftsMenu(draftRecipients) {+ recipients = draftRecipients;+ updateCurrentUser({ draft_count: 1 });++ await visit("/");+ await click("button.topic-drafts-menu-trigger");+ await waitFor(".topic-drafts-menu-content");+ }++ test("uses the robot icon for a draft addressed to a bot", async function (assert) {+ await openDraftsMenu("gpt4_bot");++ assert.dom(".topic-drafts-item svg.d-icon-robot").exists();+ });++ test("uses the robot icon when a bot is one of several recipients", async function (assert) {+ await openDraftsMenu("charlie,gpt4_bot");++ assert.dom(".topic-drafts-item svg.d-icon-robot").exists();+ });++ test("keeps the envelope icon when no recipient is a bot", async function (assert) {+ await openDraftsMenu("charlie");++ assert.dom(".topic-drafts-item svg.d-icon-envelope").exists();+ });+});