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:
1 parent
2c5918a614
commit
42572020d8
9 files changed
+656
-26
No files matched your search
@@ -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} />
|
||||
เพิ่มช่องทางติดต่อ
|
||||
|
||||
@@ -1,27 +1,102 @@
|
||||
import { IconCircleCheck, IconExternalLink } from "@tabler/icons-react";
|
||||
import {
|
||||
IconBrandFacebook,
|
||||
IconBrandInstagram,
|
||||
IconBrandLine,
|
||||
IconCircleCheck,
|
||||
IconExternalLink,
|
||||
IconMail,
|
||||
IconPhone,
|
||||
IconTicket,
|
||||
IconWorld,
|
||||
} from "@tabler/icons-react";
|
||||
|
||||
import type { Cinema, ContactChannel } from "@/lib/cinemas";
|
||||
import { resolveContact, type ContactKind } from "@/lib/contact-link";
|
||||
import { COLORS } from "@/lib/design-tokens";
|
||||
import { useI18n } from "@/lib/i18n";
|
||||
import { translate } from "@/lib/i18n/messages";
|
||||
import { COLORS } from "@/lib/design-tokens";
|
||||
|
||||
function isExternal(href: string | null): boolean {
|
||||
return href !== null && href.startsWith("http");
|
||||
/** ไอคอนตามชนิดช่องทาง (เดาจาก label ก่อน แล้วค่อยดูจาก kind/URL) */
|
||||
function ChannelIcon({
|
||||
label,
|
||||
kind,
|
||||
href,
|
||||
}: {
|
||||
label: string;
|
||||
kind: ContactKind | null;
|
||||
href: string | null;
|
||||
}) {
|
||||
const text = label.toLowerCase();
|
||||
const url = (href ?? "").toLowerCase();
|
||||
|
||||
if (kind === "tel") return <IconPhone size={16} aria-hidden="true" />;
|
||||
if (kind === "mailto") return <IconMail size={16} aria-hidden="true" />;
|
||||
if (url.includes("facebook") || text.includes("facebook")) {
|
||||
return <IconBrandFacebook size={16} aria-hidden="true" />;
|
||||
}
|
||||
if (url.includes("instagram") || text.includes("instagram")) {
|
||||
return <IconBrandInstagram size={16} aria-hidden="true" />;
|
||||
}
|
||||
if (url.includes("line.me") || text.includes("line")) {
|
||||
return <IconBrandLine size={16} aria-hidden="true" />;
|
||||
}
|
||||
if (
|
||||
text.includes("จอง") ||
|
||||
text.includes("book") ||
|
||||
text.includes("ticket")
|
||||
) {
|
||||
return <IconTicket size={16} aria-hidden="true" />;
|
||||
}
|
||||
return <IconWorld size={16} aria-hidden="true" />;
|
||||
}
|
||||
|
||||
function ContactRow({ channel }: { channel: ContactChannel }) {
|
||||
const resolved = resolveContact(channel);
|
||||
const value =
|
||||
channel.value.trim() === "" ? (channel.href ?? "") : channel.value;
|
||||
|
||||
const body = (
|
||||
<>
|
||||
<span className="flex min-w-0 items-center gap-2">
|
||||
<span className="text-[#999]">
|
||||
<ChannelIcon
|
||||
label={channel.label}
|
||||
kind={resolved.kind}
|
||||
href={resolved.href}
|
||||
/>
|
||||
</span>
|
||||
<span className="truncate">{value}</span>
|
||||
</span>
|
||||
{resolved.external ? (
|
||||
<IconExternalLink
|
||||
size={16}
|
||||
aria-hidden="true"
|
||||
className="shrink-0 text-[#999]"
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
|
||||
const rowClass =
|
||||
"flex min-h-11 items-center justify-between gap-3 rounded-lg text-sm transition-colors";
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<span className="text-sm text-[#999]">{channel.label}</span>
|
||||
<a
|
||||
href={channel.href ?? undefined}
|
||||
target={isExternal(channel.href) ? "_blank" : undefined}
|
||||
rel="noreferrer"
|
||||
className="flex items-center justify-between"
|
||||
>
|
||||
<span>{channel.value}</span>
|
||||
<IconExternalLink size={16} />
|
||||
</a>
|
||||
{resolved.href ? (
|
||||
<a
|
||||
href={resolved.href}
|
||||
{...(resolved.external
|
||||
? { target: "_blank", rel: "noopener noreferrer" }
|
||||
: { rel: "nofollow" })}
|
||||
className={`${rowClass} hover:text-[#D47272] focus-visible:outline focus-visible:outline-2 focus-visible:outline-[#A92626]`}
|
||||
>
|
||||
{body}
|
||||
</a>
|
||||
) : (
|
||||
// ไม่มีข้อมูลที่ใช้เป็นลิงก์ได้ — แสดงเป็นข้อความ ไม่หลอกให้กด
|
||||
<span className={rowClass}>{body}</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -37,11 +112,13 @@ export function ContactPanel({ cinema }: { cinema: Cinema }) {
|
||||
>
|
||||
<div className="flex flex-col gap-4">
|
||||
<h2 className="text-2xl font-semibold leading-[1.3]">
|
||||
{(cinema.contact_heading?.trim() || messages.detail.contactTitle)}
|
||||
{cinema.contact_heading?.trim() || messages.detail.contactTitle}
|
||||
</h2>
|
||||
|
||||
{cinema.contacts.length === 0 ? (
|
||||
<p className="text-sm text-[#999]">{messages.detail.contactsFallback}</p>
|
||||
<p className="text-sm text-[#999]">
|
||||
{messages.detail.contactsFallback}
|
||||
</p>
|
||||
) : (
|
||||
cinema.contacts.map((channel) => (
|
||||
<ContactRow key={channel.label} channel={channel} />
|
||||
|
||||
Reference in new issue
Block a user