DEV: Use the new more-topics API (#885)

See: https://github.com/discourse/discourse/pull/29143
This commit is contained in:
Jarek Radosz 2024-11-04 17:42:50 +01:00 committed by GitHub
parent 772ee934ab
commit fa7ca8bc31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 88 deletions

View File

@ -1,15 +0,0 @@
<div
id="related-topics"
class="more-topics__list {{if this.hidden 'hidden'}}"
role="complementary"
aria-labelledby="related-topics-title"
{{did-insert this.registerList}}
{{will-destroy this.removeList}}
>
<h3 id="related-topics-title" class="more-topics__list-title">
{{i18n "discourse_ai.related_topics.title"}}
</h3>
<div class="topics">
<BasicTopicList @topics={{this.relatedTopics}} />
</div>
</div>

View File

@ -1,41 +0,0 @@
import Component from "@glimmer/component";
import { action, computed } from "@ember/object";
import { inject as service } from "@ember/service";
import I18n from "I18n";
export default class extends Component {
static shouldRender(args) {
return (args.model.related_topics?.length || 0) > 0;
}
@service store;
@service site;
@service moreTopicsPreferenceTracking;
listId = "related-topics";
@computed("moreTopicsPreferenceTracking.selectedTab")
get hidden() {
return this.moreTopicsPreferenceTracking.selectedTab !== this.listId;
}
get relatedTopics() {
return this.args.outletArgs.model.related_topics.map((topic) =>
this.store.createRecord("topic", topic)
);
}
@action
registerList() {
this.moreTopicsPreferenceTracking.registerTopicList({
name: I18n.t("discourse_ai.related_topics.pill"),
id: this.listId,
icon: "discourse-sparkles",
});
}
@action
removeList() {
this.moreTopicsPreferenceTracking.removeTopicList(this.listId);
}
}

View File

@ -0,0 +1,71 @@
import { cached, tracked } from "@glimmer/tracking";
import BasicTopicList from "discourse/components/basic-topic-list";
import { withPluginApi } from "discourse/lib/plugin-api";
import i18n from "discourse-common/helpers/i18n";
const RelatedTopics = <template>
<div
role="complementary"
aria-labelledby="related-topics-title"
id="related-topics"
class="more-topics__list"
>
<h3 id="related-topics-title" class="more-topics__list-title">
{{i18n "discourse_ai.related_topics.title"}}
</h3>
<div class="topics">
<BasicTopicList @topics={{@topic.relatedTopics}} />
</div>
</div>
</template>;
export default {
name: "discourse-ai-related-topics",
initialize(container) {
const settings = container.lookup("service:site-settings");
if (
!settings.ai_embeddings_enabled ||
!settings.ai_embeddings_semantic_related_topics_enabled
) {
return;
}
withPluginApi("1.37.2", (api) => {
api.registerMoreTopicsTab({
id: "related-topics",
name: i18n("discourse_ai.related_topics.pill"),
icon: "discourse-sparkles",
component: RelatedTopics,
condition: ({ topic }) => topic.relatedTopics?.length,
});
api.modifyClass(
"model:topic",
(Superclass) =>
class extends Superclass {
@tracked related_topics;
@cached
get relatedTopics() {
return this.related_topics?.map((topic) =>
this.store.createRecord("topic", topic)
);
}
}
);
api.modifyClass(
"model:post-stream",
(Superclass) =>
class extends Superclass {
_setSuggestedTopics(result) {
super._setSuggestedTopics(...arguments);
this.topic.related_topics = result.related_topics;
}
}
);
});
},
};

View File

@ -1,32 +0,0 @@
import { withPluginApi } from "discourse/lib/plugin-api";
export default {
name: "discourse-ai-related-topics",
initialize(container) {
const settings = container.lookup("service:site-settings");
if (
settings.ai_embeddings_enabled &&
settings.ai_embeddings_semantic_related_topics_enabled
) {
withPluginApi("1.1.0", (api) => {
api.modifyClass("model:post-stream", {
pluginId: "discourse-ai",
_setSuggestedTopics(result) {
this._super(...arguments);
if (!result.related_topics) {
return;
}
this.topic.setProperties({
related_topics: result.related_topics,
});
},
});
});
}
},
};