"use client"; import * as React from "react"; const MOBILE_BREAKPOINT = 768; // store เดียวทั้งแอป (แก้มือ — ดู CLAUDE.md): เดิมทุก component มี state + matchMedia listener ของตัวเอง // และ render สองรอบตอน mount (ตาราง 50 แถว = 50 listener/100 render) — ตอนนี้ listener ตัวเดียว const listeners = new Set<() => void>(); let mql: MediaQueryList | null = null; const emit = () => { for (const listener of listeners) listener(); }; function subscribe(listener: () => void) { listeners.add(listener); // เบราว์เซอร์เก่า/jsdom ที่ไม่มี matchMedia: ใช้ค่าตอนอ่าน (ไม่ติดตามการปรับขนาด) แทนที่จะพังทั้งหน้า if (!mql && typeof window !== "undefined" && typeof window.matchMedia === "function") { mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`); mql.addEventListener("change", emit); } return () => { listeners.delete(listener); if (listeners.size === 0 && mql) { mql.removeEventListener("change", emit); mql = null; } }; } const getSnapshot = () => window.innerWidth < MOBILE_BREAKPOINT; // server และ render แรกตอน hydrate = จอกว้าง (ตรงกันไม่ mismatch) · mount ทีหลัง (เปิด dialog) อ่านค่าจริงทันที const getServerSnapshot = () => false; export function useIsMobile() { return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); }