Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: account settings not restoring on "Cancel" #4033

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
- fix update unread badge on when muting / unmuting a chat #4020
- fix update unread badge on receiving device messages #4020
- fix target chat was not opened on notification click #3983
- fix "Password and Account" dialog not indicating invalid credentials, making it seem that you can change password like this #4032
- fix "Password and Account" not restoring original credentials on "Cancel" sometimes #4033

<a id="1_46_1"></a>

Expand Down
9 changes: 3 additions & 6 deletions src/renderer/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ interface ConfigureProgressDialogProps {
credentials?: Partial<Credentials>
onSuccess?: () => void
onUserCancellation?: () => void
onFail?: (error: string) => void
onFail: (error: string) => void
}

export function ConfigureProgressDialog({
Expand Down Expand Up @@ -403,10 +403,7 @@ export function ConfigureProgressDialog({
await BackendRemote.rpc.stopOngoingProcess(window.__selectedAccountId)
} catch (error: any) {
log.error('failed to stopOngoingProcess', error)
onFail &&
onFail(
'failed to stopOngoingProcess' + error.message || error.toString()
)
onFail('failed to stopOngoingProcess' + error.message || error.toString())
// If it fails to cancel but is still successful, it should behave like normal.
wasCanceled.current = false
}
Expand Down Expand Up @@ -446,7 +443,7 @@ export function ConfigureProgressDialog({
} catch (err: any) {
log.error('configure error', err)
onClose()
onFail && onFail(err.message || err.toString())
onFail(err.message || err.toString())
}
})()
},
Expand Down
59 changes: 54 additions & 5 deletions src/renderer/components/dialogs/EditAccountAndPasswordDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import useDialog from '../../hooks/dialog/useDialog'
import useConfirmationDialog from '../../hooks/dialog/useConfirmationDialog'

import type { DialogProps } from '../../contexts/DialogContext'
import AlertDialog from './AlertDialog'

export default function EditAccountAndPasswordDialog({ onClose }: DialogProps) {
const tx = useTranslationFunction()
Expand All @@ -36,14 +37,20 @@ function EditAccountInner(onClose: DialogProps['onClose']) {
const [accountSettings, _setAccountSettings] =
useState<Credentials>(defaultCredentials())

const [disableUpdate, setDisableUpdate] = useState(true)
const [userNeverChangedAccountSettings, setUserNeverChangedAccountSettings] =
useState(true)
const [
userNeverAppliedNewAccountSettings,
setUserNeverAppliedNewAccountSettings,
] = useState(true)

const { openDialog } = useDialog()
const openConfirmationDialog = useConfirmationDialog()
const tx = useTranslationFunction()

const setAccountSettings = (value: Credentials) => {
disableUpdate === true && setDisableUpdate(false)
userNeverChangedAccountSettings === true &&
setUserNeverChangedAccountSettings(false)
_setAccountSettings(value)
}

Expand Down Expand Up @@ -85,13 +92,17 @@ function EditAccountInner(onClose: DialogProps['onClose']) {
}, [])

const onUpdate = useCallback(async () => {
if (disableUpdate) return true
if (userNeverChangedAccountSettings) return true
const onSuccess = () => onClose()

const update = () => {
setUserNeverAppliedNewAccountSettings(false)
openDialog(ConfigureProgressDialog, {
credentials: accountSettings,
onSuccess,
onFail: error => {
openDialog(AlertDialog, { message: error })
},
})
}

Expand All @@ -113,7 +124,7 @@ function EditAccountInner(onClose: DialogProps['onClose']) {
}
}, [
accountSettings,
disableUpdate,
userNeverChangedAccountSettings,
initial_settings.addr,
onClose,
openConfirmationDialog,
Expand All @@ -126,6 +137,44 @@ function EditAccountInner(onClose: DialogProps['onClose']) {
if (update) onClose()
}, [onClose, onUpdate])

const onCacnel = () => {
if (userNeverAppliedNewAccountSettings) {
onClose()
return
}
// This for the case when the user edited credentials, pressed "OK",
// and then we failed to confugure account (see `ConfigureProgressDialog`),
// (which would result in `EditAccountAndPasswordDialog`
// not getting closed).
// In this case, "cancel" should revert back to the credentials that were
// set when the dialog was first opened.
//
// Yes, simply doing
// `await BackendRemote.rpc.batchSetConfig(accountId, initial_settings)`
// is also an option, but let's show the user that the original
// credentials are also not good, if that is the case.
//
// And yes, simply closing Delta Chat without pressing "Cancel",
// or the user closing the dialog wiht "Esc"
// would result this code not gettings invoked, and therefore
// original credentials not getting restored...
openDialog(ConfigureProgressDialog, {
credentials: initial_settings, // Yes, `initial_settings`.
onSuccess: () => {},
onFail: error => {
// This shouldn't happen often, because
// we simply returned to original settings.
// But it could, if, for example, the credentials
// were changed on the server,
// and the user never entered the correct credentials.
openDialog(AlertDialog, { message: error })
},
})
// Close the dialog immediately, no matter the result of the
// `ConfigureProgressDialog`.
onClose()
}

if (accountSettings === null) return null
return (
<>
Expand All @@ -139,7 +188,7 @@ function EditAccountInner(onClose: DialogProps['onClose']) {
)}
</DialogContent>
</DialogBody>
<OkCancelFooterAction onCancel={() => onClose()} onOk={onOk} />
<OkCancelFooterAction onCancel={onCacnel} onOk={onOk} />
</>
)
}
4 changes: 4 additions & 0 deletions src/renderer/hooks/useInstantOnboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
VerifyContactQr,
VerifyGroupQr,
} from '../backend/qr'
import AlertDialog from '../components/dialogs/AlertDialog'

type InstantOnboarding = {
createInstantAccount: (accountId: number) => Promise<T.FullChat['id'] | null>
Expand Down Expand Up @@ -135,6 +136,9 @@ export default function useInstantOnboarding(): InstantOnboarding {
reject(error)
}
},
onFail: error => {
openDialog(AlertDialog, { message: error })
},
})
})
},
Expand Down
Loading