discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs

119 lines
3.3 KiB
Plaintext
Raw Normal View History

2023-08-24 20:49:24 -04:00
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
2023-08-24 20:49:24 -04:00
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
2023-11-03 07:30:09 -04:00
import { htmlSafe } from "@ember/template";
import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
import CookText from "discourse/components/cook-text";
2023-08-24 20:49:24 -04:00
import DButton from "discourse/components/d-button";
import DModal from "discourse/components/d-modal";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import i18n from "discourse-common/helpers/i18n";
2023-08-24 20:49:24 -04:00
export default class ModalDiffModal extends Component {
@service currentUser;
@tracked loading = false;
@tracked diff;
@tracked suggestion = "";
constructor() {
super(...arguments);
this.suggestChanges();
}
@action
async suggestChanges() {
this.loading = true;
try {
const suggestion = await ajax("/discourse-ai/ai-helper/suggest", {
method: "POST",
data: {
mode: this.args.model.mode,
text: this.args.model.selectedText,
custom_prompt: this.args.model.customPromptValue,
force_default_locale: true,
},
});
this.diff = suggestion.diff;
this.suggestion = suggestion.suggestions[0];
} catch (e) {
popupAjaxError(e);
} finally {
this.loading = false;
}
}
2023-11-03 07:30:09 -04:00
@action
triggerConfirmChanges() {
this.args.closeModal();
if (this.suggestion) {
this.args.model.toolbarEvent.replaceText(
this.args.model.selectedText,
this.suggestion
);
}
2023-11-03 07:30:09 -04:00
}
2023-08-24 20:49:24 -04:00
<template>
<DModal
class="composer-ai-helper-modal"
@title={{i18n "discourse_ai.ai_helper.context_menu.changes"}}
2023-08-24 20:49:24 -04:00
@closeModal={{@closeModal}}
>
<:body>
<ConditionalLoadingSpinner @condition={{this.loading}}>
{{#if this.loading}}
<div class="composer-ai-helper-modal__loading">
<CookText @rawText={{this.selectedText}} />
</div>
{{else}}
{{#if this.diff}}
{{htmlSafe this.diff}}
{{else}}
<div class="composer-ai-helper-modal__old-value">
{{@model.selectedText}}
</div>
<div class="composer-ai-helper-modal__new-value">
{{this.suggestion}}
</div>
{{/if}}
{{/if}}
</ConditionalLoadingSpinner>
2023-08-24 20:49:24 -04:00
</:body>
<:footer>
{{#if this.loading}}
<DButton
class="btn-primary"
@label="discourse_ai.ai_helper.context_menu.loading"
@disabled={{true}}
/>
{{else}}
<DButton
class="btn-primary confirm"
@action={{this.triggerConfirmChanges}}
@label="discourse_ai.ai_helper.context_menu.confirm"
/>
<DButton
class="btn-flat discard"
@action={{@closeModal}}
@label="discourse_ai.ai_helper.context_menu.discard"
/>
<DButton
class="regenerate"
@icon="arrows-rotate"
@action={{this.suggestChanges}}
@label="discourse_ai.ai_helper.context_menu.regen"
/>
{{/if}}
2023-08-24 20:49:24 -04:00
</:footer>
</DModal>
</template>
}