Skip to content

Commit b4a419d

Browse files
authored
Refactor comment widget to use config map context (#185)
Fixes #184 Changed to frontend fetching comment widget configuration via API to simplify the code. ```release-note None ```
1 parent 44c3f09 commit b4a419d

15 files changed

Lines changed: 427 additions & 284 deletions

File tree

packages/comment-widget/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
],
2929
"scripts": {
3030
"build": "tsc",
31-
"dev": "tsc -w"
31+
"dev": "tsc -w",
32+
"locale:extract": "lit-localize extract",
33+
"locale:build": "lit-localize build"
3234
},
3335
"dependencies": {
3436
"@emoji-mart/data": "^1.2.1",

packages/comment-widget/src/base-form.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createRef, type Ref, ref } from 'lit/directives/ref.js';
77
import {
88
allowAnonymousCommentsContext,
99
baseUrlContext,
10-
captchaEnabledContext,
10+
configMapDataContext,
1111
currentUserContext,
1212
groupContext,
1313
kindContext,
@@ -20,6 +20,7 @@ import { msg } from '@lit/localize';
2020
import type { ToastManager } from './lit-toast';
2121
import baseStyles from './styles/base';
2222
import varStyles from './styles/var';
23+
import type { ConfigMapData } from './types';
2324

2425
export class BaseForm extends LitElement {
2526
@consume({ context: baseUrlContext })
@@ -30,13 +31,13 @@ export class BaseForm extends LitElement {
3031
@state()
3132
currentUser: User | undefined;
3233

33-
@consume({ context: allowAnonymousCommentsContext, subscribe: true })
34+
@consume({ context: configMapDataContext })
3435
@state()
35-
allowAnonymousComments = false;
36+
configMapData: ConfigMapData | undefined;
3637

37-
@consume({ context: captchaEnabledContext, subscribe: true })
38+
@consume({ context: allowAnonymousCommentsContext, subscribe: true })
3839
@state()
39-
captchaEnabled = false;
40+
allowAnonymousComments = false;
4041

4142
@consume({ context: groupContext })
4243
@state()
@@ -78,18 +79,22 @@ export class BaseForm extends LitElement {
7879
.join('-')
7980
.replaceAll(/-+/g, '-')}`;
8081

81-
return `/login?redirect_uri=${encodeURIComponent(window.location.pathname + parentDomId)}`;
82+
return `/login?redirect_uri=${encodeURIComponent(
83+
window.location.pathname + parentDomId
84+
)}`;
8285
}
8386

8487
get showCaptcha() {
8588
return (
86-
this.captchaEnabled && !this.currentUser && this.allowAnonymousComments
89+
this.configMapData?.security.captcha.anonymousCommentCaptcha &&
90+
!this.currentUser &&
91+
this.allowAnonymousComments
8792
);
8893
}
8994

9095
override updated(changedProperties: Map<string, unknown>) {
9196
if (
92-
changedProperties.has('captchaEnabled') ||
97+
changedProperties.has('configMapData') ||
9398
changedProperties.has('currentUser') ||
9499
changedProperties.has('allowAnonymousComments')
95100
) {
@@ -138,9 +143,19 @@ export class BaseForm extends LitElement {
138143

139144
renderAccountInfo() {
140145
return html`<div class="form__account-info">
141-
${this.currentUser?.spec.avatar ? html`<img src=${this.currentUser.spec.avatar} />` : ''}
142-
<span> ${this.currentUser?.spec.displayName || this.currentUser?.metadata.name} </span>
143-
<button @click=${this.handleLogout} type="button" class="form__button--logout">
146+
${
147+
this.currentUser?.spec.avatar
148+
? html`<img src=${this.currentUser.spec.avatar} />`
149+
: ''
150+
}
151+
<span>
152+
${this.currentUser?.spec.displayName || this.currentUser?.metadata.name}
153+
</span>
154+
<button
155+
@click=${this.handleLogout}
156+
type="button"
157+
class="form__button--logout"
158+
>
144159
${msg('Logout')}
145160
</button>
146161
</div>`;
@@ -235,7 +250,7 @@ export class BaseForm extends LitElement {
235250
}
236251
<div class="form__actions">
237252
${
238-
this.showCaptcha
253+
this.showCaptcha && this.captcha
239254
? html`
240255
<div class="form__action--captcha">
241256
<input
@@ -255,7 +270,11 @@ export class BaseForm extends LitElement {
255270
}
256271
257272
<emoji-button @emoji-select=${this.onEmojiSelect}></emoji-button>
258-
<button .disabled=${this.submitting} type="submit" class="form__button--submit">
273+
<button
274+
.disabled=${this.submitting}
275+
type="submit"
276+
class="form__button--submit"
277+
>
259278
${
260279
this.submitting
261280
? html` <icon-loading></icon-loading>`
@@ -369,11 +388,7 @@ export class BaseForm extends LitElement {
369388
outline: 0;
370389
padding: 0.4em 0.75em;
371390
width: 100%;
372-
transition:
373-
background 0.2s,
374-
border 0.2s,
375-
box-shadow 0.2s,
376-
color 0.2s;
391+
transition: background 0.2s, border 0.2s, box-shadow 0.2s, color 0.2s;
377392
}
378393
379394
input:focus,

packages/comment-widget/src/comment-form.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,15 @@ export class CommentForm extends LitElement {
149149
);
150150
}
151151

152-
this.dispatchEvent(new CustomEvent('reload'));
152+
window.dispatchEvent(
153+
new CustomEvent('comment:reload', {
154+
detail: {
155+
page: 1,
156+
scrollIntoView: true,
157+
},
158+
})
159+
);
160+
153161
this.baseFormRef.value?.resetForm();
154162
} catch (error) {
155163
if (error instanceof Error) {

packages/comment-widget/src/comment-item.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import { createRef, type Ref, ref } from 'lit/directives/ref.js';
1212
import { getPolicyInstance } from './avatar/avatar-policy';
1313
import type { CommentReplies } from './comment-replies';
1414
import { LS_UPVOTED_COMMENTS_KEY } from './constant';
15-
import { baseUrlContext, withRepliesContext } from './context';
15+
import { baseUrlContext, configMapDataContext } from './context';
1616
import varStyles from './styles/var';
17+
import type { ConfigMapData } from './types';
1718

1819
export class CommentItem extends LitElement {
1920
@consume({ context: baseUrlContext })
@@ -23,9 +24,9 @@ export class CommentItem extends LitElement {
2324
@property({ type: Object })
2425
comment: CommentVo | undefined;
2526

26-
@consume({ context: withRepliesContext, subscribe: true })
27+
@consume({ context: configMapDataContext })
2728
@state()
28-
withReplies = false;
29+
configMapData: ConfigMapData | undefined;
2930

3031
@state()
3132
showReplies = false;
@@ -45,7 +46,7 @@ export class CommentItem extends LitElement {
4546
super.connectedCallback();
4647
this.checkUpvotedStatus();
4748

48-
if (this.withReplies) {
49+
if (this.configMapData?.basic.withReplies) {
4950
this.showReplies = true;
5051
this.showReplyForm = false;
5152
}
@@ -100,7 +101,7 @@ export class CommentItem extends LitElement {
100101
handleShowReplies() {
101102
this.showReplies = !this.showReplies;
102103

103-
if (!this.withReplies) {
104+
if (!this.configMapData?.basic.withReplies) {
104105
this.showReplyForm = !this.showReplyForm;
105106
}
106107
}
@@ -138,7 +139,12 @@ export class CommentItem extends LitElement {
138139
height="32"
139140
viewBox="0 0 24 24"
140141
>
141-
<g fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2">
142+
<g
143+
fill="none"
144+
stroke-linecap="round"
145+
stroke-linejoin="round"
146+
stroke-width="2"
147+
>
142148
<path d="M0 0h24v24H0z" />
143149
<path
144150
fill="red"
@@ -166,7 +172,8 @@ export class CommentItem extends LitElement {
166172
</base-comment-item-action>
167173
168174
${
169-
this.withReplies && this.comment?.status?.visibleReplyCount === 0
175+
this.configMapData?.basic.withReplies &&
176+
this.comment?.status?.visibleReplyCount === 0
170177
? ''
171178
: html`<base-comment-item-action
172179
slot="action"
@@ -192,7 +199,7 @@ export class CommentItem extends LitElement {
192199
</base-comment-item-action>`
193200
}
194201
${
195-
this.withReplies
202+
this.configMapData?.basic.withReplies
196203
? html` <base-comment-item-action
197204
slot="action"
198205
.text=${this.showReplyForm ? msg('Cancel reply') : msg('Add reply')}
@@ -222,7 +229,10 @@ export class CommentItem extends LitElement {
222229
${
223230
this.showReplyForm
224231
? html`<div class="item__reply-form">
225-
<reply-form @reload=${this.onReplyCreated} .comment=${this.comment}></reply-form>
232+
<reply-form
233+
@reload=${this.onReplyCreated}
234+
.comment=${this.comment}
235+
></reply-form>
226236
</div>`
227237
: ''
228238
}

0 commit comments

Comments
 (0)