Skip to content

Commit 013a353

Browse files
committed
Refine ui
Signed-off-by: Ryan Wang <i@ryanc.cc>
1 parent b8dd7e6 commit 013a353

5 files changed

Lines changed: 226 additions & 130 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class BaseForm extends LitElement {
250250
}
251251
<div class="form__actions">
252252
${
253-
this.showCaptcha
253+
this.showCaptcha && this.captcha
254254
? html`
255255
<div class="form__action--captcha">
256256
<input
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import { css, html, LitElement } from 'lit';
2+
import './icons/icon-loading';
3+
import type { CommentVoList } from '@halo-dev/api-client';
4+
import { consume } from '@lit/context';
5+
import { msg } from '@lit/localize';
6+
import { state } from 'lit/decorators.js';
7+
import { repeat } from 'lit/directives/repeat.js';
8+
import {
9+
baseUrlContext,
10+
configMapDataContext,
11+
groupContext,
12+
kindContext,
13+
nameContext,
14+
toastContext,
15+
versionContext,
16+
} from './context';
17+
import type { ToastManager } from './lit-toast';
18+
import type { ConfigMapData } from './types';
19+
import './comment-pagination';
20+
import './comment-item';
21+
import './loading-block';
22+
23+
export class CommentList extends LitElement {
24+
@consume({ context: baseUrlContext })
25+
@state()
26+
baseUrl = '';
27+
28+
@consume({ context: groupContext })
29+
@state()
30+
group = '';
31+
32+
@consume({ context: kindContext })
33+
@state()
34+
kind = '';
35+
36+
@consume({ context: nameContext })
37+
@state()
38+
name = '';
39+
40+
@consume({ context: versionContext })
41+
@state()
42+
version = '';
43+
44+
@consume({ context: configMapDataContext })
45+
@state()
46+
configMapData: ConfigMapData | undefined;
47+
48+
@consume({ context: toastContext, subscribe: true })
49+
@state()
50+
toastManager: ToastManager | undefined;
51+
52+
@state()
53+
comments: CommentVoList = {
54+
page: 1,
55+
size: 20,
56+
total: 0,
57+
items: [],
58+
first: true,
59+
last: false,
60+
hasNext: false,
61+
hasPrevious: false,
62+
totalPages: 0,
63+
};
64+
65+
@state()
66+
loading = false;
67+
68+
get shouldDisplayPagination() {
69+
if (this.loading) {
70+
return false;
71+
}
72+
73+
return this.comments.hasNext || this.comments.hasPrevious;
74+
}
75+
76+
async onPageChange(e: CustomEvent) {
77+
const data = e.detail;
78+
this.comments.page = data.page;
79+
await this.fetchComments({ scrollIntoView: true });
80+
}
81+
82+
override connectedCallback(): void {
83+
super.connectedCallback();
84+
this.fetchComments();
85+
}
86+
87+
async fetchComments(options?: { page?: number; scrollIntoView?: boolean }) {
88+
const { page, scrollIntoView } = options || {};
89+
try {
90+
if (this.comments.items.length === 0) {
91+
this.loading = true;
92+
}
93+
94+
if (page) {
95+
this.comments.page = page;
96+
}
97+
98+
const queryParams = [
99+
`group=${this.group}`,
100+
`kind=${this.kind}`,
101+
`name=${this.name}`,
102+
`page=${this.comments.page}`,
103+
`size=${this.configMapData?.basic.size || 20}`,
104+
`version=${this.version}`,
105+
`withReplies=${this.configMapData?.basic.withReplies || false}`,
106+
`replySize=${this.configMapData?.basic.replySize || 10}`,
107+
];
108+
109+
const response = await fetch(
110+
`${this.baseUrl}/apis/api.halo.run/v1alpha1/comments?${queryParams.join(
111+
'&'
112+
)}`
113+
);
114+
115+
if (!response.ok) {
116+
throw new Error(
117+
msg('Failed to load comment list, please try again later')
118+
);
119+
}
120+
121+
this.comments = await response.json();
122+
} catch (error) {
123+
if (error instanceof Error) {
124+
this.toastManager?.error(error.message);
125+
}
126+
} finally {
127+
this.loading = false;
128+
129+
if (scrollIntoView) {
130+
this.scrollIntoView({
131+
block: 'start',
132+
inline: 'start',
133+
behavior: 'smooth',
134+
});
135+
}
136+
}
137+
}
138+
139+
override render() {
140+
return html`${
141+
this.loading
142+
? html` <loading-block></loading-block>`
143+
: html`
144+
<div class="comment-widget__wrapper">
145+
<div class="comment-widget__stats">
146+
<span>${msg(html`${this.comments.total} Comments`)}</span>
147+
</div>
148+
149+
<div class="comment-widget__list">
150+
${repeat(
151+
this.comments.items,
152+
(item) => item.metadata.name,
153+
(item) => html` <comment-item .comment=${item}></comment-item>`
154+
)}
155+
</div>
156+
</div>
157+
`
158+
}
159+
${
160+
this.shouldDisplayPagination
161+
? html`
162+
<comment-pagination
163+
.total=${this.comments.total}
164+
.page=${this.comments.page}
165+
.size=${this.comments.size}
166+
@page-change=${this.onPageChange}
167+
></comment-pagination>
168+
`
169+
: ''
170+
}`;
171+
}
172+
173+
static override styles = css`
174+
.comment-widget__wrapper {
175+
margin-top: 1.2em;
176+
}
177+
178+
.comment-widget__stats {
179+
color: var(--base-color);
180+
font-size: 0.875em;
181+
margin: 0.875em 0;
182+
font-weight: 500;
183+
}
184+
`;
185+
}
186+
187+
customElements.get('comment-list') ||
188+
customElements.define('comment-list', CommentList);
189+
190+
declare global {
191+
interface HTMLElementTagNameMap {
192+
'comment-list': CommentList;
193+
}
194+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type { ConfigMapData } from './types';
1515

1616
export class CommentReplies extends LitElement {
1717
@consume({ context: baseUrlContext })
18-
@property({ attribute: false })
18+
@state()
1919
baseUrl = '';
2020

2121
@consume({ context: configMapDataContext })

0 commit comments

Comments
 (0)