|
| 1 | +import React, { useState, useMemo, useCallback } from "react"; |
| 2 | +import { Button, Dropdown, Flex, Spin } from "antd"; |
| 3 | +import styles from "./styles.module.scss"; |
| 4 | +import DownArrow from "@site/static/icons/down.svg"; |
| 5 | +import MarkdownSvg from "@site/static/icons/markdown.svg"; |
| 6 | +import CopySvg from "@site/static/icons/copy.svg"; |
| 7 | +import CopiedSvg from "@site/static/icons/copied.svg"; |
| 8 | +import { useDoc } from "@docusaurus/plugin-content-docs/client"; |
| 9 | +import axios from "axios"; |
| 10 | +import $t from "@site/src/utils/tools"; |
| 11 | +// mark |
| 12 | +// import TurndownService from "turndown"; |
| 13 | +// const turndownService = new TurndownService(); |
| 14 | +// const getPageContentAsHtml = (): string | null => { |
| 15 | +// const contentElement = document.querySelector("article"); |
| 16 | +// return contentElement ? contentElement.innerHTML : null; |
| 17 | +// }; |
| 18 | + |
| 19 | +// const convertHtmlToMarkdown = (html: string): string => { |
| 20 | +// return turndownService.turndown(html); |
| 21 | +// }; |
| 22 | +const CopyDropdownButton: React.FC = () => { |
| 23 | + const [loading, setLoading] = useState(false); |
| 24 | + const [isCopied, setIsCopied] = useState(false); |
| 25 | + const { metadata } = useDoc(); |
| 26 | + const sourceUrl = useMemo(() => { |
| 27 | + return ( |
| 28 | + metadata?.source?.replace( |
| 29 | + "@site", |
| 30 | + "https://raw.githubusercontent.com/databendlabs/databend-docs/refs/heads/main" |
| 31 | + ) || "" |
| 32 | + ); |
| 33 | + }, [metadata]); |
| 34 | + const handleCopy = useCallback((url: string) => { |
| 35 | + if (!url) return; |
| 36 | + setLoading(true); |
| 37 | + axios |
| 38 | + .get(url) |
| 39 | + .then((response) => { |
| 40 | + setIsCopied(true); |
| 41 | + if (response.status === 200) { |
| 42 | + navigator.clipboard.writeText(response.data); |
| 43 | + } else { |
| 44 | + alert($t("Failed to copy markdown")); |
| 45 | + } |
| 46 | + }) |
| 47 | + .finally(() => { |
| 48 | + setLoading(false); |
| 49 | + setTimeout(() => { |
| 50 | + setIsCopied(false); |
| 51 | + }, 3000); |
| 52 | + }); |
| 53 | + }, []); |
| 54 | + const menu = useMemo(() => { |
| 55 | + const items = [ |
| 56 | + { |
| 57 | + key: "copy", |
| 58 | + icon: <CopySvg width={16} />, |
| 59 | + label: $t("Copy Page"), |
| 60 | + description: $t("Copy page as Markdown for LLMs"), |
| 61 | + }, |
| 62 | + { |
| 63 | + key: "markdown", |
| 64 | + icon: <MarkdownSvg width={18} />, |
| 65 | + label: $t("View as Markdown"), |
| 66 | + description: $t("View this page as plain text"), |
| 67 | + }, |
| 68 | + ]; |
| 69 | + |
| 70 | + return { |
| 71 | + items: items.map(({ key, icon, label, description }) => ({ |
| 72 | + key, |
| 73 | + label: ( |
| 74 | + <div> |
| 75 | + <div style={{ fontWeight: 500 }}>{label}</div> |
| 76 | + <div style={{ fontSize: 12, color: "#888" }}>{description}</div> |
| 77 | + </div> |
| 78 | + ), |
| 79 | + icon: <Button icon={icon} />, |
| 80 | + })), |
| 81 | + onClick: ({ key }: { key: string }) => { |
| 82 | + if (key === "copy") handleCopy(sourceUrl); |
| 83 | + if (key === "markdown") window.open(sourceUrl, "_blank"); |
| 84 | + }, |
| 85 | + }; |
| 86 | + }, [sourceUrl, handleCopy]); |
| 87 | + const renderButtonContent = useMemo( |
| 88 | + () => ( |
| 89 | + <Flex align="center" gap={6}> |
| 90 | + {loading ? ( |
| 91 | + <Spin size="small" /> |
| 92 | + ) : isCopied ? ( |
| 93 | + <CopiedSvg width={16} /> |
| 94 | + ) : ( |
| 95 | + <CopySvg width={16} /> |
| 96 | + )} |
| 97 | + <span>{loading ? $t("Copying...") : $t("Copy Page")}</span> |
| 98 | + </Flex> |
| 99 | + ), |
| 100 | + [loading, isCopied] |
| 101 | + ); |
| 102 | + |
| 103 | + return ( |
| 104 | + <Dropdown.Button |
| 105 | + onClick={() => handleCopy(sourceUrl)} |
| 106 | + menu={menu} |
| 107 | + placement="bottomRight" |
| 108 | + icon={<DownArrow width={18} height={"auto"} />} |
| 109 | + className={styles.buttonCainter} |
| 110 | + trigger={["click"]} |
| 111 | + > |
| 112 | + {renderButtonContent} |
| 113 | + </Dropdown.Button> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export default CopyDropdownButton; |
0 commit comments