63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { memo, useRef } from "react";
|
|
import { Check, Copy } from "lucide";
|
|
import { Button } from "../ui/button";
|
|
import { MorphIcon } from "~/components/MorphIcon";
|
|
import { cn } from "~/lib/utils";
|
|
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
|
|
import {
|
|
ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
|
showAnchoredCopyErrorToast,
|
|
showAnchoredCopySuccessToast,
|
|
} from "../ui/anchoredCopyToast";
|
|
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
|
|
|
export const MessageCopyButton = memo(function MessageCopyButton({
|
|
text,
|
|
extraFlavors,
|
|
size = "xs",
|
|
variant = "outline",
|
|
className,
|
|
}: {
|
|
text: string;
|
|
/** Additional clipboard types written beside `text/plain` when the platform allows it. */
|
|
extraFlavors?: Readonly<Record<string, string>>;
|
|
size?: "xs" | "icon-xs";
|
|
variant?: "outline" | "ghost";
|
|
className?: string;
|
|
}) {
|
|
const ref = useRef<HTMLButtonElement>(null);
|
|
const { copyToClipboard, isCopied } = useCopyToClipboard<void>({
|
|
onCopy: () => showAnchoredCopySuccessToast(ref),
|
|
onError: (error: Error) => showAnchoredCopyErrorToast(ref, error),
|
|
timeout: ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
|
...(extraFlavors ? { extraFlavors } : {}),
|
|
});
|
|
|
|
return (
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
render={
|
|
<Button
|
|
aria-label="Copy message"
|
|
disabled={isCopied}
|
|
onClick={() => copyToClipboard(text)}
|
|
ref={ref}
|
|
type="button"
|
|
size={size}
|
|
variant={variant === "ghost" ? "ghost-muted" : variant}
|
|
className={className}
|
|
/>
|
|
}
|
|
>
|
|
<MorphIcon
|
|
className={cn("size-3", isCopied && "text-primary")}
|
|
icon={isCopied ? Check : Copy}
|
|
/>
|
|
</TooltipTrigger>
|
|
<TooltipPopup>
|
|
<p>Copy message</p>
|
|
</TooltipPopup>
|
|
</Tooltip>
|
|
);
|
|
});
|