korbit-ai[bot] commented on code in PR #33808:
URL: https://github.com/apache/superset/pull/33808#discussion_r2152999380
##########
superset-frontend/src/components/Modal/Modal.tsx:
##########
@@ -282,13 +282,16 @@ const CustomModal = ({
const [bounds, setBounds] = useState<DraggableBounds>();
const [dragDisabled, setDragDisabled] = useState<boolean>(true);
let FooterComponent;
- if (isValidElement(footer)) {
+
+ // This safely avoids injecting "closeModal" into native elements like <div>
or <span>
+ if (isValidElement(footer) && typeof footer.type === 'function')
Review Comment:
### Incomplete React Component Type Check <sub></sub>
<details>
<summary>Tell me more</summary>
###### What is the issue?
The footer.type check only handles function components but fails to handle
class components, which are also valid React components.
###### Why this matters
If a class component is passed as a footer, the closeModal prop won't be
injected, potentially breaking the unsaved changes confirmation functionality.
###### Suggested change ∙ *Feature Preview*
Modify the condition to handle both function and class components:
```typescript
if (isValidElement(footer) && (typeof footer.type === 'function' ||
(footer.type as any)?.prototype?.isReactComponent))
```
###### Provide feedback to improve future suggestions
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/d2f2c254-32ba-4cfb-8df3-1f10c8585757/upvote)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/d2f2c254-32ba-4cfb-8df3-1f10c8585757?what_not_true=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/d2f2c254-32ba-4cfb-8df3-1f10c8585757?what_out_of_scope=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/d2f2c254-32ba-4cfb-8df3-1f10c8585757?what_not_in_standard=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/d2f2c254-32ba-4cfb-8df3-1f10c8585757)
</details>
<sub>
💬 Looking for more details? Reply to this comment to chat with Korbit.
</sub>
<!--- korbi internal id:9db3095d-4715-4fd4-8098-bcde80d98402 -->
[](9db3095d-4715-4fd4-8098-bcde80d98402)
##########
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 }) => {
+ if (manualSaveRef.current) {
+ manualSaveRef.current = false;
+ return undefined;
+ }
+
+ confirmNavigationRef.current = () => {
+ unblockRef.current?.();
+ history.push(pathname);
+ };
+
+ setShowModal(true);
+ return false;
+ });
+
+ unblockRef.current = unblock;
+ return () => unblock();
+ }, [hasUnsavedChanges, history]);
+
+ useEffect(() => {
+ const handleBeforeUnload = (event: BeforeUnloadEvent) => {
+ if (!hasUnsavedChanges) return;
+ event.preventDefault();
+
+ // Most browsers require a "returnValue" set to empty string
+ const evt = event as any;
Review Comment:
### Undocumented any type assertion <sub></sub>
<details>
<summary>Tell me more</summary>
###### What is the issue?
The type assertion to 'any' lacks documentation explaining why it's
necessary.
###### Why this matters
Type assertions to 'any' are risky and should be documented to prevent
accidental removal or modification.
###### Suggested change ∙ *Feature Preview*
// TypeScript doesn't recognize returnValue on BeforeUnloadEvent
const evt = event as any;
###### Provide feedback to improve future suggestions
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/c1f4c05c-30ce-4c42-8cab-3e6d11b6bae4/upvote)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/c1f4c05c-30ce-4c42-8cab-3e6d11b6bae4?what_not_true=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/c1f4c05c-30ce-4c42-8cab-3e6d11b6bae4?what_out_of_scope=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/c1f4c05c-30ce-4c42-8cab-3e6d11b6bae4?what_not_in_standard=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/c1f4c05c-30ce-4c42-8cab-3e6d11b6bae4)
</details>
<sub>
💬 Looking for more details? Reply to this comment to chat with Korbit.
</sub>
<!--- korbi internal id:7c7d02ed-2b5d-45b2-bb6b-4bef7baafcad -->
[](7c7d02ed-2b5d-45b2-bb6b-4bef7baafcad)
--
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]