korbit-ai[bot] commented on code in PR #33809:
URL: https://github.com/apache/superset/pull/33809#discussion_r2153028818


##########
superset-frontend/src/components/UnsavedChangesModal/index.tsx:
##########
@@ -0,0 +1,134 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import Modal from 'src/components/Modal';
+import Button from 'src/components/Button';
+import { t, styled, css } from '@superset-ui/core';
+import type { FC, ReactElement } from 'react';
+import { Icons } from 'src/components/Icons';
+
+const StyledModalTitle = styled.h1`
+  ${({ theme }) => css`
+    color: ${theme.colors.grayscale.dark1};
+    font-size: ${theme.typography.sizes.l}px;
+    font-weight: ${theme.typography.weights.bold};
+    margin: 0;
+  `}
+`;
+
+const StyledModalBody = styled.p`
+  ${({ theme }) => css`
+    color: ${theme.colors.grayscale.dark1};
+    font-size: ${theme.typography.sizes.m}px;
+    margin: 0;
+    padding: 0 ${theme.gridUnit * 2}px;
+  `}
+`;
+
+const StyledDiscardBtn = styled(Button)`
+  ${({ theme }) => css`
+    min-width: ${theme.gridUnit * 22}px;
+    height: ${theme.gridUnit * 8}px;
+  `}
+`;
+
+const StyledSaveBtn = styled(Button)`
+  ${({ theme }) => css`
+    min-width: ${theme.gridUnit * 17}px;
+    height: ${theme.gridUnit * 8}px;
+    span > :first-of-type {
+      margin-right: 0;
+    }
+  `}
+`;
+
+const StyledWarningIcon = styled(Icons.WarningOutlined)`
+  ${({ theme }) => css`
+    color: ${theme.colors.warning.base};
+    margin-right: ${theme.gridUnit * 2}px;
+  `}
+`;
+
+export type UnsavedChangesModalProps = {
+  showModal: boolean;
+  onHide: () => void;
+  handleSave: () => void;
+  onConfirmNavigation: () => void;
+  title?: string;
+  body?: string;
+};
+
+const UnsavedChangesModal: FC<UnsavedChangesModalProps> = ({
+  showModal,
+  onHide,
+  handleSave,
+  onConfirmNavigation,
+  title = 'Unsaved Changes',
+  body = "If you don't save, changes will be lost.",
+}: UnsavedChangesModalProps): ReactElement => (
+  <Modal
+    centered
+    responsive
+    onHide={onHide}
+    show={showModal}
+    width="444px"

Review Comment:
   ### Hard-coded width value <sub>![category 
Readability](https://img.shields.io/badge/Readability-0284c7)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   Hard-coded pixel value in the Modal component's width prop.
   
   
   ###### Why this matters
   Magic numbers in the code make it harder to maintain consistent spacing and 
harder to update design system changes across the application.
   
   ###### Suggested change ∙ *Feature Preview*
   Move the value to the theme system or constants:
   ```typescript
   width={`${theme.gridUnit * 111}px`} // 444px with default gridUnit of 4
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/4b3febbd-1a2b-494e-9e79-dd3572b8da62/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/4b3febbd-1a2b-494e-9e79-dd3572b8da62?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/4b3febbd-1a2b-494e-9e79-dd3572b8da62?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/4b3febbd-1a2b-494e-9e79-dd3572b8da62?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/4b3febbd-1a2b-494e-9e79-dd3572b8da62)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:97bf71f9-1664-4550-86dd-273818ec061f -->
   
   
   [](97bf71f9-1664-4550-86dd-273818ec061f)



##########
superset-frontend/src/components/UnsavedChangesModal/UnsavedChangesModal.stories.tsx:
##########
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { ReactElement } from 'react';
+import UnsavedChangesModal, { type UnsavedChangesModalProps } from '.';
+
+export default {
+  title: 'UnsavedChangesModal',
+  component: UnsavedChangesModal,
+};
+
+export const InteractiveUnsavedChangesModal = (
+  props: UnsavedChangesModalProps,
+): ReactElement => (
+  <UnsavedChangesModal {...props}>
+    If you don't save, changes will be lost.
+  </UnsavedChangesModal>
+);
+
+InteractiveUnsavedChangesModal.args = {
+  showModal: true,
+  onHide: () => {},
+  handleSave: () => {},
+  onConfirmNavigation: () => {},
+  title: 'Unsaved Changes',
+};
+
+InteractiveUnsavedChangesModal.argTypes = {
+  onHandledPrimaryAction: { action: 'onHandledPrimaryAction' },
+  onHide: { action: 'onHide' },
+};

Review Comment:
   ### Inconsistent callback definitions <sub>![category 
Design](https://img.shields.io/badge/Design-0d9488)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The argTypes definition includes 'onHandledPrimaryAction' which is not 
defined in the args object, creating an inconsistency in the component's 
interface.
   
   
   ###### Why this matters
   This mismatch between args and argTypes could lead to confusion about which 
callbacks are actually available and functional in the modal component.
   
   ###### Suggested change ∙ *Feature Preview*
   Either remove the unused onHandledPrimaryAction or add it to the args object 
if it's needed:
   ```typescript
   InteractiveUnsavedChangesModal.argTypes = {
     onHide: { action: 'onHide' },
     handleSave: { action: 'handleSave' },
     onConfirmNavigation: { action: 'onConfirmNavigation' }
   };
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/df734fd6-0035-4b12-8202-5f5192b7b35d/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/df734fd6-0035-4b12-8202-5f5192b7b35d?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/df734fd6-0035-4b12-8202-5f5192b7b35d?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/df734fd6-0035-4b12-8202-5f5192b7b35d?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/df734fd6-0035-4b12-8202-5f5192b7b35d)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:9efd6a9c-0fa0-4840-a0d4-8f2b4424b481 -->
   
   
   [](9efd6a9c-0fa0-4840-a0d4-8f2b4424b481)



