Only reset export scope on genuine open transition via ref

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
vedanta.gawande 2026-06-27 01:16:42 +00:00
parent 5ca25b1eb9
commit c5ed1246a8

View File

@ -1,7 +1,7 @@
"use client";
import { DownloadSimpleIcon } from "@phosphor-icons/react";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { DesignButton } from "../button";
import { DesignDialog } from "../dialog";
@ -69,14 +69,19 @@ export function DataGridExportDialog<TRow>({
// Reset the scope to its default each time the dialog opens. The dialog stays
// mounted between opens, so without this the scope would retain whatever the
// user last picked instead of honoring `defaultScope` on every open. We key
// off `open` so this only fires on an open transition.
// user last picked instead of honoring `defaultScope` on every open. We track
// the previous `open` value with a ref so the reset only fires on a genuine
// closed->open transition -- not on every render that flips other state (e.g.
// `isExporting` going false after a failed/empty export would otherwise wipe
// the user's current selection while the dialog is still open).
const defaultScope = exportOptions?.defaultScope ?? "all";
const wasOpenRef = useRef(false);
useEffect(() => {
if (open && !isExporting) {
if (open && !wasOpenRef.current) {
setScope(defaultScope);
}
}, [open, isExporting, defaultScope]);
wasOpenRef.current = open;
}, [open, defaultScope]);
const entityName = exportOptions?.entityName ?? "row";
const entityNamePlural = exportOptions?.entityNamePlural ?? "rows";