Skip to content

Frontend type keystores #2389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions frontends/web/src/api/keystores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2023 Shift Crypto AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { subscribeEndpoint, TUnsubscribe } from './subscribe';
import { apiGet } from '../utils/request';

export type { TUnsubscribe };

type TKeystore = { type: 'hardware' | 'software' };
export type TKeystores = TKeystore[];

export const subscribeKeystores = (
cb: (keystores: TKeystores) => void
) => {
return subscribeEndpoint('keystores', cb);
};

export const getKeystores = (): Promise<TKeystores> => {
return apiGet('keystores');
};
32 changes: 18 additions & 14 deletions frontends/web/src/components/sidebar/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

import React, { Component } from 'react';
import { Link, NavLink } from 'react-router-dom';
import { TKeystores, subscribeKeystores, TUnsubscribe, getKeystores } from '../../api/keystores';
import { IAccount } from '../../api/account';
import coins from '../../assets/icons/coins.svg';
import ejectIcon from '../../assets/icons/eject.svg';
import info from '../../assets/icons/info.svg';
import settings from '../../assets/icons/settings-alt.svg';
import settingsGrey from '../../assets/icons/settings-alt_disabled.svg';
import { share } from '../../decorators/share';
import { subscribe } from '../../decorators/subscribe';
import { translate, TranslateProps } from '../../decorators/translate';
import { debug } from '../../utils/env';
import { apiPost } from '../../utils/request';
Expand All @@ -41,18 +41,14 @@ interface SidebarProps {
accounts: IAccount[];
}

interface SubscribedProps {
keystores?: Array<{ type: 'hardware' | 'software' }>;
}

export type SharedPanelProps = {
// eslint-disable-next-line react/no-unused-prop-types
activeSidebar: boolean;
// eslint-disable-next-line react/no-unused-prop-types
sidebarStatus: string;
}

type Props = SubscribedProps & SharedPanelProps & SidebarProps & TranslateProps;
type Props = SharedPanelProps & SidebarProps & TranslateProps;

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

Expand Down Expand Up @@ -93,16 +89,30 @@ const GetAccountLink = ({ coinCode, code, name, handleSidebarItemClick }: TGetAc
);
};

type State = {
keystores: TKeystores;
}

class Sidebar extends Component<Props> {
private swipe!: SwipeAttributes;
private unsubscribeFn?: TUnsubscribe;

public readonly state: State = {
keystores: [],
};

public componentDidMount() {
this.registerTouchEvents();
getKeystores().then(keystores => this.setState({ keystores }, () => {
this.unsubscribeFn = subscribeKeystores(keystores => this.setState({ keystores }));
}));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this again, I think it's a bit hard to read (especially the setState callback), I'll change the formatting.

}

public componentWillUnmount() {
this.removeTouchEvents();
if (this.unsubscribeFn) {
this.unsubscribeFn();
}
}

private registerTouchEvents = () => {
Expand Down Expand Up @@ -159,11 +169,11 @@ class Sidebar extends Component<Props> {
};

public render() {
const { keystores } = this.state;
const {
t,
deviceIDs,
accounts,
keystores,
activeSidebar,
sidebarStatus,
} = this.props;
Expand Down Expand Up @@ -265,12 +275,6 @@ function eject(e: React.SyntheticEvent): void {
e.preventDefault();
}

const subscribeHOC = subscribe<SubscribedProps, SharedPanelProps & SidebarProps & TranslateProps>(
{ keystores: 'keystores' },
false,
false,
)(Sidebar);

const guideShareHOC = share<SharedPanelProps, SidebarProps & TranslateProps>(panelStore)(subscribeHOC);
const guideShareHOC = share<SharedPanelProps, SidebarProps & TranslateProps>(panelStore)(Sidebar);
const translateHOC = translate()(guideShareHOC);
export { translateHOC as Sidebar };
15 changes: 12 additions & 3 deletions frontends/web/src/routes/account/info/buyReceiveCTA.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Balances } from '../summary/accountssummary';
import { isBitcoinCoin } from '../utils';
import { getExchangeSupportedAccounts } from '../../buy/utils';
import styles from './buyReceiveCTA.module.css';
import { useMountedRef } from '../../../hooks/mount';

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


export const AddBuyReceiveOnEmptyBalances = ({ balances, accounts }: TAddBuyReceiveOnEmpyBalancesProps) => {

const mounted = useMountedRef();
const [supportedAccounts, setSupportedAccounts] = useState<IAccount[]>();

useEffect(() => {
getExchangeSupportedAccounts(accounts).then(setSupportedAccounts);
}, [accounts]);
if (mounted.current) {
getExchangeSupportedAccounts(accounts)
.then(supportedAccounts => {
if (mounted.current) {
setSupportedAccounts(supportedAccounts);
}
})
.catch(console.error);
}
}, [accounts, mounted]);

if (balances === undefined || supportedAccounts === undefined) {
return null;
Expand Down