-
Notifications
You must be signed in to change notification settings - Fork 2k
Add assistant message copy action and harden related test/storage fallbacks #1211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7bfabe4
8d776f7
741eb41
08dd481
92905a4
250be7a
fd955a3
f9a53ab
e8504de
3da865a
0d4beff
6c0a24d
4994126
d19e592
1d34730
a7a902c
ee61e27
a4e170b
31cbcd8
5945b3e
afa41df
40aa530
5ee8129
0b8b360
90a1b7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,82 @@ | ||
| import { memo } from "react"; | ||
| import { memo, useRef } from "react"; | ||
| import { CopyIcon, CheckIcon } from "lucide-react"; | ||
| import { Button } from "../ui/button"; | ||
| import { useCopyToClipboard } from "~/hooks/useCopyToClipboard"; | ||
| import { cn } from "~/lib/utils"; | ||
| import { anchoredToastManager } from "../ui/toast"; | ||
| import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; | ||
|
|
||
| export const MessageCopyButton = memo(function MessageCopyButton({ text }: { text: string }) { | ||
| const { copyToClipboard, isCopied } = useCopyToClipboard(); | ||
| const ANCHORED_TOAST_TIMEOUT_MS = 1000; | ||
| const onCopy = (ref: React.RefObject<HTMLButtonElement | null>) => { | ||
| if (ref.current) { | ||
| anchoredToastManager.add({ | ||
| data: { | ||
| tooltipStyle: true, | ||
| }, | ||
| positionerProps: { | ||
| anchor: ref.current, | ||
| }, | ||
| timeout: ANCHORED_TOAST_TIMEOUT_MS, | ||
| title: "Copied!", | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const onCopyError = (ref: React.RefObject<HTMLButtonElement | null>, error: Error) => { | ||
| if (ref.current) { | ||
| anchoredToastManager.add({ | ||
| data: { | ||
| tooltipStyle: true, | ||
| }, | ||
| positionerProps: { | ||
| anchor: ref.current, | ||
| }, | ||
| timeout: ANCHORED_TOAST_TIMEOUT_MS, | ||
| title: "Failed to copy", | ||
| description: error.message, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export const MessageCopyButton = memo(function MessageCopyButton({ | ||
| text, | ||
| size = "xs", | ||
| variant = "outline", | ||
| className, | ||
| }: { | ||
| text: string; | ||
| size?: "xs" | "icon-xs"; | ||
| variant?: "outline" | "ghost"; | ||
| className?: string; | ||
| }) { | ||
| const ref = useRef<HTMLButtonElement>(null); | ||
| const { copyToClipboard, isCopied } = useCopyToClipboard<void>({ | ||
| onCopy: () => onCopy(ref), | ||
| onError: (error: Error) => onCopyError(ref, error), | ||
| timeout: ANCHORED_TOAST_TIMEOUT_MS, | ||
| }); | ||
|
|
||
| return ( | ||
| <Button | ||
| type="button" | ||
| size="xs" | ||
| variant="outline" | ||
| onClick={() => copyToClipboard(text)} | ||
| title="Copy message" | ||
| > | ||
| {isCopied ? <CheckIcon className="size-3 text-success" /> : <CopyIcon className="size-3" />} | ||
| </Button> | ||
| <Tooltip> | ||
| <TooltipTrigger | ||
| render={ | ||
| <Button | ||
| aria-label="Copy link" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect aria-label says "Copy link" instead of messageMedium Severity The Reviewed by Cursor Bugbot for commit 90a1b7a. Configure here. |
||
| disabled={isCopied} | ||
| onClick={() => copyToClipboard(text)} | ||
| ref={ref} | ||
| type="button" | ||
| size={size} | ||
| variant={variant} | ||
| className={cn(className)} | ||
| /> | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| } | ||
| > | ||
| {isCopied ? <CheckIcon className="size-3 text-success" /> : <CopyIcon className="size-3" />} | ||
| </TooltipTrigger> | ||
| <TooltipPopup> | ||
| <p>Copy to clipboard</p> | ||
| </TooltipPopup> | ||
| </Tooltip> | ||
| ); | ||
| }); | ||


Uh oh!
There was an error while loading. Please reload this page.