fix(note): address code review findings

- restoreVersion: ผ่าน validateAnnotationData ก่อน insert สาขา
  "ต้นทางถูกลบ" ให้สมมาตรกับสาขา update (invariant #8)
- PDFViewer: track timer ล้าง exportError ใน ref — กัน export พัง
  สองรอบติดกันแล้ว timer รอบแรกเคลียร์ error รอบหลังก่อนเวลา
- useHistory: เพิ่ม onError — undo/redo ล้มแสดง toast ผ่าน
  showActionError แทนหายเงียบ ๆ
- DebugConsole: ไม่โหลด Eruda จาก CDN ใน production build
  (ระบบ intranet + token อยู่ใน origin เดียวกัน)
- NoteHome: ตัด dead state session ที่ render ไม่ได้อ่าน
- note-auth: export SSO_REQUIRED_BANNER ให้หน้า gate ใช้ตรง ๆ
  ถอด non-null assertion ใน AnnotateView + bookmarkDepthOf

Verify: npx tsc --noEmit ผ่าน · vitest 317/317 ผ่าน

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-11-02 10:01:00 +07:00
parent d37bc2a3f0
commit 2d466efd4f
7 changed files with 50 additions and 17 deletions

View file

@ -12,11 +12,18 @@ export type HistoryEntry = {
redo: () => Promise<void>;
};
export function useHistory() {
export function useHistory(options?: {
/** เรียกเมื่อ undo/redo ล้มเหลว (เน็ตสะดุด/session หมดอายุ ฯลฯ) — entry ถูกข้าม
* ไปแล้วโดย hook ผู้ใช้ควรเห็น toast ของ caller ไม่ใช่เงียบหายไปเฉย ๆ */
onError?: (err: unknown, kind: "undo" | "redo") => void;
}) {
// source of truth เป็น ref — state ที่เปลี่ยนแค่ไว้ trigger re-render ปุ่ม disabled
const undoRef = useRef<HistoryEntry[]>([]);
const redoRef = useRef<HistoryEntry[]>([]);
const [, bump] = useState(0);
// เก็บ callback ล่าสุดของ caller ใน ref — callback ที่ส่งเข้ามาเปลี่ยนทุก render ได้
const onErrorRef = useRef(options?.onError);
onErrorRef.current = options?.onError;
/** บันทึก action ที่สำเร็จ — action ใหม่เคลียร์ redo stack เสมอ */
const push = useCallback((entry: HistoryEntry) => {
@ -34,6 +41,7 @@ export function useHistory() {
} catch (err) {
// เช่น undo ของ create แต่ row ถูกลบไปแล้วโดยแท็บอื่น → ข้าม ไม่ crash (§5)
console.error("undo ไม่สำเร็จ — ข้ามรายการนี้", err);
onErrorRef.current?.(err, "undo");
}
bump((n) => n + 1);
}, []);
@ -46,6 +54,7 @@ export function useHistory() {
undoRef.current.push(entry);
} catch (err) {
console.error("redo ไม่สำเร็จ — ข้ามรายการนี้", err);
onErrorRef.current?.(err, "redo");
}
bump((n) => n + 1);
}, []);