Skip to content

Commit 974a68f

Browse files
committed
useCallback
1 parent cfc5796 commit 974a68f

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

src/components/expandable/index.tsx

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import {ReactNode, useEffect, useRef, useState} from 'react';
3+
import {ReactNode, useCallback, useEffect, useRef, useState} from 'react';
44
import {ChevronDownIcon, ChevronRightIcon} from '@radix-ui/react-icons';
55
import * as Sentry from '@sentry/nextjs';
66

@@ -66,49 +66,52 @@ export function Expandable({
6666
};
6767
}, [id]);
6868

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
7273

73-
if (contentRef.current === null) {
74-
return;
75-
}
74+
if (contentRef.current === null) {
75+
return;
76+
}
7677

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+
}
9192

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+
}
9697

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+
}
101102

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+
);
112115

113116
function toggleIsExpanded(event: React.MouseEvent<HTMLDetailsElement>) {
114117
const newVal = event.currentTarget.open;

0 commit comments

Comments
 (0)