"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { copyText, selectContents } from "@suphark/ui/lib/copy-text"; import { CheckIcon, CopyIcon } from "lucide-react"; import { useRef, useState } from "react"; import { useUiText } from "./ui-text"; /** * บล็อก .env สำเร็จรูป: คัดลอกทั้งก้อนปุ่มเดียว (Clerk-style "quick copy") * ค่าที่เป็น secret แสดงจริง (หน้าที่เรียกต้องเป็นหน้า "แสดงครั้งเดียว" อยู่แล้ว) * - ค่ายาว (secret) ตัดบรรทัดในกรอบ ไม่ล้น dialog * - clipboard ถูกบล็อก (webview/embedded browser) → fallback execCommand; ถ้ายังไม่ได้ เลือกข้อความให้กด Ctrl+C */ export function EnvSnippet({ title = ".env.local", lines, copyLabel, }: { title?: string; /** [key, value] ตามลำดับ; value ว่าง = ให้ผู้ใช้เติมเอง */ lines: Array<[key: string, value: string]>; copyLabel?: string; }) { const text = useUiText(); const [state, setState] = useState<"idle" | "copied" | "failed">("idle"); const preRef = useRef(null); const content = lines.map(([k, v]) => `${k}=${v}`).join("\n"); async function copy() { const ok = await copyText(`${content}\n`); if (ok) { setState("copied"); setTimeout(() => setState((s) => (s === "copied" ? "idle" : s)), 1500); } else { selectContents(preRef.current); setState("failed"); } } return (
{title}
        {lines.map(([k, v]) => (
          
{k}= {v}
))}
{state === "failed" ? (

{text.copyFailed ?? "คัดลอกอัตโนมัติไม่ได้ — เลือกข้อความให้แล้ว กด Ctrl+C / ⌘C"}

) : null}
); } /** สุ่ม secret สำหรับ session cookie ของแอป (base64url 48 ไบต์ ≥ 32 ตัวอักษรตามที่ SDK ต้องการ) */ export function randomSecret(bytes = 48): string { const buf = new Uint8Array(bytes); crypto.getRandomValues(buf); let s = ""; for (const b of buf) s += String.fromCharCode(b); return btoa(s).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, ""); }