fix: resolve setTimeout memory leak on unmounted component

This commit is contained in:
SohamBhattacharjee2003 2026-04-04 01:28:45 +05:30
parent 414c4c86e9
commit 0cd997f673
2 changed files with 19 additions and 3 deletions

View file

@ -123,6 +123,13 @@ export function ReportPanelContent({
const [copied, setCopied] = useState(false);
const [exporting, setExporting] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
const copyTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
useEffect(() => {
return () => {
if (copyTimerRef.current) clearTimeout(copyTimerRef.current);
};
}, []);
// Editor state — tracks the latest markdown from the Plate editor
const [editedMarkdown, setEditedMarkdown] = useState<string | null>(null);
@ -197,7 +204,8 @@ export function ReportPanelContent({
try {
await navigator.clipboard.writeText(currentMarkdown);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
if (copyTimerRef.current) clearTimeout(copyTimerRef.current);
copyTimerRef.current = setTimeout(() => setCopied(false), 2000);
} catch (err) {
console.error("Failed to copy:", err);
}