Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/components/ui/csvDownloader/csv-export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { JSX, useCallback } from 'react';
import { CsvExportProps } from './csv-export.type';
import { useCsvExport } from './use-csv-export';
import { ExportCsvButton } from './export-csv-button';
import { ManagedExportCsvButton } from './managed-export-csv-button';

export function CsvExport({
columns,
Expand All @@ -19,10 +19,12 @@ export function CsvExport({
skipPinnedBottom = false,
language,
getData,
resetKey,
}: CsvExportProps): JSX.Element {
const csvExport = useCsvExport();
const download = useCallback(() => {
csvExport.getData({

const exportCsv = useCallback(async () => {
await csvExport.getData({
columns,
tableName,
tableNamePrefix,
Expand All @@ -32,6 +34,5 @@ export function CsvExport({
getData,
});
}, [columns, csvExport, tableName, tableNamePrefix, skipColumnHeaders, skipPinnedBottom, language, getData]);

return <ExportCsvButton disabled={disabled} onClick={download} />;
return <ManagedExportCsvButton disabled={disabled} exportCsv={exportCsv} resetKey={resetKey} />;
}
2 changes: 2 additions & 0 deletions src/components/ui/csvDownloader/csv-export.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { ColDef, CsvExportParams } from 'ag-grid-community';
import { Key } from 'react';
import { GsLangUser } from '../../../utils';

export type CsvDownloadProps = {
Expand All @@ -17,6 +18,7 @@ export type CsvDownloadProps = {
language: GsLangUser;
getData: (params?: CsvExportParams) => string | undefined | void;
isCopyCsv?: boolean;
resetKey?: Key;
};

export type CsvExportProps = CsvDownloadProps & {
Expand Down
1 change: 1 addition & 0 deletions src/components/ui/csvDownloader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
export * from './csv-export';
export * from './csv-export.type';
export * from './export-csv-button';
export * from './managed-export-csv-button';
export * from './use-csv-export';
64 changes: 64 additions & 0 deletions src/components/ui/csvDownloader/managed-export-csv-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { useCallback, useEffect, useState } from 'react';
import { ExportCsvButton } from './export-csv-button';

export interface ManagedExportCsvButtonProps {
disabled?: boolean;
exportCsv: () => Promise<void>;
resetKey?: unknown;
onSuccess?: () => void;
onError?: (error: unknown) => void;
}

export function ManagedExportCsvButton({
disabled = false,
exportCsv,
resetKey,
onSuccess,
onError,
}: ManagedExportCsvButtonProps) {

Check warning on line 25 in src/components/ui/csvDownloader/managed-export-csv-button.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Mark the props of the component as read-only.

See more on https://sonarcloud.io/project/issues?id=gridsuite_commons-ui&issues=AZ8jfbiA3-Z9uIIJgq9G&open=AZ8jfbiA3-Z9uIIJgq9G&pullRequest=1222
const [isLoading, setIsLoading] = useState(false);
const [isSuccessful, setIsSuccessful] = useState(false);

useEffect(() => {
setIsLoading(false);
setIsSuccessful(false);
}, [resetKey]);

useEffect(() => {
if (disabled) {
setIsSuccessful(false);
}
}, [disabled]);

const handleClick = useCallback(async () => {
setIsSuccessful(false);
setIsLoading(true);

try {
await exportCsv();
setIsSuccessful(true);
onSuccess?.();
} catch (error) {
setIsSuccessful(false);
onError?.(error);
} finally {
setIsLoading(false);
}
}, [exportCsv, onSuccess, onError]);
Comment on lines +40 to +54

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Guard against state updates after unmount.

handleClick awaits exportCsv() and then calls setIsSuccessful/setIsLoading/onSuccess/onError regardless of whether the component is still mounted. If the consumer unmounts while the export is in flight (e.g., navigating away mid-download), these calls will fire on an unmounted component, triggering React state-update warnings and potentially masking a stale closure over onSuccess/onError.

🔧 Proposed fix using a mounted ref
+    const isMountedRef = useRef(true);
+    useEffect(() => {
+        return () => {
+            isMountedRef.current = false;
+        };
+    }, []);
+
     const handleClick = useCallback(async () => {
         setIsSuccessful(false);
         setIsLoading(true);

         try {
             await exportCsv();
-            setIsSuccessful(true);
-            onSuccess?.();
+            if (isMountedRef.current) {
+                setIsSuccessful(true);
+                onSuccess?.();
+            }
         } catch (error) {
-            setIsSuccessful(false);
-            onError?.(error);
+            if (isMountedRef.current) {
+                setIsSuccessful(false);
+                onError?.(error);
+            }
         } finally {
-            setIsLoading(false);
+            if (isMountedRef.current) {
+                setIsLoading(false);
+            }
         }
     }, [exportCsv, onSuccess, onError]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const handleClick = useCallback(async () => {
setIsSuccessful(false);
setIsLoading(true);
try {
await exportCsv();
setIsSuccessful(true);
onSuccess?.();
} catch (error) {
setIsSuccessful(false);
onError?.(error);
} finally {
setIsLoading(false);
}
}, [exportCsv, onSuccess, onError]);
const isMountedRef = useRef(true);
useEffect(() => {
return () => {
isMountedRef.current = false;
};
}, []);
const handleClick = useCallback(async () => {
setIsSuccessful(false);
setIsLoading(true);
try {
await exportCsv();
if (isMountedRef.current) {
setIsSuccessful(true);
onSuccess?.();
}
} catch (error) {
if (isMountedRef.current) {
setIsSuccessful(false);
onError?.(error);
}
} finally {
if (isMountedRef.current) {
setIsLoading(false);
}
}
}, [exportCsv, onSuccess, onError]);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/ui/csvDownloader/managed-export-csv-button.tsx` around lines
40 - 54, handleClick in ManagedExportCsvButton can update state and invoke
callbacks after the component unmounts while exportCsv is still pending. Add a
mounted ref (set in the component lifecycle) and check it before calling
setIsSuccessful, setIsLoading, onSuccess, or onError, so only mounted instances
are updated. Keep the async flow in handleClick the same, but guard every
post-await branch and the finally cleanup with the mounted check.


return (
<ExportCsvButton
disabled={disabled || isLoading}
onClick={handleClick}
isDownloadLoading={isLoading}
isDownloadSuccessful={isSuccessful}
/>
);
}
Loading