Skip to content

Commit 5bda68d

Browse files
committed
Merge branch 'frontend-type-keystores-rebased-watchonly' into staging-watchonly
2 parents 484feb8 + 75482c7 commit 5bda68d

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

frontends/web/src/api/keystores.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2023 Shift Crypto AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { subscribeEndpoint, TUnsubscribe } from './subscribe';
18+
import { apiGet } from '../utils/request';
19+
20+
export type { TUnsubscribe };
21+
22+
type TKeystore = { type: 'hardware' | 'software' };
23+
export type TKeystores = TKeystore[];
24+
25+
export const subscribeKeystores = (
26+
cb: (keystores: TKeystores) => void
27+
) => {
28+
return subscribeEndpoint('keystores', cb);
29+
};
30+
31+
export const getKeystores = (): Promise<TKeystores> => {
32+
return apiGet('keystores');
33+
};

frontends/web/src/components/sidebar/sidebar.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
import React, { Component } from 'react';
1919
import { Link, NavLink } from 'react-router-dom';
20+
import { TKeystores, subscribeKeystores, TUnsubscribe, getKeystores } from '../../api/keystores';
2021
import { IAccount } from '../../api/account';
2122
import coins from '../../assets/icons/coins.svg';
2223
import ejectIcon from '../../assets/icons/eject.svg';
2324
import info from '../../assets/icons/info.svg';
2425
import settings from '../../assets/icons/settings-alt.svg';
2526
import settingsGrey from '../../assets/icons/settings-alt_disabled.svg';
2627
import { share } from '../../decorators/share';
27-
import { subscribe } from '../../decorators/subscribe';
2828
import { translate, TranslateProps } from '../../decorators/translate';
2929
import { debug } from '../../utils/env';
3030
import { apiPost } from '../../utils/request';
@@ -41,18 +41,14 @@ interface SidebarProps {
4141
accounts: IAccount[];
4242
}
4343

44-
interface SubscribedProps {
45-
keystores?: Array<{ type: 'hardware' | 'software' }>;
46-
}
47-
4844
export type SharedPanelProps = {
4945
// eslint-disable-next-line react/no-unused-prop-types
5046
activeSidebar: boolean;
5147
// eslint-disable-next-line react/no-unused-prop-types
5248
sidebarStatus: string;
5349
}
5450

55-
type Props = SubscribedProps & SharedPanelProps & SidebarProps & TranslateProps;
51+
type Props = SharedPanelProps & SidebarProps & TranslateProps;
5652

5753
type TGetAccountLinkProps = IAccount & { handleSidebarItemClick: ((e: React.SyntheticEvent) => void) };
5854

@@ -93,16 +89,30 @@ const GetAccountLink = ({ coinCode, code, name, handleSidebarItemClick }: TGetAc
9389
);
9490
};
9591

92+
type State = {
93+
keystores: TKeystores;
94+
}
9695

9796
class Sidebar extends Component<Props> {
9897
private swipe!: SwipeAttributes;
98+
private unsubscribeFn?: TUnsubscribe;
99+
100+
public readonly state: State = {
101+
keystores: [],
102+
};
99103

100104
public componentDidMount() {
101105
this.registerTouchEvents();
106+
getKeystores().then(keystores => this.setState({ keystores }, () => {
107+
this.unsubscribeFn = subscribeKeystores(keystores => this.setState({ keystores }));
108+
}));
102109
}
103110

104111
public componentWillUnmount() {
105112
this.removeTouchEvents();
113+
if (this.unsubscribeFn) {
114+
this.unsubscribeFn();
115+
}
106116
}
107117

108118
private registerTouchEvents = () => {
@@ -159,11 +169,11 @@ class Sidebar extends Component<Props> {
159169
};
160170

161171
public render() {
172+
const { keystores } = this.state;
162173
const {
163174
t,
164175
deviceIDs,
165176
accounts,
166-
keystores,
167177
activeSidebar,
168178
sidebarStatus,
169179
} = this.props;
@@ -265,12 +275,6 @@ function eject(e: React.SyntheticEvent): void {
265275
e.preventDefault();
266276
}
267277

268-
const subscribeHOC = subscribe<SubscribedProps, SharedPanelProps & SidebarProps & TranslateProps>(
269-
{ keystores: 'keystores' },
270-
false,
271-
false,
272-
)(Sidebar);
273-
274-
const guideShareHOC = share<SharedPanelProps, SidebarProps & TranslateProps>(panelStore)(subscribeHOC);
278+
const guideShareHOC = share<SharedPanelProps, SidebarProps & TranslateProps>(panelStore)(Sidebar);
275279
const translateHOC = translate()(guideShareHOC);
276280
export { translateHOC as Sidebar };

frontends/web/src/routes/account/info/buyReceiveCTA.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Balances } from '../summary/accountssummary';
2222
import { isBitcoinCoin } from '../utils';
2323
import { getExchangeSupportedAccounts } from '../../buy/utils';
2424
import styles from './buyReceiveCTA.module.css';
25+
import { useMountedRef } from '../../../hooks/mount';
2526

2627
type TBuyReceiveCTAProps = {
2728
balanceList?: IBalance[];
@@ -62,12 +63,20 @@ export const BuyReceiveCTA = ({ code, unit, balanceList, exchangeBuySupported =
6263

6364

6465
export const AddBuyReceiveOnEmptyBalances = ({ balances, accounts }: TAddBuyReceiveOnEmpyBalancesProps) => {
65-
66+
const mounted = useMountedRef();
6667
const [supportedAccounts, setSupportedAccounts] = useState<IAccount[]>();
6768

6869
useEffect(() => {
69-
getExchangeSupportedAccounts(accounts).then(setSupportedAccounts);
70-
}, [accounts]);
70+
if (mounted.current) {
71+
getExchangeSupportedAccounts(accounts)
72+
.then(supportedAccounts => {
73+
if (mounted.current) {
74+
setSupportedAccounts(supportedAccounts);
75+
}
76+
})
77+
.catch(console.error);
78+
}
79+
}, [accounts, mounted]);
7180

7281
if (balances === undefined || supportedAccounts === undefined) {
7382
return null;

0 commit comments

Comments
 (0)