Skip to content

Commit c1873b0

Browse files
committed
Merge remote-tracking branch 'benma/bb02-component'
2 parents 6878b7b + f49ff00 commit c1873b0

File tree

2 files changed

+16
-48
lines changed

2 files changed

+16
-48
lines changed

frontends/web/src/api/devicessync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export const syncDeviceList = (
3636
*/
3737
export const statusChanged = (
3838
deviceID: string,
39-
cb: (deviceID: string) => void,
39+
cb: () => void,
4040
): TUnsubscribe => {
4141
const unsubscribe = subscribeLegacy('statusChanged', event => {
4242
if (event.type === 'device' && event.deviceID === deviceID) {
43-
cb(deviceID);
43+
cb();
4444
}
4545
});
4646
return unsubscribe;

frontends/web/src/routes/device/bitbox02/bitbox02.tsx

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,27 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { Component } from 'react';
19-
import { getStatus, TStatus } from '../../../api/bitbox02';
18+
import { getStatus } from '../../../api/bitbox02';
2019
import { statusChanged } from '../../../api/devicessync';
21-
import { UnsubscribeList, unsubscribe } from '../../../utils/subscriptions';
20+
import { useSync } from '../../../hooks/api';
2221
import { BB02Settings } from '../../settings/bb02-settings';
2322

24-
type Props = {
23+
type TProps = {
2524
deviceID: string;
2625
deviceIDs: string[];
2726
hasAccounts: boolean;
2827
}
2928

30-
type State = {
31-
status: '' | TStatus;
32-
}
33-
34-
export class BitBox02 extends Component<Props, State> {
35-
constructor(props: Props) {
36-
super(props);
37-
this.state = {
38-
status: '',
39-
};
40-
}
29+
export const BitBox02 = ({ deviceID, deviceIDs, hasAccounts }: TProps) => {
30+
const status = useSync(
31+
() => getStatus(deviceID),
32+
cb => statusChanged(deviceID, () => {
33+
getStatus(deviceID).then(cb);
34+
})
35+
);
4136

42-
private unsubscribeList: UnsubscribeList = [];
43-
44-
public componentDidMount() {
45-
const { deviceID } = this.props;
46-
this.onStatusChanged();
47-
this.unsubscribeList = [
48-
statusChanged(deviceID, this.onStatusChanged),
49-
];
37+
if (status !== 'initialized') {
38+
return null;
5039
}
51-
52-
private onStatusChanged = () => {
53-
getStatus(this.props.deviceID).then(status => {
54-
this.setState({ status });
55-
});
56-
};
57-
58-
public componentWillUnmount() {
59-
unsubscribe(this.unsubscribeList);
60-
}
61-
62-
public render() {
63-
const { deviceID, hasAccounts, deviceIDs } = this.props;
64-
const {
65-
status,
66-
} = this.state;
67-
68-
if (status !== 'initialized') {
69-
return null;
70-
}
71-
return <BB02Settings deviceID={deviceID} deviceIDs={deviceIDs} hasAccounts={hasAccounts} />;
72-
}
73-
}
40+
return <BB02Settings deviceID={deviceID} deviceIDs={deviceIDs} hasAccounts={hasAccounts} />;
41+
};

0 commit comments

Comments
 (0)