discourse-gated-topics-in-c.../javascripts/discourse/components/topic-in-gated-category.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-04-05 13:34:13 -04:00
import Component from "@ember/component";
import discourseComputed from "discourse-common/utils/decorators";
2022-04-05 14:23:15 -04:00
const enabledCategories = settings.enabled_categories
.split("|")
.map((id) => parseInt(id, 10))
.filter((id) => id);
2022-04-05 13:34:13 -04:00
const enabledTags = settings.enabled_tags.split("|").filter(Boolean);
2022-04-05 13:34:13 -04:00
export default Component.extend({
tagName: "",
hidden: true,
didInsertElement() {
this._super(...arguments);
2022-04-05 14:23:15 -04:00
this.recalculate();
},
didUpdateAttrs() {
this._super(...arguments);
this.recalculate();
2022-04-05 13:34:13 -04:00
},
willDestroyElement() {
2024-03-27 14:32:45 -04:00
this._super(...arguments);
2022-04-05 14:23:15 -04:00
document.body.classList.remove("topic-in-gated-category");
2022-04-05 13:34:13 -04:00
},
2022-04-05 14:23:15 -04:00
recalculate() {
// do nothing if:
// a) topic does not have a category and does not have a gated tag
2022-04-05 14:23:15 -04:00
// b) component setting is empty
// c) user is logged in
const gatedByTag = this.tags?.some((t) => enabledTags.includes(t));
2022-04-05 14:23:15 -04:00
if (
(!this.categoryId && !gatedByTag) ||
(enabledCategories.length === 0 && enabledTags.length === 0) ||
2022-04-05 14:23:15 -04:00
this.currentUser
) {
return;
}
if (enabledCategories.includes(this.categoryId) || gatedByTag) {
2022-04-05 14:23:15 -04:00
document.body.classList.add("topic-in-gated-category");
this.set("hidden", false);
}
2022-04-05 13:34:13 -04:00
},
@discourseComputed("hidden")
shouldShow(hidden) {
return !hidden;
},
});