|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import {ReactNode, useEffect, useRef, useState} from 'react'; |
| 3 | +import {ReactNode, useCallback, useEffect, useRef, useState} from 'react'; |
4 | 4 | import {ChevronDownIcon, ChevronRightIcon} from '@radix-ui/react-icons'; |
5 | 5 | import * as Sentry from '@sentry/nextjs'; |
6 | 6 |
|
@@ -66,49 +66,52 @@ export function Expandable({ |
66 | 66 | }; |
67 | 67 | }, [id]); |
68 | 68 |
|
69 | | - async function copyContentOnClick(event: React.MouseEvent<HTMLButtonElement>) { |
70 | | - event.stopPropagation(); // Prevent the details element from toggling |
71 | | - event.preventDefault(); // Prevent default summary click behavior |
| 69 | + const copyContentOnClick = useCallback( |
| 70 | + async (event: React.MouseEvent<HTMLButtonElement>) => { |
| 71 | + event.stopPropagation(); // Prevent the details element from toggling |
| 72 | + event.preventDefault(); // Prevent default summary click behavior |
72 | 73 |
|
73 | | - if (contentRef.current === null) { |
74 | | - return; |
75 | | - } |
| 74 | + if (contentRef.current === null) { |
| 75 | + return; |
| 76 | + } |
76 | 77 |
|
77 | | - // Attempt to get text from markdown code blocks if they exist |
78 | | - const codeBlocks = contentRef.current.querySelectorAll('code'); |
79 | | - let contentToCopy = ''; |
80 | | - |
81 | | - if (codeBlocks.length > 0) { |
82 | | - // If there are code blocks, concatenate their text content |
83 | | - codeBlocks.forEach(block => { |
84 | | - // Exclude code elements within other code elements (e.g. inline code in a block) |
85 | | - if (!block.closest('code')?.parentElement?.closest('code')) { |
86 | | - contentToCopy += (block.textContent || '') + '\n'; |
87 | | - } |
88 | | - }); |
89 | | - contentToCopy = contentToCopy.trim(); |
90 | | - } |
| 78 | + // Attempt to get text from markdown code blocks if they exist |
| 79 | + const codeBlocks = contentRef.current.querySelectorAll('code'); |
| 80 | + let contentToCopy = ''; |
| 81 | + |
| 82 | + if (codeBlocks.length > 0) { |
| 83 | + // If there are code blocks, concatenate their text content |
| 84 | + codeBlocks.forEach(block => { |
| 85 | + // Exclude code elements within other code elements (e.g. inline code in a block) |
| 86 | + if (!block.closest('code')?.parentElement?.closest('code')) { |
| 87 | + contentToCopy += (block.textContent || '') + '\n'; |
| 88 | + } |
| 89 | + }); |
| 90 | + contentToCopy = contentToCopy.trim(); |
| 91 | + } |
91 | 92 |
|
92 | | - // Fallback to the whole content if no code blocks or if they are empty |
93 | | - if (!contentToCopy && contentRef.current.textContent) { |
94 | | - contentToCopy = contentRef.current.textContent.trim(); |
95 | | - } |
| 93 | + // Fallback to the whole content if no code blocks or if they are empty |
| 94 | + if (!contentToCopy && contentRef.current.textContent) { |
| 95 | + contentToCopy = contentRef.current.textContent.trim(); |
| 96 | + } |
96 | 97 |
|
97 | | - if (!contentToCopy) { |
98 | | - // if there is no content to copy (e.g. only images), do nothing. |
99 | | - return; |
100 | | - } |
| 98 | + if (!contentToCopy) { |
| 99 | + // if there is no content to copy (e.g. only images), do nothing. |
| 100 | + return; |
| 101 | + } |
101 | 102 |
|
102 | | - try { |
103 | | - setCopied(false); |
104 | | - await navigator.clipboard.writeText(contentToCopy); |
105 | | - setCopied(true); |
106 | | - setTimeout(() => setCopied(false), 1200); |
107 | | - } catch (error) { |
108 | | - Sentry.captureException(error); |
109 | | - setCopied(false); |
110 | | - } |
111 | | - } |
| 103 | + try { |
| 104 | + setCopied(false); |
| 105 | + await navigator.clipboard.writeText(contentToCopy); |
| 106 | + setCopied(true); |
| 107 | + setTimeout(() => setCopied(false), 1200); |
| 108 | + } catch (error) { |
| 109 | + Sentry.captureException(error); |
| 110 | + setCopied(false); |
| 111 | + } |
| 112 | + }, |
| 113 | + [] |
| 114 | + ); |
112 | 115 |
|
113 | 116 | function toggleIsExpanded(event: React.MouseEvent<HTMLDetailsElement>) { |
114 | 117 | const newVal = event.currentTarget.open; |
|
0 commit comments