From c6e89c0b86a98945a443fc221341a1526d2723ad Mon Sep 17 00:00:00 2001 From: Snowykami Date: Wed, 10 Sep 2025 12:34:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E8=AF=84=E8=AE=BA=E9=A1=B9=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=EF=BC=8C=E6=94=AF=E6=8C=81=E6=98=BE=E7=A4=BA=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/components/comment/comment-input.tsx | 3 +- web/src/components/comment/comment-item.tsx | 33 +++++++++++-- web/src/i18n/request.ts | 2 +- web/src/locales/zh-CN.json | 7 ++- web/src/utils/common/datetime.ts | 52 ++++++++++++++++++++ 5 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 web/src/utils/common/datetime.ts diff --git a/web/src/components/comment/comment-input.tsx b/web/src/components/comment/comment-input.tsx index 894d644..b0c669c 100644 --- a/web/src/components/comment/comment-input.tsx +++ b/web/src/components/comment/comment-input.tsx @@ -2,7 +2,6 @@ import { useToLogin, useToUserProfile } from "@/hooks/use-route"; import { User } from "@/models/user"; import { useTranslations } from "next-intl"; import { useState } from "react"; -import NeedLogin from "@/components/common/need-login"; import { toast } from "sonner"; import { getGravatarByUser } from "@/components/common/gravatar"; import { CircleUser } from "lucide-react"; @@ -60,7 +59,7 @@ export function CommentInput( return (
-
clickToUserProfile(user.username) : clickToLogin} className="flex-shrink-0 w-10 h-10 fade-in"> +
clickToUserProfile(user.username) : clickToLogin} className="cursor-pointer flex-shrink-0 w-10 h-10 fade-in"> {user ? getGravatarByUser(user) : null} {!user && }
diff --git a/web/src/components/comment/comment-item.tsx b/web/src/components/comment/comment-item.tsx index c94019f..9daf81d 100644 --- a/web/src/components/comment/comment-item.tsx +++ b/web/src/components/comment/comment-item.tsx @@ -1,6 +1,6 @@ import { useToLogin, useToUserProfile } from "@/hooks/use-route"; import { User } from "@/models/user"; -import { useTranslations } from "next-intl"; +import { useLocale, useTranslations } from "next-intl"; import { useState } from "react"; import { toast } from "sonner"; import { getGravatarByUser } from "@/components/common/gravatar"; @@ -13,6 +13,7 @@ import { CommentInput } from "./comment-input"; import { createComment, deleteComment, listComments, updateComment } from "@/api/comment"; import { OrderBy } from "@/models/common"; import config from "@/config"; +import { formatDateTime } from "@/utils/common/datetime"; export function CommentItem( @@ -32,8 +33,10 @@ export function CommentItem( setActiveInputId: (input: { id: number; type: 'reply' | 'edit' } | null) => void, } ) { - const t = useTranslations("Comment") - const commonT = useTranslations('Common') + const locale = useLocale(); + console.log("locale", locale); + const t = useTranslations("Comment"); + const commonT = useTranslations("Common"); const clickToUserProfile = useToUserProfile(); const clickToLogin = useToLogin(); const { confirming, onClick, onBlur } = useDoubleConfirm(); @@ -157,7 +160,27 @@ export function CommentItem( {getGravatarByUser(comment.user)}
-
clickToUserProfile(comment.user.username)} className="font-bold text-base text-slate-800 dark:text-slate-100 cursor-pointer fade-in-up">{comment.user.nickname}
+
+
clickToUserProfile(comment.user.username)} className="font-bold text-base text-slate-800 dark:text-slate-100 cursor-pointer fade-in-up"> + {comment.user.nickname} +
+ {formatDateTime({ + dateTimeString: comment.createdAt, + locale, + convertShortAgo: true, + unitI18n: { secondsAgo: commonT("secondsAgo"), minutesAgo: commonT("minutesAgo"), hoursAgo: commonT("hoursAgo"), daysAgo: commonT("daysAgo") } + })} + {comment.createdAt !== comment.updatedAt && + {t("edit_at", { + time: formatDateTime({ + dateTimeString: comment.updatedAt, + locale, + convertShortAgo: true, + unitI18n: { secondsAgo: commonT("secondsAgo"), minutesAgo: commonT("minutesAgo"), hoursAgo: commonT("hoursAgo"), daysAgo: commonT("daysAgo") } + }) + })}} +
+

{ isPrivate && @@ -169,7 +192,7 @@ export function CommentItem( {comment.content}

- {new Date(comment.updatedAt).toLocaleString()} + {/* 点赞按钮 */}