| 14 | } |
| 15 | |
| 16 | export function CopyableInput({ |
| 17 | value, |
| 18 | className, |
| 19 | successMessage = "Copied to clipboard!", |
| 20 | }: CopyableInputProps) { |
| 21 | const [copied, setCopied] = useState(false); |
| 22 | const { toast } = useToast(); |
| 23 | |
| 24 | const handleCopy = async () => { |
| 25 | try { |
| 26 | await navigator.clipboard.writeText(value); |
| 27 | setCopied(true); |
| 28 | toast({ |
| 29 | title: "Success", |
| 30 | description: successMessage, |
| 31 | }); |
| 32 | |
| 33 | // Reset the copied state after 2 seconds |
| 34 | setTimeout(() => setCopied(false), 2000); |
| 35 | } catch (error) { |
| 36 | toast({ |
| 37 | title: "Error", |
| 38 | description: "Failed to copy to clipboard", |
| 39 | variant: "destructive", |
| 40 | }); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | return ( |
| 45 | <div className={cn("relative flex items-center w-[300px]", className)}> |
| 46 | <Input |
| 47 | value={value} |
| 48 | readOnly |
| 49 | onClick={handleCopy} |
| 50 | className="pr-10 cursor-pointer hover:border-[#FD7302] focus-visible:ring-[#FD7302]" |
| 51 | /> |
| 52 | <Button |
| 53 | type="button" |
| 54 | variant="ghost" |
| 55 | size="sm" |
| 56 | className="absolute right-0 h-full px-3 py-0" |
| 57 | onClick={handleCopy} |
| 58 | > |
| 59 | {copied ? ( |
| 60 | <Check className="h-4 w-4 text-green-500" /> |
| 61 | ) : ( |
| 62 | <Copy className="h-4 w-4" /> |
| 63 | )} |
| 64 | </Button> |
| 65 | </div> |
| 66 | ); |
| 67 | } |