- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป) - แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md - PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px - แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion - ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction - SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article) - ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์ - กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630 - เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide - tests: 135 ผ่าน (16 ไฟล์)
130 lines
4.7 KiB
TypeScript
130 lines
4.7 KiB
TypeScript
import { IconCheck, IconShare } from "@tabler/icons-react";
|
|
import {
|
|
useEffect,
|
|
useRef,
|
|
useState,
|
|
type MouseEvent as ReactMouseEvent,
|
|
} from "react";
|
|
|
|
import { useI18n } from "@/lib/i18n";
|
|
import {
|
|
buildShareUrl,
|
|
buildShortSharePath,
|
|
resolveShareOrigin,
|
|
shareOrCopy,
|
|
type ShareResult,
|
|
} from "@/lib/share";
|
|
import { useShareOrigin } from "@/lib/share-origin";
|
|
|
|
type ShareButtonProps = {
|
|
/** slug ของโรงหนัง — ใช้สร้างลิงก์ย่อ `/c/<slug>` */
|
|
slug: string;
|
|
/** ชื่อที่ส่งให้แผงแชร์ของระบบ */
|
|
title: string;
|
|
/** `icon` = ปุ่มกลมบนการ์ด · `wide` = ปุ่มมีข้อความ (ใช้ท้ายหน้ารายละเอียด) */
|
|
variant?: "icon" | "wide";
|
|
};
|
|
|
|
/**
|
|
* ปุ่มแชร์โรงหนัง — ลิงก์ย่อ `/c/<slug>` บนโดเมนที่ตั้งใน env `SHARE_ORIGIN`
|
|
* - มี Web Share (มือถือ) → เปิดแผงแชร์ของระบบ และคัดลอกลิงก์สำรองให้ด้วย
|
|
* - ไม่มี → คัดลอกลิงก์ลงคลิปบอร์ด แล้วบอกผลผ่าน aria-live
|
|
*/
|
|
export function ShareButton({
|
|
slug,
|
|
title,
|
|
variant = "icon",
|
|
}: ShareButtonProps) {
|
|
const { messages } = useI18n();
|
|
const configuredOrigin = useShareOrigin();
|
|
const [result, setResult] = useState<ShareResult | null>(null);
|
|
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
// ล้างตัวจับเวลาเมื่อ unmount กัน state อัปเดตหลัง component ถูกถอด
|
|
useEffect(() => {
|
|
return () => {
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
};
|
|
}, []);
|
|
|
|
async function handleClick(event: ReactMouseEvent<HTMLButtonElement>) {
|
|
// กันการ์ดที่ห่ออยู่ถูกคลิกตาม (การ์ดมีลิงก์ "ดูรายละเอียด" ข้างใน)
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
|
|
const url = buildShareUrl(
|
|
resolveShareOrigin(configuredOrigin, window.location.origin),
|
|
buildShortSharePath(slug),
|
|
);
|
|
const next = await shareOrCopy({ title, url });
|
|
setResult(next);
|
|
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
timerRef.current = setTimeout(() => setResult(null), 2500);
|
|
}
|
|
|
|
const isCopy = result === "copied" || result === "shared";
|
|
const label =
|
|
result === "failed"
|
|
? messages.share.failed
|
|
: isCopy
|
|
? messages.share.copied
|
|
: variant === "wide"
|
|
? messages.share.pageLabel
|
|
: messages.share.label;
|
|
|
|
const icon = isCopy ? <IconCheck size={16} /> : <IconShare size={16} />;
|
|
|
|
if (variant === "wide") {
|
|
return (
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={handleClick}
|
|
title={title}
|
|
className={`flex min-h-11 items-center gap-2 rounded-lg border px-4 text-sm backdrop-blur transition focus-visible:outline focus-visible:outline-2 focus-visible:outline-[#A92626] ${
|
|
result === "failed"
|
|
? "border-[#A92626]/60 bg-[#1B1B1B]/80 text-[#D47272]"
|
|
: isCopy
|
|
? "border-[#D47272]/50 bg-[#1B1B1B]/80 text-[#D47272]"
|
|
: "border-[#332B2B] bg-[#1B1B1B]/80 text-white hover:border-[#A92626]/60 hover:bg-[#242424]"
|
|
}`}
|
|
>
|
|
{icon}
|
|
{label}
|
|
</button>
|
|
<span
|
|
role="status"
|
|
aria-live="polite"
|
|
className="text-xs text-[#B3B3B3]"
|
|
>
|
|
{result ? label : ""}
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="relative">
|
|
<button
|
|
type="button"
|
|
onClick={handleClick}
|
|
aria-label={`${messages.share.label}: ${title}`}
|
|
title={messages.share.label}
|
|
className={`relative flex size-9 items-center justify-center rounded-xl border backdrop-blur-xl transition after:absolute after:-inset-1.5 after:content-[''] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#A92626] active:scale-95 ${
|
|
result === "failed"
|
|
? "border-[#A92626]/60 bg-[#1B1B1B]/80 text-[#D47272]"
|
|
: isCopy
|
|
? "border-[#D47272]/50 bg-[#1B1B1B]/80 text-[#D47272]"
|
|
: "border-white/10 bg-[#1B1B1B]/80 text-white hover:border-[#A92626]/60 hover:bg-[#242424] hover:text-[#D47272]"
|
|
}`}
|
|
>
|
|
{icon}
|
|
</button>
|
|
<span role="status" aria-live="polite" className="sr-only">
|
|
{result ? label : ""}
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|