Skip to content

Commit cb978ea

Browse files
committed
frontend: convert wait-dialog into functional component
1 parent 15f9fe3 commit cb978ea

File tree

1 file changed

+60
-81
lines changed

1 file changed

+60
-81
lines changed
Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Copyright 2018 Shift Devices AG
3-
* Copyright 2021-2024 Shift Crypto AG
3+
* Copyright 2021-2025 Shift Crypto AG
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -15,45 +15,27 @@
1515
* limitations under the License.
1616
*/
1717

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';
2020
import { UseDisableBackButton } from '@/hooks/backbutton';
2121
import style from '@/components/dialog/dialog.module.css';
2222

23-
24-
interface WaitDialogProps {
23+
type Props = {
2524
includeDefault?: boolean;
2625
title?: string;
27-
children?: ReactNode;
26+
children?: React.ReactNode;
2827
}
2928

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);
5537

56-
private handleKeyDown = (e: KeyboardEvent) => {
38+
const handleKeyDown = (e: KeyboardEvent) => {
5739
const activeElement = document.activeElement;
5840
if (activeElement && activeElement instanceof HTMLElement) {
5941
activeElement.blur();
@@ -62,59 +44,56 @@ class WaitDialog extends Component<Props, State> {
6244
e.stopPropagation();
6345
};
6446

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);
7353
};
7454

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();
8758

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>
11183
</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 }
11294
</div>
11395
</div>
11496
</div>
115-
);
116-
}
117-
}
118-
119-
const TranslatedWaitDialog = translate()(WaitDialog);
120-
export { TranslatedWaitDialog as WaitDialog };
97+
</div>
98+
);
99+
};

0 commit comments

Comments
 (0)