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

MarkdownTable.tsx

86 lines3.1 KB

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 serializeTableElementToCsv,
8 serializeTableElementToHtml,
9 serializeTableElementToMarkdown,
10} from "../../markdown-clipboard";
11import { Button } from "../ui/button";
12import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
13import { ScrollArea } from "../ui/scroll-area";
14import { reportMarkdownActionFailure, type TableCopyFormat } from "./markdownActionFailure";
15import { SharedTooltipTrigger } from "../ui/shared-tooltip";
16​
17const TABLE_COPY_SERIALIZERS: Record<TableCopyFormat, (table: Element) => string> = {
18 markdown: serializeTableElementToMarkdown,
19 csv: serializeTableElementToCsv,
20 html: serializeTableElementToHtml,
21};
22​
23export function MarkdownTable({ children, ...props }: ComponentProps<"table">) {
24 const gt = useGT();
25 const containerRef = useRef<HTMLDivElement | null>(null);
26 const { copyToClipboard, isCopied: copied } = useCopyToClipboard<TableCopyFormat>({
27 timeout: 1200,
28 onError: (cause, format) =>
29 reportMarkdownActionFailure({ operation: "copy-table", format }, cause),
30 });
31 const copyLabel = copied ? gt("Copied") : gt("Copy table");
32​
33 const handleCopy = (format: TableCopyFormat) => {
34 const table = containerRef.current?.querySelector("table");
35 if (!table) return;
36 const text = TABLE_COPY_SERIALIZERS[format](table);
37 // Plain-text targets get the markup; rich targets (mail, docs, wikis) paste a real table.
38 copyToClipboard(
39 text,
40 format,
41 format === "html" ? { "text/html": `<meta charset="utf-8">${text}` } : undefined,
42 { requireHtml: format === "html" },
43 );
44 };
45​
46 return (
47 <div ref={containerRef} className="chat-markdown-table-container">
48 <ScrollArea radius="none" chainVerticalScroll scrollFade className="w-full max-w-full">
49 <table {...props}>{children}</table>
50 </ScrollArea>
51 {/* Stays within the row's px-1: the chat lane clips sideways overflow. */}
52 <div className="mt-0.5 -mx-1 flex items-center justify-end select-none">
53 <Menu>
54 <SharedTooltipTrigger
55 render={
56 <MenuTrigger
57 render={
58 <Button
59 type="button"
60 variant="ghost-muted"
61 size="icon-xs"
62 aria-label={copyLabel}
63 />
64 }
65 />
66 }
67 label={copyLabel}
68 >
69 {copied ? <CheckIcon className="size-3" /> : <CopyIcon className="size-3" />}
70 </SharedTooltipTrigger>
71 <MenuPopup align="end">
72 <MenuItem onClick={() => handleCopy("markdown")}>
73 <T>Copy as Markdown</T>
74 </MenuItem>
75 <MenuItem onClick={() => handleCopy("csv")}>
76 <T>Copy as CSV</T>
77 </MenuItem>
78 <MenuItem onClick={() => handleCopy("html")}>
79 <T>Copy as HTML</T>
80 </MenuItem>
81 </MenuPopup>
82 </Menu>
83 </div>
84 </div>
85 );