generated from halo-dev/plugin-starter
/
comment-replies.ts
190 lines (160 loc) · 5.02 KB
/
comment-replies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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, replySizeContext, toastContext, withRepliesContext } from './context';
import './reply-item';
import './loading-block';
import './reply-form';
import varStyles from './styles/var';
import baseStyles from './styles/base';
import { ToastManager } from './lit-toast';
export class CommentReplies extends LitElement {
@consume({ context: baseUrlContext })
@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;
@state()
activeQuoteReply: ReplyVo | undefined = undefined;
@consume({ context: toastContext, subscribe: true })
@state()
toastManager: ToastManager | undefined;
override render() {
return html`<div class="replies__wrapper">
${this.replies.length
? html`
<div class="replies__list">
${repeat(
this.replies,
(item) => item.metadata.name,
(item) =>
html`<reply-item
.comment=${this.comment}
.reply="${item}"
.replies=${this.replies}
.activeQuoteReply=${this.activeQuoteReply}
@set-active-quote-reply=${this.onSetActiveQuoteReply}
@reload=${this.fetchReplies}
></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(options?: { append: boolean }) {
try {
this.loading = true;
// 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?${queryParams.join('&')}`
);
if (!response.ok) {
throw new Error('加载回复列表失败,请稍后重试');
}
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;
}
}
async fetchNext() {
this.page++;
this.fetchReplies({ append: true });
}
override connectedCallback(): void {
super.connectedCallback();
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__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);
}
`,
];
}
customElements.get('comment-replies') || customElements.define('comment-replies', CommentReplies);
declare global {
interface HTMLElementTagNameMap {
'comment-replies': CommentReplies;
}
}