@@ -24,12 +24,13 @@ const ExportToCsv = <HeaderItemType extends string>({
2424 downloadRequestId,
2525} : ExportToCsvProps < HeaderItemType > ) => {
2626 const csvRef = useRef ( null )
27- const abortControllerRef = useRef < AbortController > ( new AbortController ( ) )
27+ const abortControllerRef = useRef < AbortController > ( null )
2828
2929 const [ dataToExport , setDataToExport ] = useState < Awaited < ReturnType < typeof apiPromise > > > ( [ ] )
3030 const [ confirmationModalType , setConfirmationModalType ] = useState < 'default' | 'custom' | null > ( null )
3131 const [ isLoading , setIsLoading ] = useState ( false )
3232 const [ dataFetchError , setDataFetchError ] = useState < ServerErrors > ( null )
33+ const [ pendingDownload , setPendingDownload ] = useState ( false )
3334
3435 const handleInitiateDownload = async ( ) => {
3536 if ( disabled ) {
@@ -41,8 +42,7 @@ const ExportToCsv = <HeaderItemType extends string>({
4142 setDataFetchError ( null )
4243 const data = await apiPromise ( { signal : abortControllerRef . current . signal } )
4344 setDataToExport ( data )
44-
45- csvRef . current ?. link ?. click ( )
45+ setPendingDownload ( true )
4646 } catch ( error ) {
4747 if ( ! getIsRequestAborted ( error ) ) {
4848 showError ( error )
@@ -64,12 +64,12 @@ const ExportToCsv = <HeaderItemType extends string>({
6464 }
6565 }
6666
67- useEffect (
68- ( ) => ( ) => {
69- abortControllerRef . current . abort ( )
70- } ,
71- [ ] ,
72- )
67+ useEffect ( ( ) => {
68+ abortControllerRef . current = new AbortController ( )
69+ return ( ) => {
70+ abortControllerRef . current ?. abort ( )
71+ }
72+ } , [ ] )
7373
7474 useEffect ( ( ) => {
7575 if ( ! isNullOrUndefined ( downloadRequestId ) ) {
@@ -78,6 +78,13 @@ const ExportToCsv = <HeaderItemType extends string>({
7878 }
7979 } , [ downloadRequestId ] )
8080
81+ useEffect ( ( ) => {
82+ if ( pendingDownload ) {
83+ csvRef . current ?. link ?. click ( )
84+ setPendingDownload ( false )
85+ }
86+ } , [ pendingDownload ] )
87+
8188 const handleCancelRequest = ( ) => {
8289 abortControllerRef . current . abort ( )
8390 abortControllerRef . current = new AbortController ( )
0 commit comments