|
| 1 | +import Component from "@glimmer/component"; |
| 2 | +import { tracked } from "@glimmer/tracking"; |
| 3 | +import { on } from "@ember/modifier"; |
| 4 | +import { action } from "@ember/object"; |
| 5 | +import { service } from "@ember/service"; |
| 6 | +import { htmlSafe } from "@ember/template"; |
| 7 | +import AsyncContent from "discourse/components/async-content"; |
| 8 | +import PostCookedHtml from "discourse/components/post/cooked-html"; |
| 9 | +import concatClass from "discourse/helpers/concat-class"; |
| 10 | +import icon from "discourse/helpers/d-icon"; |
| 11 | +import { ajax } from "discourse/lib/ajax"; |
| 12 | +import { iconHTML } from "discourse/lib/icon-library"; |
| 13 | +import { formatUsername } from "discourse/lib/utilities"; |
| 14 | +import { i18n } from "discourse-i18n"; |
| 15 | + |
| 16 | +export default class SolvedAcceptedAnswer extends Component { |
| 17 | + @service siteSettings; |
| 18 | + @service store; |
| 19 | + |
| 20 | + @tracked expanded = false; |
| 21 | + |
| 22 | + get acceptedAnswer() { |
| 23 | + return this.topic.accepted_answer; |
| 24 | + } |
| 25 | + |
| 26 | + get quoteId() { |
| 27 | + return `accepted-answer-${this.topic.id}-${this.acceptedAnswer.post_number}`; |
| 28 | + } |
| 29 | + |
| 30 | + get topic() { |
| 31 | + return this.args.post.topic; |
| 32 | + } |
| 33 | + |
| 34 | + get hasExcerpt() { |
| 35 | + return !!this.acceptedAnswer.excerpt; |
| 36 | + } |
| 37 | + |
| 38 | + get htmlAccepter() { |
| 39 | + const username = this.acceptedAnswer.accepter_username; |
| 40 | + const name = this.acceptedAnswer.accepter_name; |
| 41 | + |
| 42 | + if (!this.siteSettings.show_who_marked_solved) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const formattedUsername = |
| 47 | + this.siteSettings.display_name_on_posts && name |
| 48 | + ? name |
| 49 | + : formatUsername(username); |
| 50 | + |
| 51 | + return htmlSafe( |
| 52 | + i18n("solved.marked_solved_by", { |
| 53 | + username: formattedUsername, |
| 54 | + username_lower: username.toLowerCase(), |
| 55 | + }) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + get htmlSolvedBy() { |
| 60 | + const username = this.acceptedAnswer.username; |
| 61 | + const name = this.acceptedAnswer.name; |
| 62 | + const postNumber = this.acceptedAnswer.post_number; |
| 63 | + |
| 64 | + if (!username || !postNumber) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const displayedUser = |
| 69 | + this.siteSettings.display_name_on_posts && name |
| 70 | + ? name |
| 71 | + : formatUsername(username); |
| 72 | + |
| 73 | + const data = { |
| 74 | + icon: iconHTML("square-check", { class: "accepted" }), |
| 75 | + username_lower: username.toLowerCase(), |
| 76 | + username: displayedUser, |
| 77 | + post_path: `${this.topic.url}/${postNumber}`, |
| 78 | + post_number: postNumber, |
| 79 | + user_path: this.store.createRecord("user", { username }).path, |
| 80 | + }; |
| 81 | + |
| 82 | + return htmlSafe(i18n("solved.accepted_html", data)); |
| 83 | + } |
| 84 | + |
| 85 | + @action |
| 86 | + toggleExpandedPost() { |
| 87 | + if (!this.hasExcerpt) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + this.expanded = !this.expanded; |
| 92 | + } |
| 93 | + |
| 94 | + @action |
| 95 | + async loadExpandedAcceptedAnswer(postNumber) { |
| 96 | + const acceptedAnswer = await ajax( |
| 97 | + `/posts/by_number/${this.topic.id}/${postNumber}` |
| 98 | + ); |
| 99 | + |
| 100 | + return this.store.createRecord("post", acceptedAnswer); |
| 101 | + } |
| 102 | + |
| 103 | + <template> |
| 104 | + <aside |
| 105 | + class="quote accepted-answer" |
| 106 | + data-post={{this.acceptedAnswer.post_number}} |
| 107 | + data-topic={{this.topic.id}} |
| 108 | + data-expanded={{this.expanded}} |
| 109 | + > |
| 110 | + {{! template-lint-disable no-invalid-interactive }} |
| 111 | + <div |
| 112 | + class={{concatClass |
| 113 | + "title" |
| 114 | + (unless this.hasExcerpt "title-only") |
| 115 | + (if this.hasExcerpt "quote__title--can-toggle-content") |
| 116 | + }} |
| 117 | + {{on "click" this.toggleExpandedPost}} |
| 118 | + > |
| 119 | + <div class="accepted-answer--solver-accepter"> |
| 120 | + <div class="accepted-answer--solver"> |
| 121 | + {{this.htmlSolvedBy}} |
| 122 | + </div> |
| 123 | + <div class="accepted-answer--accepter"> |
| 124 | + {{this.htmlAccepter}} |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + {{#if this.hasExcerpt}} |
| 128 | + <div class="quote-controls"> |
| 129 | + <button |
| 130 | + aria-controls={{this.quoteId}} |
| 131 | + aria-expanded={{this.expanded}} |
| 132 | + class="quote-toggle btn-flat" |
| 133 | + type="button" |
| 134 | + > |
| 135 | + {{icon |
| 136 | + (if this.expanded "chevron-up" "chevron-down") |
| 137 | + title="post.expand_collapse" |
| 138 | + }} |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + {{/if}} |
| 142 | + </div> |
| 143 | + {{#if this.hasExcerpt}} |
| 144 | + <blockquote id={{this.quoteId}}> |
| 145 | + {{#if this.expanded}} |
| 146 | + <AsyncContent |
| 147 | + @asyncData={{this.loadExpandedAcceptedAnswer}} |
| 148 | + @context={{this.acceptedAnswer.post_number}} |
| 149 | + > |
| 150 | + <:content as |expandedAnswer|> |
| 151 | + <div class="expanded-quote" data-post-id={{expandedAnswer.id}}> |
| 152 | + <PostCookedHtml |
| 153 | + @post={{expandedAnswer}} |
| 154 | + @streamElement={{false}} |
| 155 | + /> |
| 156 | + </div> |
| 157 | + </:content> |
| 158 | + </AsyncContent> |
| 159 | + {{else}} |
| 160 | + {{htmlSafe this.acceptedAnswer.excerpt}} |
| 161 | + {{/if}} |
| 162 | + </blockquote> |
| 163 | + {{/if}} |
| 164 | + </aside> |
| 165 | + </template> |
| 166 | +} |
0 commit comments