@@ -703,6 +703,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
703703 const [ expanded , setExpanded ] = useState ( readInitialWordWrapSetting ) ;
704704 const [ copied , setCopied ] = useState ( false ) ;
705705 const copiedTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
706+ const copyAttemptRef = useRef ( 0 ) ;
706707 const expandLabel = expanded ? "Collapse table cells" : "Expand table cells" ;
707708 const copyLabel = copied ? "Copied" : "Copy table" ;
708709
@@ -739,13 +740,15 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
739740 format === "markdown"
740741 ? serializeTableElementToMarkdown ( table )
741742 : serializeTableElementToCsv ( table ) ;
743+ const attempt = ++ copyAttemptRef . current ;
742744 // A retry must not keep showing the previous attempt's Copied state.
743745 if ( copiedTimerRef . current != null ) {
744746 clearTimeout ( copiedTimerRef . current ) ;
745747 copiedTimerRef . current = null ;
746748 }
747749 setCopied ( false ) ;
748750 const reportFailure = ( cause : unknown ) => {
751+ if ( attempt !== copyAttemptRef . current ) return ;
749752 reportMarkdownActionFailure ( { operation : "copy-table" , format } , cause ) ;
750753 toastManager . add (
751754 stackedThreadToast ( {
@@ -756,6 +759,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
756759 ) ;
757760 } ;
758761 void writeTextToClipboard ( text , "table" ) . then ( ( didCopy ) => {
762+ if ( attempt !== copyAttemptRef . current ) return ;
759763 if ( ! didCopy ) {
760764 reportFailure ( new Error ( "Clipboard write produced no result." ) ) ;
761765 return ;
@@ -765,6 +769,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
765769 }
766770 setCopied ( true ) ;
767771 copiedTimerRef . current = setTimeout ( ( ) => {
772+ if ( attempt !== copyAttemptRef . current ) return ;
768773 setCopied ( false ) ;
769774 copiedTimerRef . current = null ;
770775 } , 1200 ) ;
@@ -773,6 +778,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
773778
774779 useEffect (
775780 ( ) => ( ) => {
781+ copyAttemptRef . current += 1 ;
776782 if ( copiedTimerRef . current != null ) {
777783 clearTimeout ( copiedTimerRef . current ) ;
778784 copiedTimerRef . current = null ;
@@ -945,17 +951,20 @@ function MarkdownCodeBlock({
945951 const [ copied , setCopied ] = useState ( false ) ;
946952 const [ wrapped , setWrapped ] = useState ( readInitialWordWrapSetting ) ;
947953 const copiedTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
954+ const copyAttemptRef = useRef ( 0 ) ;
948955 const wrapLabel = wrapped ? "Disable line wrap" : "Wrap lines" ;
949956 const copyLabel = copied ? "Copied" : "Copy code" ;
950957
951958 const handleCopy = useCallback ( ( ) => {
959+ const attempt = ++ copyAttemptRef . current ;
952960 // A retry must not keep showing the previous attempt's Copied state.
953961 if ( copiedTimerRef . current != null ) {
954962 clearTimeout ( copiedTimerRef . current ) ;
955963 copiedTimerRef . current = null ;
956964 }
957965 setCopied ( false ) ;
958966 const reportFailure = ( cause : unknown ) => {
967+ if ( attempt !== copyAttemptRef . current ) return ;
959968 reportMarkdownActionFailure (
960969 {
961970 operation : "copy-code-block" ,
@@ -973,6 +982,7 @@ function MarkdownCodeBlock({
973982 ) ;
974983 } ;
975984 void writeTextToClipboard ( code , "code" ) . then ( ( didCopy ) => {
985+ if ( attempt !== copyAttemptRef . current ) return ;
976986 if ( ! didCopy ) {
977987 reportFailure ( new Error ( "Clipboard write produced no result." ) ) ;
978988 return ;
@@ -982,6 +992,7 @@ function MarkdownCodeBlock({
982992 }
983993 setCopied ( true ) ;
984994 copiedTimerRef . current = setTimeout ( ( ) => {
995+ if ( attempt !== copyAttemptRef . current ) return ;
985996 setCopied ( false ) ;
986997 copiedTimerRef . current = null ;
987998 } , 1200 ) ;
@@ -990,6 +1001,7 @@ function MarkdownCodeBlock({
9901001
9911002 useEffect (
9921003 ( ) => ( ) => {
1004+ copyAttemptRef . current += 1 ;
9931005 if ( copiedTimerRef . current != null ) {
9941006 clearTimeout ( copiedTimerRef . current ) ;
9951007 copiedTimerRef . current = null ;
0 commit comments