Open History to see the last commit
1import { T, useGT } from "gt-react";
2import { CheckIcon, CopyIcon } from "lucide-react";
3import { useRef, type ComponentProps } from "react";
4
5import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
6import {
7 serializeTableElementForChat,
8 serializeTableElementToCsv,
9 serializeTableElementToHtml,
10 serializeTableElementToMarkdown,
11} from "../../markdown-clipboard";
12import { Button } from "../ui/button";
13import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
14import { ScrollArea } from "../ui/scroll-area";
15import { reportMarkdownActionFailure, type TableCopyFormat } from "./markdownActionFailure";
16import { SharedTooltipTrigger } from "../ui/shared-tooltip";
17
18type TableTextFormat = Exclude<TableCopyFormat, "png" | "chat">;
19
20const TABLE_COPY_SERIALIZERS: Record<TableTextFormat, (table: Element) => string> = {
21 markdown: serializeTableElementToMarkdown,
22 csv: serializeTableElementToCsv,
23 html: serializeTableElementToHtml,
24};
25
26// Loaded on first use; the clipboard write still starts inside the click.
27const renderTablePng = (table: HTMLTableElement) =>
28 import("./tablePng").then((module) => module.renderTablePng(table));
29
30export function MarkdownTable({ children, ...props }: ComponentProps<"table">) {
31 const gt = useGT();
32 const containerRef = useRef<HTMLDivElement | null>(null);
33 const {
34 copyToClipboard,
35 copyPngToClipboard,
36 isCopied: copied,
37 } = useCopyToClipboard<TableCopyFormat>({
38 timeout: 1200,
39 onError: (cause, format) =>
40 reportMarkdownActionFailure({ operation: "copy-table", format }, cause),
41 });
42 const copyLabel = copied ? gt`Copied` : gt`Copy table`;
43 // Insecure origins (plain http over a LAN or tailnet) have no async clipboard for images.
44 const canCopyPng =
45 typeof ClipboardItem !== "undefined" && typeof navigator.clipboard?.write === "function";
46
47 const handleCopy = (format: TableTextFormat) => {
48 const table = containerRef.current?.querySelector("table");
49 if (!table) return;
50 const text = TABLE_COPY_SERIALIZERS[format](table);
51 // Plain-text targets get the markup; rich targets (mail, docs, wikis) paste a real table.
52 copyToClipboard(
53 text,
54 format,
55 format === "html" ? { "text/html": `<meta charset="utf-8">${text}` } : undefined,
56 { requireHtml: format === "html" },
57 );
58 };
59
60 const handleCopyForChat = () => {
61 const table = containerRef.current?.querySelector("table");
62 if (!table) return;
63 const { text, html } = serializeTableElementForChat(table);
64 copyToClipboard(text, "chat", { "text/html": html });
65 };
66
67 const handleCopyPng = () => {
68 const table = containerRef.current?.querySelector("table");
69 if (!table) return;
70 copyPngToClipboard(() => renderTablePng(table), "png");
71 };
72
73 return (
74 <div ref={containerRef} className="chat-markdown-table-container">
75 <ScrollArea radius="none" chainVerticalScroll scrollFade className="w-full max-w-full">
76 <table {...props}>{children}</table>
77 </ScrollArea>
78 {/* Stays within the row's px-1: the chat lane clips sideways overflow. */}
79 <div className="mt-0.5 -mx-1 flex items-center justify-end select-none">
80 <Menu>
81 <SharedTooltipTrigger
82 render={
83 <MenuTrigger
84 render={
85 <Button
86 type="button"
87 variant="ghost-muted"
88 size="icon-xs"
89 aria-label={copyLabel}
90 />
91 }
92 />
93 }
94 label={copyLabel}
95 >
96 {copied ? <CheckIcon className="size-3" /> : <CopyIcon className="size-3" />}
97 </SharedTooltipTrigger>
98 <MenuPopup align="end">
99 <MenuItem onClick={() => handleCopy("markdown")}>
100 <T>Copy as Markdown</T>
101 </MenuItem>
102 <MenuItem onClick={() => handleCopy("csv")}>
103 <T>Copy as CSV</T>
104 </MenuItem>
105 <MenuItem onClick={() => handleCopy("html")}>
106 <T>Copy as HTML</T>
107 </MenuItem>
108 <MenuItem onClick={handleCopyForChat}>
109 <T>Copy for chat apps</T>
110 </MenuItem>
111 {canCopyPng ? (
112 <MenuItem onClick={handleCopyPng}>
113 <T>Copy as PNG</T>
114 </MenuItem>
115 ) : null}
116 </MenuPopup>
117 </Menu>
118 </div>
119 </div>
120 );
121}