1
1
/**
2
2
* Copyright 2018 Shift Devices AG
3
- * Copyright 2021-2024 Shift Crypto AG
3
+ * Copyright 2021-2025 Shift Crypto AG
4
4
*
5
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
6
* you may not use this file except in compliance with the License.
15
15
* limitations under the License.
16
16
*/
17
17
18
- import React , { Component , createRef , ReactNode } from 'react' ;
19
- import { translate , TranslateProps } from '@/decorators/translate ' ;
18
+ import React , { useEffect , useRef } from 'react' ;
19
+ import { useTranslation } from 'react-i18next ' ;
20
20
import { UseDisableBackButton } from '@/hooks/backbutton' ;
21
21
import style from '@/components/dialog/dialog.module.css' ;
22
22
23
-
24
- interface WaitDialogProps {
23
+ type Props = {
25
24
includeDefault ?: boolean ;
26
25
title ?: string ;
27
- children ?: ReactNode ;
26
+ children ?: React . ReactNode ;
28
27
}
29
28
30
- type Props = WaitDialogProps & TranslateProps ;
31
-
32
- interface State {
33
- active : boolean ;
34
- }
35
-
36
- class WaitDialog extends Component < Props , State > {
37
- private overlay = createRef < HTMLDivElement > ( ) ;
38
- private modal = createRef < HTMLDivElement > ( ) ;
39
-
40
- public readonly state : State = {
41
- active : false ,
42
- } ;
43
-
44
- public UNSAFE_componentWillMount ( ) {
45
- document . body . addEventListener ( 'keydown' , this . handleKeyDown ) ;
46
- }
47
-
48
- public componentDidMount ( ) {
49
- setTimeout ( this . activate , 10 ) ;
50
- }
51
-
52
- public componentWillUnmount ( ) {
53
- document . body . removeEventListener ( 'keydown' , this . handleKeyDown ) ;
54
- }
29
+ export const WaitDialog = ( {
30
+ includeDefault,
31
+ title,
32
+ children,
33
+ } : Props ) => {
34
+ const { t } = useTranslation ( ) ;
35
+ const overlay = useRef < HTMLDivElement > ( null ) ;
36
+ const modal = useRef < HTMLDivElement > ( null ) ;
55
37
56
- private handleKeyDown = ( e : KeyboardEvent ) => {
38
+ const handleKeyDown = ( e : KeyboardEvent ) => {
57
39
const activeElement = document . activeElement ;
58
40
if ( activeElement && activeElement instanceof HTMLElement ) {
59
41
activeElement . blur ( ) ;
@@ -62,59 +44,56 @@ class WaitDialog extends Component<Props, State> {
62
44
e . stopPropagation ( ) ;
63
45
} ;
64
46
65
- private activate = ( ) => {
66
- this . setState ( { active : true } , ( ) => {
67
- if ( ! this . overlay . current || ! this . modal . current ) {
68
- return ;
69
- }
70
- this . overlay . current . classList . add ( style . activeOverlay ) ;
71
- this . modal . current . classList . add ( style . activeModal ) ;
72
- } ) ;
47
+ const activate = ( ) => {
48
+ if ( ! overlay . current || ! modal . current ) {
49
+ return ;
50
+ }
51
+ overlay . current . classList . add ( style . activeOverlay ) ;
52
+ modal . current . classList . add ( style . activeModal ) ;
73
53
} ;
74
54
75
- public render ( ) {
76
- const {
77
- t,
78
- includeDefault,
79
- title,
80
- children,
81
- } = this . props ;
82
- const defaultContent = (
83
- < div >
84
- < p className = { style . confirmationLabel } > { t ( 'confirm.info' ) } </ p >
85
- </ div >
86
- ) ;
55
+ useEffect ( ( ) => {
56
+ document . body . addEventListener ( 'keydown' , handleKeyDown ) ;
57
+ activate ( ) ;
87
58
88
- const hasChildren = React . Children . toArray ( children ) . length > 0 ;
89
- return (
90
- < div
91
- className = { style . overlay }
92
- ref = { this . overlay }
93
- style = { { zIndex : 10001 } } >
94
- < UseDisableBackButton />
95
- < div className = { [ style . modal , style . open ] . join ( ' ' ) } ref = { this . modal } >
96
- {
97
- title && (
98
- < div className = { style . header } >
99
- < h3 className = { style . title } > { title } </ h3 >
100
- </ div >
101
- )
102
- }
103
- < div className = { style . contentContainer } >
104
- < div className = { style . content } >
105
- { ( hasChildren && includeDefault ) ? defaultContent : null }
106
- { hasChildren ? (
107
- < div className = "flex flex-column flex-start" >
108
- { children }
109
- </ div >
110
- ) : defaultContent }
59
+ return ( ) => {
60
+ document . body . removeEventListener ( 'keydown' , handleKeyDown ) ;
61
+ } ;
62
+ } , [ ] ) ;
63
+
64
+ const defaultContent = (
65
+ < div >
66
+ < p className = { style . confirmationLabel } > { t ( 'confirm.info' ) } </ p >
67
+ </ div >
68
+ ) ;
69
+
70
+ const hasChildren = React . Children . toArray ( children ) . length > 0 ;
71
+
72
+ return (
73
+ < div
74
+ className = { style . overlay }
75
+ ref = { overlay }
76
+ style = { { zIndex : 10001 } } >
77
+ < UseDisableBackButton />
78
+ < div className = { [ style . modal , style . open ] . join ( ' ' ) } ref = { modal } >
79
+ {
80
+ title && (
81
+ < div className = { style . header } >
82
+ < h3 className = { style . title } > { title } </ h3 >
111
83
</ div >
84
+ )
85
+ }
86
+ < div className = { style . contentContainer } >
87
+ < div className = { style . content } >
88
+ { ( hasChildren && includeDefault ) ? defaultContent : null }
89
+ { hasChildren ? (
90
+ < div className = "flex flex-column flex-start" >
91
+ { children }
92
+ </ div >
93
+ ) : defaultContent }
112
94
</ div >
113
95
</ div >
114
96
</ div >
115
- ) ;
116
- }
117
- }
118
-
119
- const TranslatedWaitDialog = translate ( ) ( WaitDialog ) ;
120
- export { TranslatedWaitDialog as WaitDialog } ;
97
+ </ div >
98
+ ) ;
99
+ } ;
0 commit comments