|
| 1 | +import { |
| 2 | + html, |
| 3 | + css, |
| 4 | + LitElement, |
| 5 | + nothing, |
| 6 | + HTMLTemplateResult, |
| 7 | + CSSResultGroup, |
| 8 | +} from 'lit'; |
| 9 | +import { property, customElement, state } from 'lit/decorators.js'; |
| 10 | +import { msg } from '@lit/localize'; |
| 11 | + |
| 12 | +import { Review } from '@internetarchive/metadata-service'; |
| 13 | + |
| 14 | +import starBasic from './assets/star-basic'; |
| 15 | + |
| 16 | +/* Further properties for reviews added before render */ |
| 17 | +export interface ReviewForRender extends Review { |
| 18 | + screenname: string; |
| 19 | + domId: string; |
| 20 | + itemname?: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Renders a single IA review |
| 25 | + */ |
| 26 | +@customElement('ia-review') |
| 27 | +export class IaReview extends LitElement { |
| 28 | + /* The review to be rendered */ |
| 29 | + @property({ type: Object }) review?: ReviewForRender; |
| 30 | + |
| 31 | + /* Maximum length for subject */ |
| 32 | + @property({ type: Number }) maxSubjectLength = 100; |
| 33 | + |
| 34 | + /* Maximum length for body */ |
| 35 | + @property({ type: Number }) maxBodyLength = 1000; |
| 36 | + |
| 37 | + /* Base for URLs */ |
| 38 | + @property({ type: String }) baseHost = 'https://archive.org'; |
| 39 | + |
| 40 | + /* Whether to show truncated review title / body */ |
| 41 | + @state() |
| 42 | + private showTruncatedContent: boolean = false; |
| 43 | + |
| 44 | + render() { |
| 45 | + return !this.review |
| 46 | + ? html`<div class="error"> |
| 47 | + ${msg('This review cannot be displayed at this time.')} |
| 48 | + </div>` |
| 49 | + : html`<article class="review" id=${this.review.domId}> |
| 50 | + <div class="top-line"> |
| 51 | + <b>${msg('Reviewer:')} </b>${this.reviewerTemplate} - |
| 52 | + ${this.starsTemplate}${this.createDateTemplate} |
| 53 | + </div> |
| 54 | + <div class="subject"> |
| 55 | + <b>${msg('Subject: ')}</b>${this.subjectTemplate} |
| 56 | + </div> |
| 57 | + <div class="body">${this.bodyTemplate}</div> |
| 58 | + ${this.truncationButtonsTemplate} |
| 59 | + </article>`; |
| 60 | + } |
| 61 | + |
| 62 | + private get subjectTemplate(): string { |
| 63 | + if (!this.review?.reviewtitle) return ''; |
| 64 | + |
| 65 | + return this.review.reviewtitle.length <= this.maxSubjectLength || |
| 66 | + this.showTruncatedContent |
| 67 | + ? this.review.reviewtitle |
| 68 | + : this.review.reviewtitle.slice(0, this.maxSubjectLength).concat('...'); |
| 69 | + } |
| 70 | + |
| 71 | + private get bodyTemplate(): string { |
| 72 | + if (!this.review?.reviewbody) return ''; |
| 73 | + |
| 74 | + return this.review.reviewbody.length <= this.maxBodyLength || |
| 75 | + this.showTruncatedContent |
| 76 | + ? this.review.reviewbody |
| 77 | + : this.review.reviewbody.slice(0, this.maxBodyLength).concat('...'); |
| 78 | + } |
| 79 | + |
| 80 | + private get truncationButtonsTemplate(): HTMLTemplateResult | typeof nothing { |
| 81 | + if (!this.review?.reviewtitle || !this.review?.reviewbody) return nothing; |
| 82 | + |
| 83 | + const noTruncationNeeded = |
| 84 | + this.review.reviewtitle.length <= this.maxSubjectLength && |
| 85 | + this.review.reviewbody.length <= this.maxBodyLength; |
| 86 | + |
| 87 | + if (noTruncationNeeded) return nothing; |
| 88 | + |
| 89 | + return this.showTruncatedContent |
| 90 | + ? this.lessButtonTemplate |
| 91 | + : this.moreButtonTemplate; |
| 92 | + } |
| 93 | + |
| 94 | + private get moreButtonTemplate(): HTMLTemplateResult { |
| 95 | + return html`<button |
| 96 | + class="simple-link more-btn" |
| 97 | + @click=${() => (this.showTruncatedContent = true)} |
| 98 | + > |
| 99 | + ${msg('More...')} |
| 100 | + </button>`; |
| 101 | + } |
| 102 | + |
| 103 | + private get lessButtonTemplate(): HTMLTemplateResult { |
| 104 | + return html`<button |
| 105 | + class="simple-link less-btn" |
| 106 | + @click=${() => (this.showTruncatedContent = false)} |
| 107 | + > |
| 108 | + ${msg('...Less')} |
| 109 | + </button>`; |
| 110 | + } |
| 111 | + |
| 112 | + private get reviewerTemplate(): HTMLTemplateResult | typeof nothing { |
| 113 | + return !this.review |
| 114 | + ? nothing |
| 115 | + : this.review.itemname |
| 116 | + ? html`<a |
| 117 | + href="${this.baseHost}/details/${this.review.itemname}" |
| 118 | + class="reviewer-link simple-link" |
| 119 | + data-event-click-tracking="ItemReviews|ReviewerLink" |
| 120 | + >${this.review.screenname}</a |
| 121 | + >` |
| 122 | + : html`${this.review.screenname}`; |
| 123 | + } |
| 124 | +
|
| 125 | + private get starsTemplate(): HTMLTemplateResult | typeof nothing { |
| 126 | + if (!this.review || !this.review.stars) return nothing; |
| 127 | + return html`<div |
| 128 | + class="review-stars" |
| 129 | + title="${msg(`${this.review.stars} out of 5 stars`)}" |
| 130 | + > |
| 131 | + ${new Array(this.review.stars) |
| 132 | + .fill(null) |
| 133 | + .map(() => html`<div class="review-star">${starBasic}</div>`)} |
| 134 | + </div> |
| 135 | + - `; |
| 136 | + } |
| 137 | + |
| 138 | + private get createDateTemplate(): string | typeof nothing { |
| 139 | + if (!this.review) return nothing; |
| 140 | + const prettyDate = this.review.createdate?.toLocaleString('en-us', { |
| 141 | + month: 'long', |
| 142 | + day: 'numeric', |
| 143 | + year: 'numeric', |
| 144 | + }); |
| 145 | + const editedMsg = |
| 146 | + this.review.reviewdate?.getTime() !== this.review.createdate?.getTime() |
| 147 | + ? '(edited)' |
| 148 | + : ''; |
| 149 | + |
| 150 | + return msg(`${prettyDate} ${editedMsg}`); |
| 151 | + } |
| 152 | + |
| 153 | + static get styles(): CSSResultGroup { |
| 154 | + return css` |
| 155 | + :host { |
| 156 | + font-family: var( |
| 157 | + --ia-font-stack, |
| 158 | + 'Helvetica Neue', |
| 159 | + Helvetica, |
| 160 | + Arial, |
| 161 | + sans-serif |
| 162 | + ); |
| 163 | +
|
| 164 | + font-size: inherit; |
| 165 | + } |
| 166 | +
|
| 167 | + .error { |
| 168 | + color: var(--ia-theme-error-color, #cc0000); |
| 169 | + } |
| 170 | +
|
| 171 | + .top-line { |
| 172 | + display: flex; |
| 173 | + flex-direction: row; |
| 174 | + gap: 3px; |
| 175 | + margin-bottom: 0.5rem; |
| 176 | + } |
| 177 | +
|
| 178 | + .review-star { |
| 179 | + width: 1rem; |
| 180 | + } |
| 181 | +
|
| 182 | + .review-stars { |
| 183 | + display: flex; |
| 184 | + flex-direction: row; |
| 185 | + } |
| 186 | +
|
| 187 | + .simple-link { |
| 188 | + color: var(--ia-link-color, #4b64ff); |
| 189 | + text-decoration: none; |
| 190 | + background: transparent; |
| 191 | + border: none; |
| 192 | + padding: 0px; |
| 193 | + } |
| 194 | +
|
| 195 | + .simple-link:hover { |
| 196 | + cursor: pointer; |
| 197 | + text-decoration: underline; |
| 198 | + } |
| 199 | +
|
| 200 | + .subject { |
| 201 | + margin-bottom: 0.5rem; |
| 202 | + } |
| 203 | + `; |
| 204 | + } |
| 205 | +} |
0 commit comments