Skip to content

Commit fdc1c22

Browse files
committed
frontend: type keystores
1 parent d1e0c87 commit fdc1c22

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
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';
@@ -40,18 +40,14 @@ interface SidebarProps {
4040
accounts: IAccount[];
4141
}
4242

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

54-
type Props = SubscribedProps & SharedPanelProps & SidebarProps & TranslateProps;
50+
type Props = SharedPanelProps & SidebarProps & TranslateProps;
5551

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

@@ -92,16 +88,30 @@ const GetAccountLink = ({ coinCode, code, name, handleSidebarItemClick }: TGetAc
9288
);
9389
};
9490

91+
type State = {
92+
keystores: TKeystores;
93+
}
9594

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

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

103110
public componentWillUnmount() {
104111
this.removeTouchEvents();
112+
if (this.unsubscribeFn) {
113+
this.unsubscribeFn();
114+
}
105115
}
106116

107117
private registerTouchEvents = () => {
@@ -158,11 +168,11 @@ class Sidebar extends Component<Props> {
158168
};
159169

160170
public render() {
171+
const { keystores } = this.state;
161172
const {
162173
t,
163174
deviceIDs,
164175
accounts,
165-
keystores,
166176
activeSidebar,
167177
sidebarStatus,
168178
} = this.props;
@@ -255,12 +265,6 @@ function eject(e: React.SyntheticEvent): void {
255265
e.preventDefault();
256266
}
257267

258-
const subscribeHOC = subscribe<SubscribedProps, SharedPanelProps & SidebarProps & TranslateProps>(
259-
{ keystores: 'keystores' },
260-
false,
261-
false,
262-
)(Sidebar);
263-
264-
const guideShareHOC = share<SharedPanelProps, SidebarProps & TranslateProps>(panelStore)(subscribeHOC);
268+
const guideShareHOC = share<SharedPanelProps, SidebarProps & TranslateProps>(panelStore)(Sidebar);
265269
const translateHOC = translate()(guideShareHOC);
266270
export { translateHOC as Sidebar };

0 commit comments

Comments
 (0)