fix(contacts): ช่องทางติดต่อกดใช้ได้จริง (tel:/mailto:/โซเชียล) + ซ่อมข้อมูลเดิม

ปัญหา: href ในข้อมูลดิบไม่สม่ำเสมอ (ว่าง / www. ไม่มี scheme / เบอร์ไม่มี tel: /
facebook ติด fbclid) ทำให้กดแล้วไม่เกิดอะไร และปุ่มโทรใช้ไม่ได้

- app/lib/contact-link.ts: resolveContact() จับคู่ลิงก์ตามชนิด
  · เบอร์ → tel:+66… (แปลง 0 นำหน้า, ตัดขีด/ช่องว่าง/วงเล็บ)
  · อีเมล → mailto: · LINE @id → line.me · เว็บ → https + ตัด fbclid/_rdc/utm_*
- ContactPanel: ไอคอนตามชนิด (phone/mail/fb/ig/line/ticket/globe),
  tel/mailto เปิดแท็บเดิม, เว็บเปิดแท็บใหม่; แถวที่ไม่มีลิงก์แสดงเป็นข้อความ (ไม่หลอกให้กด)
- sanitizeCinemas: คำนวณ href ใหม่ทุกครั้งที่บันทึก → ข้อมูลเก่าไม่กลับมาเพี้ยน
- ContactsEditor: auto-format ตอนออกจากช่อง + พรีวิว "✓ กดโทรได้ → tel:+66…"
- scripts/audit-contacts.mjs [--fix]: ตรวจ/ซ่อมทั้งไฟล์ (แก้ไป 13 จุด, สำรองข้อมูลแล้ว)
- contact-link.spec.ts 13 tests · docs: admin-guide/api/DESIGN
This commit is contained in:
Bkk1234 committed 2026-11-15 23:56:46 +07:00
1 parent 2c5918a614
commit 42572020d8
9 files changed
+656 -26

No files matched your search

+45 -6
View File
@@ -1,6 +1,7 @@
import { IconPlus, IconTrash } from "@tabler/icons-react";
import type { ContactChannel } from "@/lib/cinemas";
import { resolveContact } from "@/lib/contact-link";
/**
* แก้ช่องทางติดต่อของการ์ด "สอบถามข้อมูลและจองตั๋ว" ต่อโรง
@@ -27,7 +28,12 @@ type Props = {
labelClass: string;
};
export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: Props) {
export function ContactsEditor({
contacts,
onChange,
inputClass,
labelClass,
}: Props) {
function updateAt(index: number, patch: Partial<ContactChannel>) {
onChange(
contacts.map((item, i) => (i === index ? { ...item, ...patch } : item)),
@@ -56,7 +62,9 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
value={contact.label}
placeholder="Website"
list="contact-label-options"
onChange={(event) => updateAt(index, { label: event.target.value })}
onChange={(event) =>
updateAt(index, { label: event.target.value })
}
/>
</label>
<label className={labelClass}>
@@ -65,20 +73,32 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
className={inputClass}
value={contact.value}
placeholder="www.housesamyan.com"
onChange={(event) => updateAt(index, { value: event.target.value })}
onChange={(event) =>
updateAt(index, { value: event.target.value })
}
/>
</label>
<label className={labelClass}>
ลิงก์ (ไม่บังคับ)
ลิงก์ (ไม่บังคับ — ระบบเติมให้อัตโนมัติ)
<input
className={inputClass}
value={contact.href ?? ""}
placeholder="https://… หรือ tel:+66…"
onChange={(event) =>
updateAt(index, {
href: event.target.value.trim() === "" ? null : event.target.value,
href:
event.target.value.trim() === ""
? null
: event.target.value,
})
}
onBlur={() => {
// จัดรูปแบบลิงก์ให้กดใช้ได้จริง (tel:/mailto:/https:) ทันทีที่ออกจากช่อง
const resolved = resolveContact(contact);
if (resolved.href !== (contact.href ?? null)) {
updateAt(index, { href: resolved.href });
}
}}
/>
</label>
<div className="flex items-end">
@@ -91,6 +111,23 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
<IconTrash size={14} />
</button>
</div>
<span className="text-[11px] text-[#777] sm:col-span-4">
{(() => {
const resolved = resolveContact(contact);
if (!resolved.href) {
return contact.value.trim() === ""
? "ใส่ข้อความหรือเบอร์ก่อน — ระบบจะสร้างลิงก์ให้"
: "⚠ ค่านี้ยังใช้เป็นลิงก์ไม่ได้ (ต้องเป็นเบอร์ / อีเมล / โดเมน / URL)";
}
const label =
resolved.kind === "tel"
? "กดโทรได้"
: resolved.kind === "mailto"
? "เปิดอีเมล"
: "เปิดเว็บ (แท็บใหม่)";
return `✓ ${label} → ${resolved.href}`;
})()}
</span>
</div>
))}
@@ -103,7 +140,9 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
<button
type="button"
className={`${smallButtonClass} w-fit`}
onClick={() => onChange([...contacts, { label: "", value: "", href: null }])}
onClick={() =>
onChange([...contacts, { label: "", value: "", href: null }])
}
>
<IconPlus size={14} />
เพิ่มช่องทางติดต่อ