From 85711d8adaf8c65fa573990c1ed1e402786c499a Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 23 Aug 2024 18:58:48 -0400 Subject: [PATCH] feat: Add Copy Code button to Artifacts component --- client/src/components/Artifacts/Artifacts.tsx | 25 ++++++----------- client/src/components/Artifacts/Code.tsx | 27 ++++++++++++++++++- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/client/src/components/Artifacts/Artifacts.tsx b/client/src/components/Artifacts/Artifacts.tsx index 92b052c105..83ca2254dc 100644 --- a/client/src/components/Artifacts/Artifacts.tsx +++ b/client/src/components/Artifacts/Artifacts.tsx @@ -12,7 +12,7 @@ import { getFileExtension, getArtifactFilename, } from '~/utils/artifacts'; -import { CodeMarkdown } from './Code'; +import { CodeMarkdown, CopyCodeButton } from './Code'; import store from '~/store'; export function ArtifactPreview({ @@ -167,7 +167,6 @@ export default function Artifacts() { - {/* Footer */}
@@ -198,18 +197,9 @@ export default function Artifacts() {
- - - */} + {/* Publish button */} + {/* + */}
diff --git a/client/src/components/Artifacts/Code.tsx b/client/src/components/Artifacts/Code.tsx index f2234101a2..c07fd5c616 100644 --- a/client/src/components/Artifacts/Code.tsx +++ b/client/src/components/Artifacts/Code.tsx @@ -1,8 +1,12 @@ -import React, { useRef, RefObject, memo } from 'react'; +import React, { useRef, RefObject, memo, useState } from 'react'; import rehypeKatex from 'rehype-katex'; import ReactMarkdown from 'react-markdown'; import rehypeHighlight from 'rehype-highlight'; +import copy from 'copy-to-clipboard'; import { handleDoubleClick, cn, langSubset } from '~/utils'; +import Clipboard from '~/components/svg/Clipboard'; +import CheckMark from '~/components/svg/CheckMark'; +import useLocalize from '~/hooks/useLocalize'; type CodeBarProps = { lang: string; @@ -80,3 +84,24 @@ export const CodeMarkdown = memo( ); }, ); + +export const CopyCodeButton: React.FC<{ content: string }> = ({ content }) => { + const localize = useLocalize(); + const [isCopied, setIsCopied] = useState(false); + + const handleCopy = () => { + copy(content, { format: 'text/plain' }); + setIsCopied(true); + setTimeout(() => setIsCopied(false), 3000); + }; + + return ( + + ); +};