Files

120 lines
3.2 KiB
Plaintext
Raw Permalink Normal View History

2023-08-24 17:49:24 -07:00
import Component from "@glimmer/component";
2024-08-26 15:43:40 +10:00
import { tracked } from "@glimmer/tracking";
2023-08-24 17:49:24 -07:00
import { action } from "@ember/object";
2024-11-19 10:57:40 +00:00
import { service } from "@ember/service";
2023-11-03 11:30:09 +00:00
import { htmlSafe } from "@ember/template";
2024-08-26 15:43:40 +10:00
import CookText from "discourse/components/cook-text";
2023-08-24 17:49:24 -07:00
import DButton from "discourse/components/d-button";
import DModal from "discourse/components/d-modal";
2024-08-26 15:43:40 +10:00
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { i18n } from "discourse-i18n";
import AiIndicatorWave from "../ai-indicator-wave";
2023-08-24 17:49:24 -07:00
export default class ModalDiffModal extends Component {
2024-08-26 15:43:40 +10:00
@service currentUser;
2025-03-17 05:14:53 +01:00
2024-08-26 15:43:40 +10:00
@tracked loading = false;
@tracked diff;
@tracked suggestion = "";
2024-08-26 15:43:40 +10:00
constructor() {
super(...arguments);
this.suggestChanges();
2024-08-26 15:43:40 +10:00
}
@action
async suggestChanges() {
2024-08-26 15:43:40 +10:00
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,
2024-08-26 15:43:40 +10:00
force_default_locale: true,
},
});
this.diff = suggestion.diff;
this.suggestion = suggestion.suggestions[0];
} catch (e) {
popupAjaxError(e);
} finally {
this.loading = false;
}
}
2023-11-03 11:30:09 +00:00
@action
triggerConfirmChanges() {
this.args.closeModal();
2024-08-26 15:43:40 +10:00
if (this.suggestion) {
2024-08-26 15:43:40 +10:00
this.args.model.toolbarEvent.replaceText(
this.args.model.selectedText,
2024-08-26 15:43:40 +10:00
this.suggestion
);
}
2023-11-03 11:30:09 +00:00
}
2023-08-24 17:49:24 -07:00
<template>
<DModal
class="composer-ai-helper-modal"
2024-01-13 00:28:06 +01:00
@title={{i18n "discourse_ai.ai_helper.context_menu.changes"}}
2023-08-24 17:49:24 -07:00
@closeModal={{@closeModal}}
>
<:body>
{{#if this.loading}}
<div class="composer-ai-helper-modal__loading">
<CookText @rawText={{@model.selectedText}} />
</div>
{{else}}
{{#if this.diff}}
{{htmlSafe this.diff}}
2024-08-26 15:43:40 +10:00
{{else}}
<div class="composer-ai-helper-modal__old-value">
{{@model.selectedText}}
</div>
2023-08-24 17:49:24 -07:00
<div class="composer-ai-helper-modal__new-value">
{{this.suggestion}}
</div>
2024-08-26 15:43:40 +10:00
{{/if}}
{{/if}}
2023-08-24 17:49:24 -07:00
</:body>
<:footer>
2024-08-26 15:43:40 +10:00
{{#if this.loading}}
<DButton
class="btn-primary"
@label="discourse_ai.ai_helper.context_menu.loading"
@disabled={{true}}
>
<AiIndicatorWave @loading={{this.loading}} />
</DButton>
2024-08-26 15:43:40 +10:00
{{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"
/>
2024-08-26 15:43:40 +10:00
{{/if}}
2023-08-24 17:49:24 -07:00
</:footer>
</DModal>
</template>
}