forked from halo-dev/plugin-comment-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch comments with related replies (halo-dev#106)
支持加载评论列表时同时加载对应回复。 <img width="895" alt="image" src="https://github.com/halo-dev/plugin-comment-widget/assets/21301288/a70d8490-fe9f-4361-a246-9f8638f5b646"> TODO: - [x] 在插件设置中提供设置,是否启用自动加载回复,以及默认加载条数 ```release-note 支持加载评论列表时同时加载对应回复。 ```
- Loading branch information
Showing
13 changed files
with
274 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,4 @@ build { | |
|
||
halo { | ||
version = "2.13.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { CommentVo, ReplyVo } from '@halo-dev/api-client'; | ||
import { CommentVo, ReplyVo, ReplyVoList } from '@halo-dev/api-client'; | ||
import { LitElement, css, html } from 'lit'; | ||
import { property, state } from 'lit/decorators.js'; | ||
import { repeat } from 'lit/directives/repeat.js'; | ||
import { consume } from '@lit/context'; | ||
import { baseUrlContext, toastContext } from './context'; | ||
import { baseUrlContext, replySizeContext, toastContext, withRepliesContext } from './context'; | ||
import './reply-item'; | ||
import './loading-block'; | ||
import './reply-form'; | ||
|
@@ -16,12 +16,29 @@ export class CommentReplies extends LitElement { | |
@property({ attribute: false }) | ||
baseUrl = ''; | ||
|
||
@consume({ context: withRepliesContext, subscribe: true }) | ||
@state() | ||
withReplies = false; | ||
|
||
@consume({ context: replySizeContext, subscribe: true }) | ||
@state() | ||
replySize = 10; | ||
|
||
@property({ type: Object }) | ||
comment: CommentVo | undefined; | ||
|
||
@property({ type: Boolean }) | ||
showReplyForm = false; | ||
|
||
@state() | ||
replies: ReplyVo[] = []; | ||
|
||
@state() | ||
page = 1; | ||
|
||
@state() | ||
hasNext = false; | ||
|
||
@state() | ||
loading = false; | ||
|
||
|
@@ -34,10 +51,8 @@ export class CommentReplies extends LitElement { | |
|
||
override render() { | ||
return html`<div class="replies__wrapper"> | ||
<reply-form @reload=${this.fetchReplies} .comment=${this.comment}></reply-form> | ||
${this.loading | ||
? html`<loading-block></loading-block>` | ||
: html` | ||
${this.replies.length | ||
? html` | ||
<div class="replies__list"> | ||
${repeat( | ||
this.replies, | ||
|
@@ -53,55 +68,115 @@ export class CommentReplies extends LitElement { | |
></reply-item>` | ||
)} | ||
</div> | ||
`} | ||
` | ||
: ''} | ||
${this.loading ? html`<loading-block></loading-block>` : ''} | ||
${this.hasNext && !this.loading | ||
? html`<div class="replies__next-wrapper"> | ||
<button @click=${this.fetchNext}>加载更多</button> | ||
</div>` | ||
: ''} | ||
</div>`; | ||
} | ||
|
||
onSetActiveQuoteReply(event: CustomEvent) { | ||
this.activeQuoteReply = event.detail.quoteReply; | ||
} | ||
|
||
async fetchReplies() { | ||
if (this.replies.length === 0) { | ||
async fetchReplies(options?: { append: boolean }) { | ||
try { | ||
this.loading = true; | ||
} | ||
|
||
try { | ||
// Reload replies list | ||
if (!options?.append) { | ||
this.page = 1; | ||
} | ||
|
||
const queryParams = [`page=${this.page || 0}`, `size=${this.replySize}`]; | ||
|
||
const response = await fetch( | ||
`${this.baseUrl}/apis/api.halo.run/v1alpha1/comments/${this.comment?.metadata.name}/reply` | ||
`${this.baseUrl}/apis/api.halo.run/v1alpha1/comments/${this.comment?.metadata.name}/reply?${queryParams.join('&')}` | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error('加载回复列表失败,请稍后重试'); | ||
} | ||
|
||
const data = await response.json(); | ||
this.replies = data.items; | ||
const data = (await response.json()) as ReplyVoList; | ||
|
||
if (options?.append) { | ||
this.replies = this.replies.concat(data.items); | ||
} else { | ||
this.replies = data.items; | ||
} | ||
|
||
this.hasNext = data.hasNext; | ||
this.page = data.page; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
this.toastManager?.error(error.message); | ||
} | ||
} finally { | ||
this.loading = false; | ||
} | ||
} | ||
|
||
this.loading = false; | ||
async fetchNext() { | ||
this.page++; | ||
this.fetchReplies({ append: true }); | ||
} | ||
|
||
override connectedCallback(): void { | ||
super.connectedCallback(); | ||
this.fetchReplies(); | ||
|
||
if (this.withReplies) { | ||
// TODO: Fix ts error | ||
// Needs @halo-dev/[email protected] | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.replies = this.comment?.replies.items; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.page = this.comment?.replies.page; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.hasNext = this.comment?.replies.hasNext; | ||
} else { | ||
this.fetchReplies(); | ||
} | ||
} | ||
|
||
static override styles = [ | ||
varStyles, | ||
baseStyles, | ||
css` | ||
.replies__wrapper { | ||
margin-top: 0.5em; | ||
} | ||
.replies__list { | ||
margin-top: 0.875em; | ||
} | ||
.replies__next-wrapper { | ||
display: flex; | ||
justify-content: center; | ||
margin: 0.5em 0; | ||
} | ||
.replies__next-wrapper button { | ||
border-radius: var(--base-border-radius); | ||
color: var(--base-color); | ||
font-size: 0.875em; | ||
display: inline-flex; | ||
align-items: center; | ||
font-weight: 600; | ||
padding: 0.4em 0.875em; | ||
transition-property: all; | ||
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); | ||
transition-duration: 0.15s; | ||
border: 1px solid transparent; | ||
} | ||
.replies__next-wrapper button:hover { | ||
background-color: var(--component-pagination-button-bg-color-hover); | ||
} | ||
`, | ||
]; | ||
} | ||
|
Oops, something went wrong.