refactor(announcements,messaging,notifications): V1+V2 审计重构 — i18n 命名空间独立 + 通知标题 i18n 化 + 服务端过滤 + 编排下沉 + 表单错误展示 + 架构图同步

V1 改进(已完成):
- P0-4/P1-4/P1-5: 通知组件和 CRUD Action 从 messaging 迁移至 notifications 模块
- P1-5: 新增 getMessagesPageData / getAdminAnnouncementsPageData 编排函数
- P1-6: announcements schema 添加 superRefine 条件校验
- P1-7: 新增 useMessageSearch hook(防抖 + 请求竞态取消)+ 客户端分页 UI
- P1-9: deleteMessage 事务化
- P2-11: 全模块 trackEvent 埋点
- 全模块 i18n 接入 + Error Boundary + a11y 改进

V2 改进(本次完成):
- V2-P0-1: 通知 i18n 命名空间独立(notifications.json),useTranslations 从 "messages" 切换到 "notifications"
- V2-P0-2: 公告/消息通知标题 i18n 化,Server Action 中使用 getTranslations 生成通知标题
- V2-P1-1: AnnouncementList 纯服务端过滤,移除客户端 useState/useMemo
- V2-P1-2: MessageList 客户端过滤仅在初始数据时执行,搜索结果由服务端按 tab 过滤
- V2-P1-3: 消息详情页编排下沉,新增 getMessageDetailPageData 编排函数
- V2-P1-4: 表单服务端校验错误展示(fieldErrors + aria-invalid)
- V2-P2-1: 轮询间隔常量化(POLL_INTERVAL_MS)
- V2-P2-2: 架构图同步(004 + 005)
This commit is contained in:
SpecialX
2026-06-22 18:43:12 +08:00
parent 6d7838a210
commit 1fe30984b6
29 changed files with 1252 additions and 351 deletions

View File

@@ -23,6 +23,8 @@ import {
import { sendMessageAction } from "../actions"
import type { RecipientOption } from "../types"
type FieldErrors = Record<string, string[]>
export function MessageCompose({
recipients,
parentMessageId,
@@ -40,10 +42,16 @@ export function MessageCompose({
const router = useRouter()
const [isWorking, setIsWorking] = useState(false)
const [receiverId, setReceiverId] = useState(defaultReceiverId ?? "")
const [fieldErrors, setFieldErrors] = useState<FieldErrors>({})
const getFieldError = (field: string): string | null => {
const errs = fieldErrors[field]
return errs && errs.length > 0 ? errs[0] : null
}
const handleSubmit = async (formData: FormData) => {
if (!receiverId) {
toast.error(t("messages.selectRecipient"))
toast.error(t("form.selectRecipient"))
return
}
formData.set("receiverId", receiverId)
@@ -52,6 +60,7 @@ export function MessageCompose({
}
setIsWorking(true)
setFieldErrors({})
try {
const res = await sendMessageAction(null, formData)
if (res.success) {
@@ -59,6 +68,10 @@ export function MessageCompose({
router.push("/messages")
router.refresh()
} else {
// 展示字段级错误(来自 Zod 校验)
if (res.errors) {
setFieldErrors(res.errors)
}
toast.error(res.message || t("messages.sendFailed"))
}
} catch {
@@ -85,7 +98,7 @@ export function MessageCompose({
<div className="grid gap-2">
<Label htmlFor="receiverId">{t("form.to")}</Label>
<Select value={receiverId} onValueChange={setReceiverId} disabled={!!defaultReceiverId}>
<SelectTrigger>
<SelectTrigger aria-invalid={!!getFieldError("receiverId")}>
<SelectValue placeholder={t("form.toPlaceholder")} />
</SelectTrigger>
<SelectContent>
@@ -98,6 +111,9 @@ export function MessageCompose({
</SelectContent>
</Select>
<input type="hidden" name="receiverId" value={receiverId} />
{getFieldError("receiverId") ? (
<p className="text-destructive text-xs">{getFieldError("receiverId")}</p>
) : null}
</div>
<div className="grid gap-2">
@@ -108,7 +124,11 @@ export function MessageCompose({
placeholder={t("form.subjectPlaceholder")}
defaultValue={defaultSubject ?? ""}
maxLength={255}
aria-invalid={!!getFieldError("subject")}
/>
{getFieldError("subject") ? (
<p className="text-destructive text-xs">{getFieldError("subject")}</p>
) : null}
</div>
<div className="grid gap-2">
@@ -119,7 +139,11 @@ export function MessageCompose({
placeholder={t("form.contentPlaceholder")}
className="min-h-[200px]"
required
aria-invalid={!!getFieldError("content")}
/>
{getFieldError("content") ? (
<p className="text-destructive text-xs">{getFieldError("content")}</p>
) : null}
</div>
<CardFooter className="justify-end gap-2 px-0">