初始提交:姜十三论坛 Jiang13 Forum
轻量自用论坛,Go 单二进制 + React SPA 内嵌 + SQLite。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
55
frontend/src/utils/comment.ts
Normal file
55
frontend/src/utils/comment.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import type { Comment } from '../api/types';
|
||||
|
||||
export interface CommentNode {
|
||||
comment: Comment;
|
||||
children: CommentNode[];
|
||||
}
|
||||
|
||||
/** 评论显示昵称 */
|
||||
export function commentNick(c: Comment): string {
|
||||
if (c.user?.nickname) return c.user.nickname;
|
||||
if (c.guest_nick) return c.guest_nick;
|
||||
return '游客';
|
||||
}
|
||||
|
||||
/** 评论头像首字 */
|
||||
export function commentInitial(c: Comment): string {
|
||||
return commentNick(c)[0] || '?';
|
||||
}
|
||||
|
||||
/** 是否为游客评论 */
|
||||
export function isGuestComment(c: Comment): boolean {
|
||||
return !c.user_id || c.user_id === 0;
|
||||
}
|
||||
|
||||
/** 构建嵌套评论树(按 reply_to) */
|
||||
export function buildCommentTree(comments: Comment[]): CommentNode[] {
|
||||
const map = new Map<number, CommentNode>();
|
||||
const roots: CommentNode[] = [];
|
||||
|
||||
for (const c of comments) {
|
||||
map.set(c.id, { comment: c, children: [] });
|
||||
}
|
||||
|
||||
for (const c of comments) {
|
||||
const node = map.get(c.id)!;
|
||||
if (c.reply_to && map.has(c.reply_to)) {
|
||||
map.get(c.reply_to)!.children.push(node);
|
||||
} else {
|
||||
roots.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
return roots;
|
||||
}
|
||||
|
||||
/** 评论日期:当年显示 MM月DD日 */
|
||||
export function formatCommentDate(iso: string): string {
|
||||
const d = new Date(iso);
|
||||
const now = new Date();
|
||||
const pad = (n: number) => String(n).padStart(2, '0');
|
||||
if (d.getFullYear() === now.getFullYear()) {
|
||||
return `${pad(d.getMonth() + 1)}月${pad(d.getDate())}日`;
|
||||
}
|
||||
return `${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日`;
|
||||
}
|
||||
Reference in New Issue
Block a user