|
| 1 | +import Component from "@glimmer/component"; |
| 2 | +import { tracked } from "@glimmer/tracking"; |
| 3 | +import { action } from "@ember/object"; |
| 4 | +import didInsert from "@ember/render-modifiers/modifiers/did-insert"; |
| 5 | +import didUpdate from "@ember/render-modifiers/modifiers/did-update"; |
| 6 | +import { service } from "@ember/service"; |
| 7 | +import { gt } from "truth-helpers"; |
| 8 | +import CreateTopicButton from "discourse/components/create-topic-button"; |
| 9 | +import not from "truth-helpers/helpers/not"; |
| 10 | + |
| 11 | +export default class SidebarNewTopicButton extends Component { |
| 12 | + @service composer; |
| 13 | + @service currentUser; |
| 14 | + @service siteSettings; |
| 15 | + @service router; |
| 16 | + |
| 17 | + @tracked category; |
| 18 | + @tracked tag; |
| 19 | + |
| 20 | + get shouldRender() { |
| 21 | + return this.currentUser; |
| 22 | + } |
| 23 | + |
| 24 | + get canCreateTopic() { |
| 25 | + return this.currentUser?.can_create_topic; |
| 26 | + } |
| 27 | + |
| 28 | + get draftCount() { |
| 29 | + return this.currentUser?.get("draft_count"); |
| 30 | + } |
| 31 | + |
| 32 | + get createTopicTargetCategory() { |
| 33 | + if (this.category?.canCreateTopic) { |
| 34 | + return this.category; |
| 35 | + } |
| 36 | + |
| 37 | + if (this.siteSettings.default_subcategory_on_read_only_category) { |
| 38 | + return this.category?.subcategoryWithCreateTopicPermission; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + get tagRestricted() { |
| 43 | + return this.tag?.staff; |
| 44 | + } |
| 45 | + |
| 46 | + get createTopicDisabled() { |
| 47 | + return ( |
| 48 | + (this.category && !this.createTopicTargetCategory) || this.tagRestricted |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + get categoryReadOnlyBanner() { |
| 53 | + if (this.category && this.currentUser && this.createTopicDisabled) { |
| 54 | + return this.category.read_only_banner; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + get createTopicClass() { |
| 59 | + const baseClasses = "btn-default sidebar-new-topic-button"; |
| 60 | + return this.categoryReadOnlyBanner |
| 61 | + ? `${baseClasses} disabled` |
| 62 | + : baseClasses; |
| 63 | + } |
| 64 | + |
| 65 | + @action |
| 66 | + createNewTopic() { |
| 67 | + this.composer.openNewTopic({ category: this.category, tags: this.tag?.id }); |
| 68 | + } |
| 69 | + |
| 70 | + @action |
| 71 | + getCategoryandTag() { |
| 72 | + this.category = this.router.currentRoute.attributes.category || null; |
| 73 | + this.tag = this.router.currentRoute.attributes.tag || null; |
| 74 | + } |
| 75 | + |
| 76 | + <template> |
| 77 | + {{#if this.shouldRender}} |
| 78 | + <div |
| 79 | + class="sidebar-new-topic-button__wrapper" |
| 80 | + {{didInsert this.getCategoryandTag}} |
| 81 | + {{didUpdate this.getCategoryandTag this.router.currentRoute}} |
| 82 | + > |
| 83 | + <CreateTopicButton |
| 84 | + @canCreateTopic={{this.canCreateTopic}} |
| 85 | + @action={{this.createNewTopic}} |
| 86 | + @disabled={{this.createTopicDisabled}} |
| 87 | + @label="topic.create" |
| 88 | + @btnClass={{this.createTopicClass}} |
| 89 | + @canCreateTopicOnTag={{not this.tagRestricted}} |
| 90 | + @showDrafts={{gt this.draftCount 0}} |
| 91 | + /> |
| 92 | + </div> |
| 93 | + {{/if}} |
| 94 | + </template> |
| 95 | +} |
0 commit comments