Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/ui/src/ui-component/dialog/SaveChatflowDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PropTypes from 'prop-types'
import { Button, Dialog, DialogActions, DialogContent, OutlinedInput, DialogTitle } from '@mui/material'
import { StyledButton } from '@/ui-component/button/StyledButton'

const SaveChatflowDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
const SaveChatflowDialog = ({ show, dialogProps, onCancel, onConfirm, loading = false }) => {
const portalElement = document.getElementById('portal')

const [chatflowName, setChatflowName] = useState('')
Expand All @@ -16,6 +16,8 @@ const SaveChatflowDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
else setIsReadyToSave(false)
}, [chatflowName])

const isSaveDisabled = !isReadyToSave || loading

const component = show ? (
<Dialog
open={show}
Expand All @@ -41,13 +43,13 @@ const SaveChatflowDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
value={chatflowName}
onChange={(e) => setChatflowName(e.target.value)}
onKeyDown={(e) => {
if (isReadyToSave && e.key === 'Enter') onConfirm(e.target.value)
if (!isSaveDisabled && e.key === 'Enter') onConfirm(e.target.value)
}}
/>
</DialogContent>
<DialogActions>
<Button onClick={onCancel}>{dialogProps.cancelButtonName}</Button>
<StyledButton disabled={!isReadyToSave} variant='contained' onClick={() => onConfirm(chatflowName)}>
<Button onClick={onCancel} disabled={loading}>{dialogProps.cancelButtonName}</Button>
<StyledButton disabled={isSaveDisabled} variant='contained' onClick={() => onConfirm(chatflowName)}>
{dialogProps.confirmButtonName}
</StyledButton>
</DialogActions>
Expand All @@ -61,7 +63,8 @@ SaveChatflowDialog.propTypes = {
show: PropTypes.bool,
dialogProps: PropTypes.object,
onCancel: PropTypes.func,
onConfirm: PropTypes.func
onConfirm: PropTypes.func,
loading: PropTypes.bool
}

export default SaveChatflowDialog
19 changes: 16 additions & 3 deletions packages/ui/src/views/canvas/CanvasHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const CanvasHeader = ({ chatflow, isAgentCanvas, isAgentflowV2, handleSaveFlow,
const [flowName, setFlowName] = useState('')
const [isSettingsOpen, setSettingsOpen] = useState(false)
const [flowDialogOpen, setFlowDialogOpen] = useState(false)
const [flowDialogLoading, setFlowDialogLoading] = useState(false)
const [apiDialogOpen, setAPIDialogOpen] = useState(false)
const [apiDialogProps, setAPIDialogProps] = useState({})
const [viewMessagesDialogOpen, setViewMessagesDialogOpen] = useState(false)
Expand Down Expand Up @@ -324,9 +325,18 @@ const CanvasHeader = ({ chatflow, isAgentCanvas, isAgentflowV2, handleSaveFlow,
}

const onConfirmSaveName = (flowName) => {
setFlowDialogOpen(false)
setFlowDialogLoading(true)
setSavePermission(isAgentCanvas ? 'agentflows:update' : 'chatflows:update')
handleSaveFlow(flowName)
const savePromise = handleSaveFlow(flowName)
if (savePromise && typeof savePromise.then === 'function') {
Comment on lines 327 to +331
savePromise.finally(() => {
setFlowDialogOpen(false)
setFlowDialogLoading(false)
Comment on lines +330 to +334
})
} else {
setFlowDialogOpen(false)
setFlowDialogLoading(false)
}
Comment on lines +331 to +339

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If the save operation fails (i.e., the promise rejects), closing the dialog will cause the user to lose the flow name they just typed. It is better to keep the dialog open on failure so the user can correct any issues or try again, and only close it on a successful save.

        if (savePromise && typeof savePromise.then === 'function') {
            savePromise
                .then(() => {
                    setFlowDialogOpen(false)
                })
                .catch(() => {
                    // Keep dialog open on error so user doesn't lose input
                })
                .finally(() => {
                    setFlowDialogLoading(false)
                })
        } else {
            setFlowDialogOpen(false)
            setFlowDialogLoading(false)
        }

}

useEffect(() => {
Expand Down Expand Up @@ -651,7 +661,10 @@ const CanvasHeader = ({ chatflow, isAgentCanvas, isAgentflowV2, handleSaveFlow,
confirmButtonName: 'Save',
cancelButtonName: 'Cancel'
}}
onCancel={() => setFlowDialogOpen(false)}
loading={flowDialogLoading}
onCancel={() => {
if (!flowDialogLoading) setFlowDialogOpen(false)
}}
onConfirm={onConfirmSaveName}
/>
{apiDialogOpen && <APICodeDialog show={apiDialogOpen} dialogProps={apiDialogProps} onCancel={() => setAPIDialogOpen(false)} />}
Expand Down