@@ -7,92 +7,82 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
7
7
Please see LICENSE files in the repository root for full details.
8
8
*/
9
9
10
- import React from "react" ;
11
- import { KeyBackupInfo } from "matrix-js-sdk/src/crypto-api" ;
10
+ import React , { JSX , useEffect , useState } from "react" ;
12
11
13
- import { MatrixClientPeg } from "../../../../MatrixClientPeg" ;
14
12
import dis from "../../../../dispatcher/dispatcher" ;
15
13
import { _t } from "../../../../languageHandler" ;
16
14
import Modal from "../../../../Modal" ;
17
15
import RestoreKeyBackupDialog from "../../../../components/views/dialogs/security/RestoreKeyBackupDialog" ;
18
16
import { Action } from "../../../../dispatcher/actions" ;
19
17
import DialogButtons from "../../../../components/views/elements/DialogButtons" ;
20
18
import BaseDialog from "../../../../components/views/dialogs/BaseDialog" ;
19
+ import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext.tsx" ;
21
20
22
- interface IProps {
23
- newVersionInfo : KeyBackupInfo ;
21
+ /**
22
+ * Properties for {@link NewRecoveryMethodDialog}.
23
+ */
24
+ interface NewRecoveryMethodDialogProps {
25
+ /**
26
+ * Callback when the dialog is dismissed.
27
+ */
24
28
onFinished ( ) : void ;
25
29
}
26
30
27
- export default class NewRecoveryMethodDialog extends React . PureComponent < IProps > {
28
- private onOkClick = ( ) : void => {
29
- this . props . onFinished ( ) ;
30
- } ;
31
+ // Export as default instead of a named export so that it can be dynamically imported with `Modal.createDialogAsync`
31
32
32
- private onGoToSettingsClick = ( ) : void => {
33
- this . props . onFinished ( ) ;
34
- dis . fire ( Action . ViewUserSettings ) ;
35
- } ;
33
+ /**
34
+ * Dialog to inform the user that a new recovery method has been detected.
35
+ */
36
+ export default function NewRecoveryMethodDialog ( { onFinished } : NewRecoveryMethodDialogProps ) : JSX . Element {
37
+ const matrixClient = useMatrixClientContext ( ) ;
38
+ const [ isKeyBackupEnabled , setIsKeyBackupEnabled ] = useState ( false ) ;
39
+ useEffect ( ( ) => {
40
+ const checkBackupEnabled = async ( ) : Promise < void > => {
41
+ const crypto = matrixClient . getCrypto ( ) ;
42
+ setIsKeyBackupEnabled ( Boolean ( crypto && ( await crypto . getActiveSessionBackupVersion ( ) ) !== null ) ) ;
43
+ } ;
36
44
37
- private onSetupClick = async ( ) : Promise < void > => {
38
- Modal . createDialog (
39
- RestoreKeyBackupDialog ,
40
- {
41
- onFinished : this . props . onFinished ,
42
- } ,
43
- undefined ,
44
- /* priority = */ false ,
45
- /* static = */ true ,
46
- ) ;
47
- } ;
45
+ checkBackupEnabled ( ) ;
46
+ } , [ matrixClient ] ) ;
48
47
49
- public render ( ) : React . ReactNode {
50
- const title = (
51
- < span className = "mx_KeyBackupFailedDialog_title" >
52
- { _t ( "encryption|new_recovery_method_detected|title" ) }
53
- </ span >
54
- ) ;
55
-
56
- const newMethodDetected = < p > { _t ( "encryption|new_recovery_method_detected|description_1" ) } </ p > ;
57
-
58
- const hackWarning = (
59
- < strong className = "warning" > { _t ( "encryption|new_recovery_method_detected|warning" ) } </ strong >
60
- ) ;
61
-
62
- let content : JSX . Element | undefined ;
63
- if ( MatrixClientPeg . safeGet ( ) . getKeyBackupEnabled ( ) ) {
64
- content = (
65
- < div >
66
- { newMethodDetected }
67
- < p > { _t ( "encryption|new_recovery_method_detected|description_2" ) } </ p >
68
- { hackWarning }
69
- < DialogButtons
70
- primaryButton = { _t ( "action|ok" ) }
71
- onPrimaryButtonClick = { this . onOkClick }
72
- cancelButton = { _t ( "common|go_to_settings" ) }
73
- onCancel = { this . onGoToSettingsClick }
74
- />
75
- </ div >
76
- ) ;
48
+ function onClick ( ) : void {
49
+ if ( isKeyBackupEnabled ) {
50
+ onFinished ( ) ;
77
51
} else {
78
- content = (
79
- < div >
80
- { newMethodDetected }
81
- { hackWarning }
82
- < DialogButtons
83
- primaryButton = { _t ( "common|setup_secure_messages" ) }
84
- onPrimaryButtonClick = { this . onSetupClick }
85
- cancelButton = { _t ( "common|go_to_settings" ) }
86
- onCancel = { this . onGoToSettingsClick }
87
- />
88
- </ div >
52
+ Modal . createDialog (
53
+ RestoreKeyBackupDialog ,
54
+ {
55
+ onFinished,
56
+ } ,
57
+ undefined ,
58
+ false ,
59
+ true ,
89
60
) ;
90
61
}
91
-
92
- return (
93
- < BaseDialog className = "mx_KeyBackupFailedDialog" onFinished = { this . props . onFinished } title = { title } >
94
- { content }
95
- </ BaseDialog >
96
- ) ;
97
62
}
63
+
64
+ return (
65
+ < BaseDialog
66
+ className = "mx_KeyBackupFailedDialog"
67
+ onFinished = { onFinished }
68
+ title = {
69
+ < span className = "mx_KeyBackupFailedDialog_title" >
70
+ { _t ( "encryption|new_recovery_method_detected|title" ) }
71
+ </ span >
72
+ }
73
+ >
74
+ < p > { _t ( "encryption|new_recovery_method_detected|description_1" ) } </ p >
75
+ { isKeyBackupEnabled && < p > { _t ( "encryption|new_recovery_method_detected|description_2" ) } </ p > }
76
+ < strong className = "warning" > { _t ( "encryption|new_recovery_method_detected|warning" ) } </ strong >
77
+ < DialogButtons
78
+ primaryButton = { _t ( "common|setup_secure_messages" ) }
79
+ onPrimaryButtonClick = { onClick }
80
+ cancelButton = { _t ( "common|go_to_settings" ) }
81
+ onCancel = { ( ) => {
82
+ onFinished ( ) ;
83
+ dis . fire ( Action . ViewUserSettings ) ;
84
+ } }
85
+ />
86
+ </ BaseDialog >
87
+ ) ;
98
88
}
0 commit comments