eCourt-note/lib/annotate-events.ts

56 lines
2.5 KiB
TypeScript
Raw Normal View History

import type { AnnotationDTO, BookmarkDTO, ReplyDTO } from "@/db/schema";
// pub/sub in-process (PLAN.md §4): Server Actions publish หลังเขียน DB สำเร็จ
refactor(routes): move routes under /note, APIs under /api/note เตรียม basePath /docedit เป็นพื้นที่กลางให้แอปพี่น้อง (wordaddin ใน อนาคต) — เดิม basePath ครอบ /docedit/note ทั้งหมดจนแชร์ prefix ไม่ได้ (Next มี basePath ได้ทางเดียว) และจัด API แยกต้นไม้ตามที่ผู้ใช้ขอ (1) โครงสร้างใหม่: basePath = /docedit + route หน้าทั้งหมดย้ายเข้า app/note/** + API 4 เส้นย้ายเข้า app/api/note/** + statics ย้ายเข้า public/note/** (samples, uploads, pdf.worker) — หน้า/statics URL ปลายทาง คงเดิมเป๊ะ (/docedit/note/…) ส่วน URL API เปลี่ยนเป็น /docedit/api/note/… ตามคำขอผู้ใช้ (N10 endpoint เปลี่ยน — ต้องแจ้ง backend e-Court ก่อนใช้) (2) กลไก: withBasePath (utils/base-path.ts) เติม base + app path /note ยกเว้น path ที่ขึ้นต้น /api/ เติมแค่ base (caller เขียน /api/note/… เต็ม); resolvePublicPdfPath root ที่ public/note (traversal guard เดิม); middleware matcher ยกเว้นใหม่ (note/login, api/note, note/uploads, note/samples, note/pdf.worker, _next, fonts, favicon) + redirect /note/login; next.config เพิ่ม redirects /docedit -> /docedit/note; navigation ภายใน 19 จุด + revalidatePath 3 จุด ใส่ prefix /note; pdf_url ใน DB เป็น logical path ไม่แก้แม้แต่ตัวเดียว (seed/upload เขียนค่าเดิม, browser เติมผ่าน withBasePath, disk เติมผ่าน guard) (3) ตามแก้: patch-pdf-worker (DEST public/note + mkdirSync ปลายทาง), seed-samples (SAMPLES_DIR), uploadFile (UPLOAD_DIR), Dockerfile/ .dockerignore/.gitignore/docker-compose.test.yml, tests 2 ไฟล์ที่อ้าง disk path จริง, sync เอกสารทั้งหมด (CLAUDE.md invariant #6/#12 + tree, README, docs/API-DOCUMENTS.md, .claude/rules/*, sync-db skill) (4) !! ตอน deploy ต้องย้าย volume ตาม: mount /app/public/uploads -> /app/public/note/uploads + ย้ายไฟล์ใน volume ให้ตรงตำแหน่ง ไม่งั้นไฟล์ ที่เคยอัปโหลด (พร้อม annotation ที่ผูกอยู่) 404 ทันทีที่สลับ image (5) verify: build ผ่าน (route table /api/note/** + /note/** ครบ), tsc, vitest 215/215, curl ครบทุก endpoint (login/redirect chain, worker, samples, uploads, SSE, api/pdf, export, N8 register, N10 bundle + deep link), Playwright browser จริง (canvas + textLayer ไทย render, browser ยิง URL ใหม่ทั้งหมดไม่มี request ไป URL เก่า), plan-reviewer + code-reviewer 3 รายงาน 0 blocking Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-06 14:55:07 +07:00
// → SSE route (app/api/note/annotations/stream) subscribe แล้วยิง event ให้ client ที่เปิดไฟล์เดียวกัน
// ⚠ in-memory ของ process เดียว (dev / next start ธรรมดา) — ถ้าวันหนึ่งรันหลาย instance
// ต้องเพิ่มตัวกลาง (Redis pub/sub หรือ Postgres LISTEN/NOTIFY) ก่อน ห้ามแอบ sync ผ่าน DB polling
export type AnnotateEvent =
| { table: "annotations"; action: "create"; row: AnnotationDTO }
| { table: "annotations"; action: "update"; row: AnnotationDTO }
| { table: "annotations"; action: "delete"; id: string }
| { table: "replies"; action: "create"; row: ReplyDTO }
| { table: "replies"; action: "delete"; id: string }
| { table: "bookmarks"; action: "create"; row: BookmarkDTO }
| { table: "bookmarks"; action: "update"; row: BookmarkDTO }
| { table: "bookmarks"; action: "delete"; id: string };
type Listener = (event: AnnotateEvent) => void;
// ⚠ เก็บบน globalThis เพราะ Next dev bundle "use server" (actions) กับ route handler
// แบบคนละส่วน — module-level state จะถูก instantiate เป็น 2 อินสแตนซ์แล้ว event ไม่ไหลถึงกัน
// (ยังช่วยให้ state รอดจาก HMR recompile ด้วย)
type EventsGlobal = { __annotateEventListeners?: Map<string, Set<Listener>> };
const g = globalThis as typeof globalThis & EventsGlobal;
const listenersByFile: Map<string, Set<Listener>> =
g.__annotateEventListeners ?? (g.__annotateEventListeners = new Map());
/** คืนฟังก์ชัน unsubscribe */
export function subscribeAnnotateEvents(
fileId: string,
listener: Listener,
): () => void {
let set = listenersByFile.get(fileId);
if (!set) {
set = new Set();
listenersByFile.set(fileId, set);
}
set.add(listener);
return () => {
set.delete(listener);
if (set.size === 0) listenersByFile.delete(fileId);
};
}
export function publishAnnotateEvent(fileId: string, event: AnnotateEvent): void {
const set = listenersByFile.get(fileId);
if (!set) return;
for (const listener of Array.from(set)) {
try {
listener(event);
} catch (err) {
console.error("annotate-events listener error", err);
}
}
}