Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

UI: add confirmation modal before closing the sheet

adilson57f863a8c470
diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/VaultSecretImportModal.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/VaultSecretImportModal.tsx
index 5a3ba2eafc..f3823be0ac 100644
--- a/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/VaultSecretImportModal.tsx
+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/VaultSecretImportModal.tsx
@@ -1,4 +1,4 @@
-import { useMemo, useReducer, useRef, useState } from "react";
+import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from "react";
import { TriangleAlertIcon } from "lucide-react";
import {
@@ -20,6 +20,7 @@ import {
Button,
Checkbox,
Combobox,
+ DiscardChangesAlertDialog,
Field,
FieldContent,
FieldDescription,
@@ -38,6 +39,7 @@ import {
import { useBadgeOverflow } from "@app/components/v3/generic/DataGrid/hooks/use-badge-overflow";
import { TAvailableAppConnection } from "@app/hooks/api/appConnections/types";
import { useGetVaultMounts, useGetVaultSecretPaths } from "@app/hooks/api/migration/queries";
+import { useDiscardChangesGuard } from "@app/hooks/useDiscardChangesGuard";
export type TVaultSecretImportArgs = {
vaultPaths: string[];
@@ -59,6 +61,7 @@ type ContentProps = {
appConnections: TAvailableAppConnection[];
destinationPath: string;
onImport: (args: TVaultSecretImportArgs) => void;
+ onDirtyChange: (isDirty: boolean) => void;
};
const MAX_PATH_LENGTH = 30;
@@ -90,7 +93,13 @@ const renderWildcardPath = (path: string) => {
);
};
-const Content = ({ onClose, appConnections, destinationPath, onImport }: ContentProps) => {
+const Content = ({
+ onClose,
+ appConnections,
+ destinationPath,
+ onImport,
+ onDirtyChange
+}: ContentProps) => {
const hasAppConnections = appConnections.length > 0;
const [keepVaultStructure, setKeepVaultStructure] = useState(false);
const [state, dispatch] = useReducer(
@@ -100,6 +109,19 @@ const Content = ({ onClose, appConnections, destinationPath, onImport }: Content
);
const { connectionId, mountPath, namespace } = state;
const selectedPaths = useMemo(() => state.selection ?? [], [state.selection]);
+ const initialConnectionIdRef = useRef(connectionId);
+ const isDirty =
+ connectionId !== initialConnectionIdRef.current ||
+ Boolean(namespace) ||
+ Boolean(mountPath) ||
+ selectedPaths.length > 0 ||
+ keepVaultStructure;
+
+ useEffect(() => {
+ onDirtyChange(isDirty);
+ return () => onDirtyChange(false);
+ }, [isDirty, onDirtyChange]);
+
const activeConnectionId = hasAppConnections ? (connectionId ?? undefined) : undefined;
const shouldFetchMounts = Boolean(namespace && activeConnectionId);
const shouldFetchPaths = Boolean(namespace && mountPath && activeConnectionId);
@@ -386,29 +408,63 @@ export const VaultSecretImportModal = ({
appConnections,
destinationPath,
onImport
-}: Props) => (
- <Sheet open={isOpen} onOpenChange={onOpenChange}>
- {isOpen && (
- <SheetContent className="sm:max-w-2xl" onOpenAutoFocus={(event) => event.preventDefault()}>
- <SheetHeader>
- <SheetTitle className="flex items-center gap-2">
- <div className="flex size-5 items-center justify-center rounded-full bg-foreground/75">
- <img src="/images/integrations/Vault.png" alt="" className="mt-0.5 size-4" />
- </div>
- Import from HashiCorp Vault
- </SheetTitle>
- <SheetDescription>
- Select a Vault namespace and one or more secret paths to import secrets into the current
- environment and folder.
- </SheetDescription>
- </SheetHeader>
- <Content
- onClose={() => onOpenChange(false)}
- appConnections={appConnections}
- destinationPath={destinationPath}
- onImport={onImport}
- />
- </SheetContent>
- )}
- </Sheet>
-);
+}: Props) => {
+ const [isDirty, setIsDirty] = useState(false);
+
+ const closeSheet = useCallback(() => {
+ setIsDirty(false);
+ onOpenChange(false);
+ }, [onOpenChange]);
+
+ const { confirmDiscard, isDiscardDialogOpen, requestDiscard, setIsDiscardDialogOpen } =
+ useDiscardChangesGuard({ isDirty, onDiscard: closeSheet });
+
+ const handleSheetOpenChange = (open: boolean) => {
+ if (!open) {
+ requestDiscard();
+ return;
+ }
+ onOpenChange(true);
+ };
+
+ return (
+ <>
+ <Sheet open={isOpen} onOpenChange={handleSheetOpenChange}>
+ {isOpen && (
+ <SheetContent
+ className="sm:max-w-2xl"
+ onOpenAutoFocus={(event) => event.preventDefault()}
+ >
+ <SheetHeader>
+ <SheetTitle className="flex items-center gap-2">
+ <div className="flex size-5 items-center justify-center rounded-full bg-foreground/75">
+ <img src="/images/integrations/Vault.png" alt="" className="mt-0.5 size-4" />
+ </div>
+ Import from HashiCorp Vault
+ </SheetTitle>
+ <SheetDescription>
+ Select a Vault namespace and one or more secret paths to import secrets into the
+ current environment and folder.
+ </SheetDescription>
+ </SheetHeader>
+ <Content
+ onClose={closeSheet}
+ appConnections={appConnections}
+ destinationPath={destinationPath}
+ onImport={onImport}
+ onDirtyChange={setIsDirty}
+ />
+ </SheetContent>
+ )}
+ </Sheet>
+
+ <DiscardChangesAlertDialog
+ open={isDiscardDialogOpen}
+ onOpenChange={setIsDiscardDialogOpen}
+ onDiscard={confirmDiscard}
+ title="Discard Vault Import?"
+ description="Your selected Vault namespace and secret paths will be lost."
+ />
+ </>
+ );
+};