From f4fab6f727f509a7b4ebbdfc62a44fe49bada2a7 Mon Sep 17 00:00:00 2001 From: margrop <1731827+margrop@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:31:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AF=A6=E6=83=85=E9=A1=B5=E5=B1=80?= =?UTF-8?q?=E9=83=A8=E6=9B=B4=E6=96=B0=E5=90=8E=E5=88=B7=E6=96=B0=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E6=A0=A1=E9=AA=8C=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 上传配图、重生成答案等接口只返回局部字段,页面异步回读最新 updatedAt - 保存前等待回读完成,避免本次修改被误判为他人并发编辑 - 保留共享成员并发编辑的 409 冲突校验 --- src/app/error-items/[id]/page.tsx | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/app/error-items/[id]/page.tsx b/src/app/error-items/[id]/page.tsx index cb0d5418..e4e6bf37 100644 --- a/src/app/error-items/[id]/page.tsx +++ b/src/app/error-items/[id]/page.tsx @@ -1,6 +1,6 @@ "use client"; -import { useCallback, useEffect, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import Link from "next/link"; import { useParams, useRouter } from "next/navigation"; import { ArrowLeft, FileQuestion, RefreshCw, Trash2 } from "lucide-react"; @@ -53,11 +53,15 @@ export default function ErrorDetailPage() { // 复习状态由举一反三记录和掌握概率决定,随掌握面板的数据更新 const [masteryStatus, setMasteryStatus] = useState(null); const [savingDifficulty, setSavingDifficulty] = useState(false); + // 保存冲突校验使用的最新读取版本;其他接口只返回局部字段时异步回读刷新 + const updatedAtRef = useRef(undefined); + const pendingUpdatedAt = useRef | null>(null); const fetchItem = useCallback(async (id: string) => { try { const data = await apiClient.get(`/api/error-items/${id}`); setItem(data); + updatedAtRef.current = data.updatedAt; } catch (error) { frontendLogger.error('[ErrorDetail]', 'Failed to fetch error item', { error }); toast.error(t.common?.messages?.loadFailed || 'Failed to load item'); @@ -85,15 +89,32 @@ export default function ErrorDetailPage() { }, [params.id, fetchItem]); const updateItem = useCallback((fields: Partial) => { + if (fields.updatedAt !== undefined) { + updatedAtRef.current = fields.updatedAt; + } setItem((current) => current && { ...current, ...fields }); - }, []); + // 局部更新接口(上传配图、重生成答案等)不返回 updatedAt:回读最新版本, + // 避免随后保存时把本次修改误判为他人并发编辑。 + if (fields.updatedAt === undefined && params.id) { + pendingUpdatedAt.current = apiClient.get(`/api/error-items/${params.id as string}`) + .then((data) => { + updatedAtRef.current = data.updatedAt; + setItem((current) => current && { ...current, updatedAt: data.updatedAt }); + }) + .catch((error) => frontendLogger.warn('[ErrorDetail]', 'Failed to refresh update timestamp', { error })); + } + }, [params.id]); // 保存字段并合并到页面状态,失败时抛出,由各卡片提示;带上读取时的 updatedAt 供并发编辑校验 const saveFields = async (fields: Partial) => { if (!item) return; + if (pendingUpdatedAt.current) { + await pendingUpdatedAt.current; + pendingUpdatedAt.current = null; + } const updated = await apiClient.put>(`/api/error-items/${item.id}`, { ...fields, - ...(item.updatedAt ? { expectedUpdatedAt: item.updatedAt } : {}), + ...(updatedAtRef.current ? { expectedUpdatedAt: updatedAtRef.current } : {}), }); updateItem({ ...fields, ...(updated.updatedAt ? { updatedAt: updated.updatedAt } : {}) }); };