fix: clean up timeout in PlanSidebar to prevent memory leaks (#949)

Co-authored-by: hobostay <[email protected]>
This commit is contained in:
Qiaochu Huandhobostay authored and GitHub committed 2026-03-12 12:32:00 -07:00
1 parent b6eba3347d
commit 724f54c2c1
1 file changed
+18 -2
+18 -2
View File
@@ -1,4 +1,4 @@
import { memo, useState, useCallback } from "react";
import { memo, useState, useCallback, useRef, useEffect } from "react";
import { Badge } from "./ui/badge";
import { Button } from "./ui/button";
import { ScrollArea } from "./ui/scroll-area";
@@ -66,6 +66,7 @@ const PlanSidebar = memo(function PlanSidebar({
const [proposedPlanExpanded, setProposedPlanExpanded] = useState(false);
const [isSavingToWorkspace, setIsSavingToWorkspace] = useState(false);
const [copied, setCopied] = useState(false);
const copiedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const planMarkdown = activeProposedPlan?.planMarkdown ?? null;
const displayedPlanMarkdown = planMarkdown ? stripDisplayedPlanMarkdown(planMarkdown) : null;
@@ -74,8 +75,15 @@ const PlanSidebar = memo(function PlanSidebar({
const handleCopyPlan = useCallback(() => {
if (!planMarkdown) return;
void navigator.clipboard.writeText(planMarkdown);
if (copiedTimerRef.current != null) {
clearTimeout(copiedTimerRef.current);
}
setCopied(true);
setTimeout(() => setCopied(false), 2000);
copiedTimerRef.current = setTimeout(() => {
setCopied(false);
copiedTimerRef.current = null;
}, 2000);
}, [planMarkdown]);
const handleDownload = useCallback(() => {
@@ -120,6 +128,14 @@ const PlanSidebar = memo(function PlanSidebar({
{/* Header */}
<div className="flex h-12 shrink-0 items-center justify-between border-b border-border/60 px-3">
<div className="flex items-center gap-2">
// Cleanup timeout on unmount
useEffect(() => {
return () => {
if (copiedTimerRef.current \!= null) {
clearTimeout(copiedTimerRef.current);
}
};
}, []);
<Badge
variant="secondary"
className="rounded-md bg-blue-500/10 px-1.5 py-0 text-[10px] font-semibold tracking-wide text-blue-400 uppercase"