"use client"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@suphark/ui/components/ui/dialog"; import { ExternalLink } from "lucide-react"; import { type ReactNode, useEffect } from "react"; import { useUiText } from "./ui-text"; /** * หน้าของ Pi Auth (ทีม, สิทธิ์เข้าโครงการ) ใน dialog ผ่าน iframe (ยกจาก Pi PDF) * หน้า embed แสดงปุ่ม "ปิด" ของตัวเองแล้วส่ง `{ type: "pi-auth:close" }` กลับมา — dialog จึงไม่มีปุ่ม X · Esc/กดนอกกรอบปิดได้ * เชื่อเฉพาะข้อความจาก `authOrigin` เท่านั้น · แอปต้องอนุญาต `frame-src` ของ origin นั้นใน CSP * "เปิดในแท็บใหม่" = หน้าเดียวกันแบบเต็มแท็บ สำหรับเบราว์เซอร์ที่ไม่ยอมให้ใส่ใน frame */ export function HostedAuthDialog({ src, openUrl, authOrigin, title, description, onClose, onSelectTenant, }: { /** URL แบบ embed (`?embed=1`) */ src: string; /** หน้าเดียวกันแบบเต็มแท็บ */ openUrl: string; /** origin ของ Pi Auth — ผู้ส่งเดียวที่รับคำสั่ง */ authOrigin: string; title: string; description?: ReactNode; onClose: () => void; /** ตัวเลือกองค์กรของ Pi Auth ส่ง `{ type: "pi-auth:select-tenant", tenant: }` */ onSelectTenant?: (slug: string) => void; }) { const text = useUiText(); useEffect(() => { const onMessage = (event: MessageEvent) => { if (event.origin !== authOrigin) return; const data = event.data as { type?: unknown; tenant?: unknown } | null; if (data?.type === "pi-auth:close") onClose(); if (data?.type === "pi-auth:select-tenant" && typeof data.tenant === "string") { onSelectTenant?.(data.tenant); } }; window.addEventListener("message", onMessage); return () => window.removeEventListener("message", onMessage); }, [authOrigin, onClose, onSelectTenant]); return ( !open && onClose()}> {title} {description && {description}}