mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-06 20:15:17 +02:00
fix(react): Add cancelAnimationFrame cleanup in useEffect
- Add cleanup function to cancel animation frame on unmount - Prevents potential memory leaks and setState on unmounted component - Closes #1093
This commit is contained in:
parent
c2bd2bc935
commit
805eacb9fc
3 changed files with 18 additions and 5 deletions
|
|
@ -140,6 +140,14 @@ export function ReportPanelContent({
|
||||||
setActiveReportId(reportId);
|
setActiveReportId(reportId);
|
||||||
}, [reportId]);
|
}, [reportId]);
|
||||||
|
|
||||||
|
// Clear copied state after 2 seconds
|
||||||
|
useEffect(() => {
|
||||||
|
if (copied) {
|
||||||
|
const timer = setTimeout(() => setCopied(false), 2000);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}
|
||||||
|
}, [copied]);
|
||||||
|
|
||||||
// Fetch report content (re-runs when activeReportId changes for version switching)
|
// Fetch report content (re-runs when activeReportId changes for version switching)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
|
|
@ -197,7 +205,6 @@ export function ReportPanelContent({
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.writeText(currentMarkdown);
|
await navigator.clipboard.writeText(currentMarkdown);
|
||||||
setCopied(true);
|
setCopied(true);
|
||||||
setTimeout(() => setCopied(false), 2000);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to copy:", err);
|
console.error("Failed to copy:", err);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -306,7 +306,8 @@ const TabsList = forwardRef<
|
||||||
}, [updateActiveIndicator]);
|
}, [updateActiveIndicator]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
requestAnimationFrame(updateActiveIndicator);
|
const frameId = requestAnimationFrame(updateActiveIndicator);
|
||||||
|
return () => cancelAnimationFrame(frameId);
|
||||||
}, [updateActiveIndicator]);
|
}, [updateActiveIndicator]);
|
||||||
|
|
||||||
const scrollTabToCenter = useCallback((index: number) => {
|
const scrollTabToCenter = useCallback((index: number) => {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,14 @@ export function useApiKey(): UseApiKeyReturn {
|
||||||
const [copied, setCopied] = useState(false);
|
const [copied, setCopied] = useState(false);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
|
// Clear copied state after 2 seconds
|
||||||
|
useEffect(() => {
|
||||||
|
if (copied) {
|
||||||
|
const timer = setTimeout(() => setCopied(false), 2000);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}
|
||||||
|
}, [copied]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Load API key from localStorage
|
// Load API key from localStorage
|
||||||
const loadApiKey = () => {
|
const loadApiKey = () => {
|
||||||
|
|
@ -41,9 +49,6 @@ export function useApiKey(): UseApiKeyReturn {
|
||||||
if (success) {
|
if (success) {
|
||||||
setCopied(true);
|
setCopied(true);
|
||||||
toast.success("API key copied to clipboard");
|
toast.success("API key copied to clipboard");
|
||||||
setTimeout(() => {
|
|
||||||
setCopied(false);
|
|
||||||
}, 2000);
|
|
||||||
} else {
|
} else {
|
||||||
toast.error("Failed to copy API key");
|
toast.error("Failed to copy API key");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue