The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { T, useGT } from "gt-react"; |
| 2 | import { CheckIcon, CopyIcon } from "lucide-react"; |
| 3 | import { useRef, type ComponentProps } from "react"; |
| 4 | |
| 5 | import { useCopyToClipboard } from "../../hooks/useCopyToClipboard"; |
| 6 | import { |
| 7 | serializeTableElementToCsv, |
| 8 | serializeTableElementToMarkdown, |
| 9 | } from "../../markdown-clipboard"; |
| 10 | import { Button } from "../ui/button"; |
| 11 | import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 12 | import { ScrollArea } from "../ui/scroll-area"; |
| 13 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 14 | import { reportMarkdownActionFailure } from "./markdownActionFailure"; |
| 15 | |
| 16 | export function MarkdownTable({ children, ...props }: ComponentProps<"table">) { |
| 17 | const gt = useGT(); |
| 18 | const containerRef = useRef<HTMLDivElement | null>(null); |
| 19 | const { copyToClipboard, isCopied: copied } = useCopyToClipboard<"markdown" | "csv">({ |
| 20 | timeout: 1200, |
| 21 | onError: (cause, format) => |
| 22 | reportMarkdownActionFailure({ operation: "copy-table", format }, cause), |
| 23 | }); |
| 24 | const copyLabel = copied ? gt("Copied") : gt("Copy table"); |
| 25 | |
| 26 | const handleCopy = (format: "markdown" | "csv") => { |
| 27 | const table = containerRef.current?.querySelector("table"); |
| 28 | if (!table) return; |
| 29 | copyToClipboard( |
| 30 | format === "markdown" |
| 31 | ? serializeTableElementToMarkdown(table) |
| 32 | : serializeTableElementToCsv(table), |
| 33 | format, |
| 34 | ); |
| 35 | }; |
| 36 | |
| 37 | return ( |
| 38 | <div ref={containerRef} className="chat-markdown-table-container"> |
| 39 | <ScrollArea radius="none" chainVerticalScroll scrollFade className="w-full max-w-full"> |
| 40 | <table {...props}>{children}</table> |
| 41 | </ScrollArea> |
| 42 | {/* Stays within the row's px-1: the chat lane clips sideways overflow. */} |
| 43 | <div className="mt-0.5 -mx-1 flex items-center justify-end select-none"> |
| 44 | <Menu> |
| 45 | <Tooltip> |
| 46 | <TooltipTrigger |
| 47 | render={ |
| 48 | <MenuTrigger |
| 49 | render={ |
| 50 | <Button |
| 51 | type="button" |
| 52 | variant="ghost-muted" |
| 53 | size="icon-xs" |
| 54 | aria-label={copyLabel} |
| 55 | /> |
| 56 | } |
| 57 | /> |
| 58 | } |
| 59 | > |
| 60 | {copied ? <CheckIcon className="size-3" /> : <CopyIcon className="size-3" />} |
| 61 | </TooltipTrigger> |
| 62 | <TooltipPopup side="top">{copyLabel}</TooltipPopup> |
| 63 | </Tooltip> |
| 64 | <MenuPopup align="end"> |
| 65 | <MenuItem onClick={() => handleCopy("markdown")}> |
| 66 | <T>Copy as Markdown</T> |
| 67 | </MenuItem> |
| 68 | <MenuItem onClick={() => handleCopy("csv")}> |
| 69 | <T>Copy as CSV</T> |
| 70 | </MenuItem> |
| 71 | </MenuPopup> |
| 72 | </Menu> |
| 73 | </div> |
| 74 | </div> |
| 75 | ); |
| 76 | } |