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

Commit b16390a

Browse files
authored
UX: Improve toast message location (#800)
1 parent 9374cd7 commit b16390a

File tree

4 files changed

+86
-31
lines changed

4 files changed

+86
-31
lines changed

assets/javascripts/discourse/components/ai-composer-helper-menu.gjs

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Component from "@glimmer/component";
22
import { tracked } from "@glimmer/tracking";
33
import { action } from "@ember/object";
4+
import { getOwner } from "@ember/owner";
45
import { service } from "@ember/service";
56
import I18n from "discourse-i18n";
7+
import DToast from "float-kit/components/d-toast";
8+
import DToastInstance from "float-kit/lib/d-toast-instance";
69
import AiHelperOptionsList from "../components/ai-helper-options-list";
710
import ModalDiffModal from "../components/modal/diff-modal";
811
import ThumbnailSuggestion from "../components/modal/thumbnail-suggestions";
@@ -15,9 +18,18 @@ export default class AiComposerHelperMenu extends Component {
1518
@tracked newSelectedText;
1619
@tracked diff;
1720
@tracked customPromptValue = "";
21+
@tracked noContentError = false;
1822
prompts = [];
1923
promptTypes = {};
2024

25+
constructor() {
26+
super(...arguments);
27+
28+
if (this.args.data.toolbarEvent.getText().length === 0) {
29+
this.noContentError = true;
30+
}
31+
}
32+
2133
get helperOptions() {
2234
let prompts = this.currentUser?.ai_helper_prompts;
2335

@@ -70,6 +82,32 @@ export default class AiComposerHelperMenu extends Component {
7082
return prompts;
7183
}
7284

85+
get toast() {
86+
const owner = getOwner(this);
87+
const options = {
88+
close: () => this.args.close(),
89+
duration: 3000,
90+
data: {
91+
theme: "error",
92+
icon: "triangle-exclamation",
93+
message: I18n.t("discourse_ai.ai_helper.no_content_error"),
94+
},
95+
};
96+
97+
const custom = class CustomToastInstance extends DToastInstance {
98+
constructor() {
99+
super(owner, options);
100+
}
101+
102+
@action
103+
close() {
104+
this.options.close();
105+
}
106+
};
107+
108+
return new custom(owner, options);
109+
}
110+
73111
@action
74112
suggestChanges(option) {
75113
if (option.name === "illustrate_post") {
@@ -102,19 +140,23 @@ export default class AiComposerHelperMenu extends Component {
102140
}
103141

104142
<template>
105-
<div class="ai-composer-helper-menu">
106-
{{#if this.site.mobileView}}
107-
<div class="ai-composer-helper-menu__selected-text">
108-
{{@data.selectedText}}
109-
</div>
110-
{{/if}}
111-
112-
<AiHelperOptionsList
113-
@options={{this.helperOptions}}
114-
@customPromptValue={{this.customPromptValue}}
115-
@performAction={{this.suggestChanges}}
116-
@shortcutVisible={{true}}
117-
/>
118-
</div>
143+
{{#if this.noContentError}}
144+
<DToast @toast={{this.toast}} />
145+
{{else}}
146+
<div class="ai-composer-helper-menu">
147+
{{#if this.site.mobileView}}
148+
<div class="ai-composer-helper-menu__selected-text">
149+
{{@data.selectedText}}
150+
</div>
151+
{{/if}}
152+
153+
<AiHelperOptionsList
154+
@options={{this.helperOptions}}
155+
@customPromptValue={{this.customPromptValue}}
156+
@performAction={{this.suggestChanges}}
157+
@shortcutVisible={{true}}
158+
/>
159+
</div>
160+
{{/if}}
119161
</template>
120162
}

assets/javascripts/initializers/ai-helper.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@ import ModalDiffModal from "../discourse/components/modal/diff-modal";
55
import { showComposerAiHelper } from "../discourse/lib/show-ai-helper";
66

77
function initializeAiHelperTrigger(api) {
8-
const showErrorToast = () => {
9-
const toasts = api.container.lookup("service:toasts");
10-
11-
return toasts.error({
12-
duration: 3000,
13-
data: {
14-
message: i18n("discourse_ai.ai_helper.no_content_error"),
15-
},
16-
});
17-
};
18-
198
api.onToolbarCreate((toolbar) => {
209
const currentUser = api.getCurrentUser();
2110
const modal = api.container.lookup("service:modal");
@@ -41,7 +30,15 @@ function initializeAiHelperTrigger(api) {
4130
shortcut: "ALT+P",
4231
shortcutAction: (toolbarEvent) => {
4332
if (toolbarEvent.getText().length === 0) {
44-
return showErrorToast();
33+
const toasts = api.container.lookup("service:toasts");
34+
35+
return toasts.error({
36+
class: "ai-proofread-error-toast",
37+
duration: 3000,
38+
data: {
39+
message: i18n("discourse_ai.ai_helper.no_content_error"),
40+
},
41+
});
4542
}
4643

4744
const mode = currentUser?.ai_helper_prompts.find(
@@ -64,10 +61,6 @@ function initializeAiHelperTrigger(api) {
6461
"context_menu"
6562
),
6663
sendAction: (event) => {
67-
if (toolbar.context.value.length === 0) {
68-
return showErrorToast();
69-
}
70-
7164
const menu = api.container.lookup("service:menu");
7265
menu.show(document.querySelector(".ai-helper-trigger"), {
7366
identifier: "ai-composer-helper-menu",

assets/stylesheets/modules/ai-helper/common/ai-helper.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,23 @@
605605
.mobile-view .fk-d-menu[data-identifier="ai-composer-helper-menu"] {
606606
z-index: z("mobile-composer");
607607
}
608+
609+
.fk-d-toasts:has(.ai-proofread-error-toast) {
610+
top: unset;
611+
bottom: calc(var(--composer-height) - 5%);
612+
right: unset;
613+
left: 0;
614+
}
615+
616+
@media screen and (min-width: $reply-area-max-width) {
617+
.has-sidebar-page {
618+
.fk-d-toasts:has(.ai-proofread-error-toast) {
619+
transform: translateX(
620+
calc(
621+
(100vw - var(--d-max-width) - var(--d-sidebar-width) / 0.5) / 2 + 17em +
622+
1rem
623+
)
624+
);
625+
}
626+
}
627+
}

config/locales/client.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ en:
332332
prompt: "This post contains non-captioned images. Would you like to enable automatic AI captions on image uploads? (This can be changed in your preferences later)"
333333
confirm: "Enable"
334334
cancel: "Don't ask again"
335-
no_content_error: "Please add some content to perform AI actions on."
335+
no_content_error: "Add content first to perform AI actions on it"
336336

337337
reviewables:
338338
model_used: "Model used:"

0 commit comments

Comments
 (0)