FEATURE: Add cancel button to loading context menu (#213)

This commit is contained in:
Keegan George 2023-09-07 13:50:56 -07:00 committed by GitHub
parent 087be9f4da
commit 6295b16678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 7 deletions

View File

@ -34,7 +34,14 @@
<span> <span>
{{i18n "discourse_ai.ai_helper.context_menu.loading"}} {{i18n "discourse_ai.ai_helper.context_menu.loading"}}
</span> </span>
<DButton
@icon="times"
@title="discourse_ai.ai_helper.context_menu.cancel"
@action={{this.cancelAIAction}}
class="btn-flat cancel-request"
/>
</li> </li>
</ul> </ul>
{{else if (eq this.menuState this.CONTEXT_MENU_STATES.review)}} {{else if (eq this.menuState this.CONTEXT_MENU_STATES.review)}}

View File

@ -60,6 +60,7 @@ export default class AiHelperContextMenu extends Component {
@tracked _popper; @tracked _popper;
@tracked _dEditorInput; @tracked _dEditorInput;
@tracked _contextMenu; @tracked _contextMenu;
@tracked _activeAIRequest = null;
constructor() { constructor() {
super(...arguments); super(...arguments);
@ -168,7 +169,7 @@ export default class AiHelperContextMenu extends Component {
} }
get canCloseContextMenu() { get canCloseContextMenu() {
if (this.loading) { if (this.loading && this._activeAIRequest !== null) {
return false; return false;
} }
@ -205,6 +206,16 @@ export default class AiHelperContextMenu extends Component {
this.menuState = this.CONTEXT_MENU_STATES.review; this.menuState = this.CONTEXT_MENU_STATES.review;
} }
_toggleLoadingState(loading) {
if (loading) {
this._dEditorInput.classList.add("loading");
return (this.loading = true);
}
this._dEditorInput.classList.remove("loading");
return (this.loading = false);
}
handleBoundaries() { handleBoundaries() {
const textAreaWrapper = document const textAreaWrapper = document
.querySelector(".d-editor-textarea-wrapper") .querySelector(".d-editor-textarea-wrapper")
@ -302,15 +313,16 @@ export default class AiHelperContextMenu extends Component {
@action @action
async updateSelected(option) { async updateSelected(option) {
this.loading = true; this._toggleLoadingState(true);
this.lastUsedOption = option; this.lastUsedOption = option;
this._dEditorInput.classList.add("loading");
this.menuState = this.CONTEXT_MENU_STATES.loading; this.menuState = this.CONTEXT_MENU_STATES.loading;
return ajax("/discourse-ai/ai-helper/suggest", { this._activeAIRequest = ajax("/discourse-ai/ai-helper/suggest", {
method: "POST", method: "POST",
data: { mode: option, text: this.selectedText }, data: { mode: option, text: this.selectedText },
}) });
this._activeAIRequest
.then((data) => { .then((data) => {
// resets the values if new suggestion is started: // resets the values if new suggestion is started:
this.diff = null; this.diff = null;
@ -319,9 +331,10 @@ export default class AiHelperContextMenu extends Component {
}) })
.catch(popupAjaxError) .catch(popupAjaxError)
.finally(() => { .finally(() => {
this.loading = false; this._toggleLoadingState(false);
this._dEditorInput.classList.remove("loading");
}); });
return this._activeAIRequest;
} }
@action @action
@ -333,4 +346,14 @@ export default class AiHelperContextMenu extends Component {
confirmChanges() { confirmChanges() {
this.menuState = this.CONTEXT_MENU_STATES.resets; this.menuState = this.CONTEXT_MENU_STATES.resets;
} }
@action
cancelAIAction() {
if (this._activeAIRequest) {
this._activeAIRequest.abort();
this._activeAIRequest = null;
this._toggleLoadingState(false);
this.closeContextMenu();
}
}
} }

View File

@ -90,6 +90,10 @@
justify-content: flex-start; justify-content: flex-start;
align-items: center; align-items: center;
} }
.btn {
width: unset;
}
} }
&__resets { &__resets {