##########
superset-frontend/src/hooks/useUnsavedChangesPrompt/index.ts:
##########
@@ -0,0 +1,119 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { getClientErrorObject, t } from '@superset-ui/core';
+import { useEffect, useRef, useCallback, useState } from 'react';
+import { useHistory } from 'react-router-dom';
+
+type UseUnsavedChangesPromptProps = {
+  hasUnsavedChanges: boolean;
+  onSave: () => Promise<void> | void;
+  isSaveModalVisible?: boolean;
+  manualSaveOnUnsavedChanges?: boolean;
+};
+
+export const useUnsavedChangesPrompt = ({
+  hasUnsavedChanges,
+  onSave,
+  isSaveModalVisible = false,
+  manualSaveOnUnsavedChanges = false,
+}: UseUnsavedChangesPromptProps) => {
+  const history = useHistory();
+  const [showModal, setShowModal] = useState(false);
+
+  const confirmNavigationRef = useRef<(() => void) | null>(null);
+  const unblockRef = useRef<() => void>(() => {});
+  const manualSaveRef = useRef(false); // Track if save was user-initiated 
(not via navigation)
+
+  const handleConfirmNavigation = useCallback(() => {
+    confirmNavigationRef.current?.();
+  }, []);
+
+  const handleSaveAndCloseModal = useCallback(async () => {
+    try {
+      if (manualSaveOnUnsavedChanges) manualSaveRef.current = true;
+
+      await onSave();
+      setShowModal(false);
+    } catch (err) {
+      const clientError = await getClientErrorObject(err);
+      throw new Error(
+        clientError.message ||
+          clientError.error ||
+          t('Sorry, an error occurred'),
+        { cause: err },
+      );
+    }
+  }, [manualSaveOnUnsavedChanges, onSave]);
+
+  const triggerManualSave = useCallback(() => {
+    manualSaveRef.current = true;
+    onSave();
+  }, [onSave]);
+
+  useEffect(() => {
+    if (!hasUnsavedChanges) return undefined;
+
+    const unblock = history.block(({ pathname }: { pathname: string }) => {

Review Comment:
   ### Callback Recreation in History Block <sub>![category 
Performance](https://img.shields.io/badge/Performance-4f46e5)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The history.block callback is recreated on every hasUnsavedChanges or 
history change, potentially causing unnecessary re-renders and memory churn.
   
   
   ###### Why this matters
   Each recreation of the callback function creates a new function instance in 
memory, which could impact performance in larger applications with frequent 
state changes.
   
   ###### Suggested change ∙ *Feature Preview*
   Move the block callback outside the useEffect and memoize it with 
useCallback, keeping the dependencies minimal:
   ```typescript
   const blockCallback = useCallback(({ pathname }: { pathname: string }) => {
     if (manualSaveRef.current) {
       manualSaveRef.current = false;
       return undefined;
     }
     confirmNavigationRef.current = () => {
       unblockRef.current?.();
       history.push(pathname);
     };
     setShowModal(true);
     return false;
   }, [history]);
   
   useEffect(() => {
     if (!hasUnsavedChanges) return undefined;
     const unblock = history.block(blockCallback);
     unblockRef.current = unblock;
     return () => unblock();
   }, [hasUnsavedChanges, blockCallback]);
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/8754fcd5-1f8a-4391-bdf0-8b8a6e587623/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/8754fcd5-1f8a-4391-bdf0-8b8a6e587623?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/8754fcd5-1f8a-4391-bdf0-8b8a6e587623?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/8754fcd5-1f8a-4391-bdf0-8b8a6e587623?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/8754fcd5-1f8a-4391-bdf0-8b8a6e587623)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:1f2cb2d5-10d7-4702-8b2a-48009c88ee18 -->
   
   
   [](1f2cb2d5-10d7-4702-8b2a-48009c88ee18)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to