@@ -4,6 +4,7 @@ import { useDebounceEffect } from "@src/utils/useDebounceEffect"
44import styled from "styled-components"
55import { vscode } from "@src/utils/vscode"
66import { useAppTranslation } from "@src/i18n/TranslationContext"
7+ import { useCopyToClipboard } from "@src/utils/clipboard"
78import CodeBlock from "./CodeBlock"
89
910const MERMAID_THEME = {
@@ -85,7 +86,7 @@ export default function MermaidBlock({ code }: MermaidBlockProps) {
8586 const [ isLoading , setIsLoading ] = useState ( false )
8687 const [ error , setError ] = useState < string | null > ( null )
8788 const [ isErrorExpanded , setIsErrorExpanded ] = useState ( false )
88- const [ showCopySuccess , setShowCopySuccess ] = useState ( false )
89+ const { showCopyFeedback , copyWithFeedback } = useCopyToClipboard ( )
8990 const { t } = useAppTranslation ( )
9091
9192 // 1) Whenever `code` changes, mark that we need to re-render a new chart
@@ -144,22 +145,7 @@ export default function MermaidBlock({ code }: MermaidBlockProps) {
144145 }
145146 }
146147
147- /**
148- * Copy the mermaid code to clipboard for easier fixing
149- */
150- const handleCopyCode = ( ) => {
151- navigator . clipboard
152- . writeText ( code )
153- . then ( ( ) => {
154- setShowCopySuccess ( true )
155- setTimeout ( ( ) => {
156- setShowCopySuccess ( false )
157- } , 1000 )
158- } )
159- . catch ( ( err ) => {
160- console . error ( "Failed to copy code:" , err )
161- } )
162- }
148+ // Copy functionality handled directly through the copyWithFeedback utility
163149
164150 return (
165151 < MermaidBlockContainer >
@@ -200,11 +186,9 @@ export default function MermaidBlock({ code }: MermaidBlockProps) {
200186 < CopyButton
201187 onClick = { ( e ) => {
202188 e . stopPropagation ( )
203- handleCopyCode ( )
189+ copyWithFeedback ( code , e )
204190 } } >
205- < span
206- className = { `codicon codicon-${ showCopySuccess ? "check" : "copy" } ` }
207- title = { showCopySuccess ? t ( "common:mermaid.copy_success" ) : undefined } > </ span >
191+ < span className = { `codicon codicon-${ showCopyFeedback ? "check" : "copy" } ` } > </ span >
208192 </ CopyButton >
209193 < span className = { `codicon codicon-chevron-${ isErrorExpanded ? "up" : "down" } ` } > </ span >
210194 </ div >
0 commit comments