"use client"; import { cn } from "@/lib/utils"; import { CheckIcon, ClipboardIcon } from "lucide-react"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { Card } from "./card"; export default function CodeCopyButton({ children, }: { children: React.ReactNode; }) { const [hasCopied, setHasCopied] = useState(false); const isMobile = window.innerWidth <= 640; useEffect(() => { if (hasCopied) { setTimeout(() => { setHasCopied(false); }, 2000); } }, [hasCopied]); const handleCopy = (type: string, value: any) => { navigator.clipboard.writeText(value); setHasCopied(true); let warning = localStorage.getItem("warning"); if (warning === null) { localStorage.setItem("warning", "1"); setTimeout(() => { toast.error( "Be careful when copying scripts from the internet. Always remember check the source!", { duration: 8000 }, ); }, 500); } // toast.success(`copied ${type} to clipboard`, { // icon: , // }); }; return (
{!isMobile && children ? children : "Copy install command"}
handleCopy("install command", children)} > {hasCopied ? ( ) : ( )} Copy
); }