mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-07 01:39:54 +00:00
A missing parameter on the `parseInt` function was causing unexpected UI behavior for the AI helper since it turned an allowed group ID into NaN. We should always use base10 when parsing these IDs.
73 lines
1.9 KiB
JavaScript
73 lines
1.9 KiB
JavaScript
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
import showModal from "discourse/lib/show-modal";
|
|
|
|
function initializeComposerAIHelper(api) {
|
|
api.modifyClass("component:composer-editor", {
|
|
pluginId: "discourse-ai",
|
|
|
|
actions: {
|
|
extraButtons(toolbar) {
|
|
this._super(toolbar);
|
|
|
|
const removeAiHelperFromPM =
|
|
this.composerModel.privateMessage &&
|
|
!this.siteSettings.ai_helper_allowed_in_pm;
|
|
|
|
if (removeAiHelperFromPM) {
|
|
const extrasGroup = toolbar.groups.find((g) => g.group === "extras");
|
|
const newButtons = extrasGroup.buttons.filter(
|
|
(b) => b.id !== "ai-helper"
|
|
);
|
|
|
|
extrasGroup.buttons = newButtons;
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
api.modifyClass("component:d-editor", {
|
|
pluginId: "discourse-ai",
|
|
|
|
actions: {
|
|
openAIHelper() {
|
|
if (this.value) {
|
|
showModal("composer-ai-helper").setProperties({ editor: this });
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
api.onToolbarCreate((toolbar) => {
|
|
toolbar.addButton({
|
|
id: "ai-helper",
|
|
title: "discourse_ai.ai_helper.title",
|
|
group: "extras",
|
|
icon: "discourse-sparkles",
|
|
className: "composer-ai-helper",
|
|
sendAction: () => toolbar.context.send("openAIHelper"),
|
|
});
|
|
});
|
|
}
|
|
|
|
export default {
|
|
name: "discourse-ai-composer-helper",
|
|
|
|
initialize(container) {
|
|
const settings = container.lookup("service:site-settings");
|
|
const user = container.lookup("service:current-user");
|
|
const helperEnabled =
|
|
settings.discourse_ai_enabled && settings.composer_ai_helper_enabled;
|
|
|
|
const allowedGroups = settings.ai_helper_allowed_groups
|
|
.split("|")
|
|
.map((id) => parseInt(id, 10));
|
|
const canUseAssistant = user?.groups.some((g) =>
|
|
allowedGroups.includes(g.id)
|
|
);
|
|
|
|
if (helperEnabled && canUseAssistant) {
|
|
withPluginApi("1.6.0", initializeComposerAIHelper);
|
|
}
|
|
},
|
|
};
|