|
1 | 1 | /**
|
2 | 2 | * Copyright 2018 Shift Devices AG
|
| 3 | + * Copyright 2024 Shift Crypto AG |
3 | 4 | *
|
4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 6 | * you may not use this file except in compliance with the License.
|
|
14 | 15 | * limitations under the License.
|
15 | 16 | */
|
16 | 17 |
|
17 |
| -import { Component, ReactNode } from 'react'; |
| 18 | +import { ReactNode, useEffect, useState } from 'react'; |
18 | 19 | import { backendConnected } from './api/subscribe';
|
19 | 20 |
|
20 |
| -interface State { |
21 |
| - connected: boolean; |
| 21 | +type TProps = { |
| 22 | + children: ReactNode; |
22 | 23 | }
|
23 | 24 |
|
24 |
| -interface Props { |
25 |
| - children: ReactNode; |
26 |
| -} |
27 |
| - |
28 |
| -class ConnectedApp extends Component<Props, State> { |
29 |
| - public readonly state: State = { |
30 |
| - connected: true, |
31 |
| - }; |
32 |
| - |
33 |
| - private unsubscribe!: () => void; |
34 |
| - |
35 |
| - public componentDidMount() { |
36 |
| - this.unsubscribe = backendConnected(connected => this.setState({ connected })); |
37 |
| - } |
| 25 | +export const ConnectedApp = ({ children }: TProps) => { |
| 26 | + const [connected, setConnected] = useState(true); |
38 | 27 |
|
39 |
| - public componentWillUnmount() { |
40 |
| - this.unsubscribe(); |
41 |
| - } |
| 28 | + useEffect(() => { |
| 29 | + return backendConnected(connected => { |
| 30 | + setConnected(connected); |
| 31 | + }); |
| 32 | + }, []); |
42 | 33 |
|
43 |
| - public render() { |
44 |
| - const { children } = this.props; |
45 |
| - const { connected } = this.state; |
46 |
| - if (!connected) { |
47 |
| - return ( |
48 |
| - <div className="app" style={{ padding: 40 }}> |
49 |
| - The WebSocket closed. Please restart the backend and reload this page. |
50 |
| - </div> |
51 |
| - ); |
52 |
| - } |
| 34 | + if (!connected) { |
53 | 35 | return (
|
54 |
| - <div>{children}</div> |
| 36 | + <div className="app" style={{ padding: 40 }}> |
| 37 | + The WebSocket closed. Please restart the backend and reload this page. |
| 38 | + </div> |
55 | 39 | );
|
56 | 40 | }
|
57 |
| -} |
58 |
| - |
59 |
| -export { ConnectedApp }; |
| 41 | + return <div>{children}</div>; |
| 42 | +}; |
0 commit comments