Files
web-alterflixhub/app/components/admin/ContactsEditor.tsx
T
Bkk1234 42572020d8 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
2026-11-15 23:56:46 +07:00

153 lines
5.6 KiB
TypeScript

import { IconPlus, IconTrash } from "@tabler/icons-react";
import type { ContactChannel } from "@/lib/cinemas";
import { resolveContact } from "@/lib/contact-link";
/**
* แก้ช่องทางติดต่อของการ์ด "สอบถามข้อมูลและจองตั๋ว" ต่อโรง
* แต่ละแถว = label / ค่าที่แสดง / ลิงก์ (ถ้าไม่มีลิงก์ หน้าเว็บแสดงเป็นข้อความธรรมดา)
*/
const SUGGESTED_LABELS = [
"Website",
"Facebook",
"Instagram",
"Line",
"Call",
"อีเมล",
"จองตั๋ว",
];
const smallButtonClass =
"flex min-h-9 items-center gap-1 rounded-lg border border-[#303030] px-2 text-xs text-[#999] transition hover:border-[#A92626] hover:text-[#D47272]";
type Props = {
contacts: ContactChannel[];
onChange: (next: ContactChannel[]) => void;
inputClass: string;
labelClass: string;
};
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)),
);
}
return (
<div className="flex flex-col gap-2">
<span className="text-xs text-[#999]">ช่องทางติดต่อ / จองตั๋ว</span>
{contacts.length === 0 ? (
<span className="text-[11px] text-[#777]">
ยังไม่มีช่องทางติดต่อ — หน้าเว็บจะขึ้น “ยังไม่ระบุช่องทางติดต่อ”
</span>
) : null}
{contacts.map((contact, index) => (
<div
key={index}
className="grid gap-2 rounded-lg border border-[#242424] p-2 sm:grid-cols-[130px_minmax(0,1fr)_minmax(0,1fr)_auto]"
>
<label className={labelClass}>
ป้ายชื่อ
<input
className={inputClass}
value={contact.label}
placeholder="Website"
list="contact-label-options"
onChange={(event) =>
updateAt(index, { label: event.target.value })
}
/>
</label>
<label className={labelClass}>
ข้อความที่แสดง
<input
className={inputClass}
value={contact.value}
placeholder="www.housesamyan.com"
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,
})
}
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">
<button
type="button"
className={smallButtonClass}
aria-label={`ลบช่องทางที่ ${index + 1}`}
onClick={() => onChange(contacts.filter((_, i) => i !== index))}
>
<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>
))}
<datalist id="contact-label-options">
{SUGGESTED_LABELS.map((label) => (
<option key={label} value={label} />
))}
</datalist>
<button
type="button"
className={`${smallButtonClass} w-fit`}
onClick={() =>
onChange([...contacts, { label: "", value: "", href: null }])
}
>
<IconPlus size={14} />
เพิ่มช่องทางติดต่อ
</button>
</div>
);
}