|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Image from "next/image"; |
| 4 | +import { useState, useRef, useEffect } from "react"; |
| 5 | +import Hint from "./hint"; |
| 6 | + |
| 7 | +const EMAIL = "support@circuitverse.org"; |
| 8 | +const COPIED_TOOLTIP_DURATION = 1500; |
| 9 | +export default function CopyEmailButton() { |
| 10 | + const [copied, setCopied] = useState(false); |
| 11 | + const timeoutRef = useRef<NodeJS.Timeout | null>(null); |
| 12 | + useEffect(() => { |
| 13 | + return () => { |
| 14 | + if (timeoutRef.current) { |
| 15 | + clearTimeout(timeoutRef.current); |
| 16 | + } |
| 17 | + }; |
| 18 | + }, []); |
| 19 | + const handleCopy = async () => { |
| 20 | + if (!navigator.clipboard) { |
| 21 | + console.error("Clipboard API not available"); |
| 22 | + return; |
| 23 | + } |
| 24 | + try { |
| 25 | + await navigator.clipboard.writeText(EMAIL); |
| 26 | + setCopied(true); |
| 27 | + if (timeoutRef.current) { |
| 28 | + clearTimeout(timeoutRef.current); |
| 29 | + } |
| 30 | + timeoutRef.current = setTimeout(() => { |
| 31 | + setCopied(false); |
| 32 | + timeoutRef.current = null; |
| 33 | + }, COPIED_TOOLTIP_DURATION); |
| 34 | + } catch (error) { |
| 35 | + console.error("Failed to copy email:", error); |
| 36 | + } |
| 37 | + }; |
| 38 | + return ( |
| 39 | + <div className="relative"> |
| 40 | + <Hint label="Email"> |
| 41 | + <button |
| 42 | + type="button" |
| 43 | + onClick={handleCopy} |
| 44 | + aria-label="Email" |
| 45 | + className="text-zinc-400 hover:bg-zinc-100 dark:hover:bg-zinc-800 p-2 rounded-full transition-all duration-200" |
| 46 | + > |
| 47 | + <Image |
| 48 | + src="/gmail.svg" |
| 49 | + alt="Email" |
| 50 | + width={20} |
| 51 | + height={20} |
| 52 | + className="w-5 h-5" |
| 53 | + /> |
| 54 | + </button> |
| 55 | + </Hint> |
| 56 | + |
| 57 | + {copied && ( |
| 58 | + <div className="absolute -top-9 left-1/2 -translate-x-1/2 whitespace-nowrap |
| 59 | + rounded-md bg-zinc-900 px-2 py-1 text-xs text-white |
| 60 | + dark:bg-zinc-100 dark:text-zinc-900 shadow-md"> |
| 61 | + Email Copied! |
| 62 | + </div> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + ); |
| 66 | +} |
0 commit comments