/** * แปลง "ช่องทางติดต่อ" ของโรงหนังให้เป็นลิงก์ที่กดใช้ได้จริง * * ปัญหาเดิม: ข้อมูลเก็บ `href` ดิบจากหลังบ้าน (บางทีว่าง, บางทีเป็น `www.…` ไม่มี scheme, * เบอร์โทรไม่ขึ้นต้นด้วย `tel:`) → หน้าเว็บได้ `` ที่กดแล้วไม่เกิดอะไร * * กติกาที่นี่ (ใช้ทั้งตอนบันทึกในหลังบ้านและตอนแสดงผล): * - เบอร์โทร → `tel:+66…` (แปลง 0 นำหน้าเป็น +66, ตัดขีด/วงเล็บ/ช่องว่าง) * - อีเมล → `mailto:` * - LINE ID (`@abc`) → `https://line.me/R/ti/p/~abc` * - เว็บ/โซเชียล → บังคับ `https://`, แปลง `web.facebook.com` → `www.facebook.com` * และตัดพารามิเตอร์ติดตาม (fbclid, _rdc, utm_*, igshid) */ export type ContactKind = "tel" | "mailto" | "web"; export type ResolvedContact = { /** href พร้อมใช้ — null = ไม่มีข้อมูลที่ใช้เป็นลิงก์ได้ */ href: string | null; kind: ContactKind | null; /** true = ลิงก์ออกเว็บนอก (เปิดแท็บใหม่) */ external: boolean; }; const EMPTY_VALUES = new Set([ "-", "–", "—", "n/a", "na", "null", "undefined", "ไม่มี", "ไม่มีข้อมูล", ]); const TRACKING_PARAMS = [ "fbclid", "gclid", "_rdc", "_rdr", "igshid", "ref", "ref_src", "mibextid", ]; function isTrackingParam(name: string): boolean { const key = name.toLowerCase(); return TRACKING_PARAMS.includes(key) || key.startsWith("utm_"); } /** แปลงเบอร์เป็นรูปแบบที่กดโทรได้: `+66812345678` (คืน null ถ้าไม่ใช่เบอร์ที่ใช้ได้) */ export function normalizePhone(raw: string): string | null { const trimmed = raw.trim(); if (trimmed === "") return null; const hasPlus = trimmed.startsWith("+"); const digits = trimmed.replace(/[^\d]/g, ""); if (digits.length < 8 || digits.length > 15) return null; if (hasPlus) return `+${digits}`; if (digits.startsWith("66")) return `+${digits}`; if (digits.startsWith("0")) return `+66${digits.slice(1)}`; return `+${digits}`; } /** ดูว่าข้อความนี้ "หน้าตาเป็นเบอร์โทร" ไหม (ใช้ตัดสินก่อนถือว่าเป็นเว็บ) */ export function looksLikePhone(raw: string): boolean { const trimmed = raw.trim(); if (trimmed === "") return false; if (/[a-z@]/i.test(trimmed)) return false; const digits = trimmed.replace(/\D/g, ""); return digits.length >= 8 && digits.length <= 15; } function stripTracking(url: URL): URL { const keep = new URLSearchParams(); url.searchParams.forEach((value, key) => { if (!isTrackingParam(key)) keep.append(key, value); }); const query = keep.toString(); url.search = query === "" ? "" : `?${query}`; return url; } function normalizeWebUrl(raw: string): string | null { let candidate = raw.trim(); if (candidate === "") return null; // LINE ID แบบ @id if (/^@[\w.\-_]+$/.test(candidate)) { return `https://line.me/R/ti/p/~${candidate.slice(1)}`; } if (/^line:/i.test(candidate)) { candidate = candidate.replace(/^line:\/\//i, "https://line.me/R/ti/p/~"); } if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(candidate)) { // `www.x.com` / `x.com/path` / `x.com` → https if (!/^[\w-]+(\.[\w-]+)+/.test(candidate)) return null; candidate = `https://${candidate}`; } try { const url = new URL(candidate); if (url.protocol === "http:") url.protocol = "https:"; if (url.hostname === "web.facebook.com") url.hostname = "www.facebook.com"; url.hostname = url.hostname.toLowerCase(); return stripTracking(url).toString(); } catch { return null; } } function looksLikeEmail(raw: string): boolean { return /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(raw.trim()); } /** * แปลงช่องทางติดต่อหนึ่งรายการ → ลิงก์ที่ใช้ได้ * ลำดับความเชื่อ: `href` ที่ผู้ใช้กรอก → ลักษณะของ `value` → label */ export function resolveContact(channel: { label?: string; value?: string; href?: string | null; }): ResolvedContact { const value = (channel.value ?? "").trim(); const href = (channel.href ?? "").trim(); const label = (channel.label ?? "").trim().toLowerCase(); const candidates = [href, value].filter( (item) => item !== "" && !EMPTY_VALUES.has(item.toLowerCase()), ); if (candidates.length === 0) return { href: null, kind: null, external: false }; for (const candidate of candidates) { const lower = candidate.toLowerCase(); // 1) เบอร์โทรที่ระบุชัด if (lower.startsWith("tel:")) { const phone = normalizePhone(candidate.slice(4)); if (phone) return { href: `tel:${phone}`, kind: "tel", external: false }; } // 2) อีเมล if (lower.startsWith("mailto:")) { const address = candidate.slice(7).trim(); if (looksLikeEmail(address)) { return { href: `mailto:${address}`, kind: "mailto", external: false }; } } if (looksLikeEmail(candidate)) { return { href: `mailto:${candidate}`, kind: "mailto", external: false }; } // 3) ลิงก์เว็บ/โซเชียล (ตัดสินจาก scheme หรือรูปทรงโดเมน) if ( /^https?:\/\//i.test(candidate) || /^line:/i.test(candidate) || /^@[\w.\-_]+$/.test(candidate) || /^[\w-]+(\.[\w-]+)+(\/|$)/.test(candidate) ) { const url = normalizeWebUrl(candidate); if (url) return { href: url, kind: "web", external: true }; } // 4) เบอร์โทรที่ไม่ได้ใส่ tel: if ( looksLikePhone(candidate) && /call|โทร|phone|tel|เบอร์|มือถือ/.test(label + " " + value) ) { const phone = normalizePhone(candidate); if (phone) return { href: `tel:${phone}`, kind: "tel", external: false }; } if (looksLikePhone(candidate)) { const phone = normalizePhone(candidate); if (phone) return { href: `tel:${phone}`, kind: "tel", external: false }; } } return { href: null, kind: null, external: false }; } /** ค่าที่ควรบันทึกกลับลง `href` (ใช้ตอน sanitize/หลังบ้าน) */ export function contactHref(channel: { label?: string; value?: string; href?: string | null; }): string | null { return resolveContact(channel).href; }