Skip to content

Commit ae04b11

Browse files
committed
Merge remote-tracking branch 'nicolas/refactor-connected-function'
2 parents 1e3dd0b + 617f8c3 commit ae04b11

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

frontends/web/src/connected.tsx

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright 2018 Shift Devices AG
3+
* Copyright 2024 Shift Crypto AG
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
56
* you may not use this file except in compliance with the License.
@@ -14,46 +15,28 @@
1415
* limitations under the License.
1516
*/
1617

17-
import { Component, ReactNode } from 'react';
18+
import { ReactNode, useEffect, useState } from 'react';
1819
import { backendConnected } from './api/subscribe';
1920

20-
interface State {
21-
connected: boolean;
21+
type TProps = {
22+
children: ReactNode;
2223
}
2324

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

39-
public componentWillUnmount() {
40-
this.unsubscribe();
41-
}
28+
useEffect(() => {
29+
return backendConnected(connected => {
30+
setConnected(connected);
31+
});
32+
}, []);
4233

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) {
5335
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>
5539
);
5640
}
57-
}
58-
59-
export { ConnectedApp };
41+
return <div>{children}</div>;
42+
};

0 commit comments

Comments
 (0)