Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MessageCopyButton.tsx

61 lines1.7 KB

1import { useGT } from "gt-react";
2import { memo, useRef } from "react";
3import { CheckIcon, CopyIcon, type LucideIcon } from "lucide-react";
4import { Button } from "../ui/button";
5import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
6import {
7 ANCHORED_COPY_TOAST_TIMEOUT_MS,
8 showAnchoredCopyErrorToast,
9} from "../ui/anchoredCopyToast";
10import { SharedTooltipTrigger } from "../ui/shared-tooltip";
11​
12export const MessageCopyButton = memo(function MessageCopyButton({
13 text,
14 label: labelProp,
15 icon: Icon = CopyIcon,
16 extraFlavors,
17 size = "xs",
18 variant = "outline",
19 className,
20}: {
21 text: string;
22 label?: string;
23 icon?: LucideIcon;
24 extraFlavors?: Readonly<Record<string, string>>;
25 size?: "xs" | "icon-xs";
26 variant?: "outline" | "ghost";
27 className?: string;
28}) {
29 const gt = useGT();
30 const label = labelProp ?? gt("Copy message");
31 const ref = useRef<HTMLButtonElement>(null);
32 const { copyToClipboard, isCopied } = useCopyToClipboard<void>({
33 onError: (error: Error) => showAnchoredCopyErrorToast(ref, error),
34 timeout: ANCHORED_COPY_TOAST_TIMEOUT_MS,
35 ...(extraFlavors ? { extraFlavors } : {}),
36 });
37​
38 return (
39 <SharedTooltipTrigger
40 render={
41 <Button
42 aria-label={label}
43 disabled={isCopied}
44 onClick={() => copyToClipboard(text)}
45 ref={ref}
46 type="button"
47 size={size}
48 variant={variant === "ghost" ? "ghost-muted" : variant}
49 className={className}
50 />
51 }
52 label={
53 <>
54 <p>{label}</p>
55 </>
56 }
57 >
58 {isCopied ? <CheckIcon className="size-3 text-primary" /> : <Icon className="size-3" />}
59 </SharedTooltipTrigger>
60 );
61});