Replace with* with useContext as applicable (#135)

This commit is contained in:
Jaco Greeff 2019-09-01 16:07:52 +02:00 committed by GitHub
parent 459531981c
commit 0d1485db6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 60 additions and 175 deletions

View File

@ -2,22 +2,20 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { OnActionFromCtx } from '../../components/types'; import React, { useContext, useState } from 'react';
import React, { useState } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { ActionBar, Address, Link, withOnAction } from '../../components'; import { ActionBar, ActionContext, Address, Link } from '../../components';
import { editAccount } from '../../messaging'; import { editAccount } from '../../messaging';
import { Name } from '../../partials'; import { Name } from '../../partials';
interface Props { interface Props {
address: string; address: string;
className?: string; className?: string;
onAction: OnActionFromCtx;
} }
function Account ({ address, className, onAction }: Props): React.ReactElement<Props> { function Account ({ address, className }: Props): React.ReactElement<Props> {
const onAction = useContext(ActionContext);
const [isEditing, setEditing] = useState(false); const [isEditing, setEditing] = useState(false);
const [editedName, setName] = useState<string | null>(null); const [editedName, setName] = useState<string | null>(null);
@ -56,11 +54,11 @@ function Account ({ address, className, onAction }: Props): React.ReactElement<P
); );
} }
export default withOnAction(styled(Account)` export default styled(Account)`
.edit-name { .edit-name {
left: 4.75rem; left: 4.75rem;
position: absolute; position: absolute;
right: 0.75rem; right: 0.75rem;
top: -0.5rem; top: -0.5rem;
} }
`); `;

View File

@ -2,18 +2,14 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { AccountsFromCtx } from '../../components/types'; import React, { useContext } from 'react';
import React from 'react'; import { AccountContext, Button, Header, Link, Tip, unicode } from '../../components';
import { Button, Header, Link, Tip, unicode, withAccounts } from '../../components';
import Account from './Account'; import Account from './Account';
interface Props { export default function Accounts (): React.ReactElement<{}> {
accounts: AccountsFromCtx; const accounts = useContext(AccountContext);
}
function Accounts ({ accounts }: Props): React.ReactElement<Props> {
return ( return (
<div> <div>
<Header <Header
@ -41,5 +37,3 @@ function Accounts ({ accounts }: Props): React.ReactElement<Props> {
</div> </div>
); );
} }
export default withAccounts(Accounts);

View File

@ -3,24 +3,23 @@
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { MessageAuthorize } from '@polkadot/extension/background/types'; import { MessageAuthorize } from '@polkadot/extension/background/types';
import { OnActionFromCtx } from '../../components/types';
import React from 'react'; import React, { useContext } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { ActionBar, Button, Icon, IconBox, Link, Tip, defaults, withOnAction } from '../../components'; import { ActionBar, ActionContext, Button, Icon, IconBox, Link, Tip, defaults } from '../../components';
import { approveAuthRequest, rejectAuthRequest } from '../../messaging'; import { approveAuthRequest, rejectAuthRequest } from '../../messaging';
interface Props { interface Props {
authId: string; authId: string;
className?: string; className?: string;
isFirst: boolean; isFirst: boolean;
onAction: OnActionFromCtx;
request: MessageAuthorize; request: MessageAuthorize;
url: string; url: string;
} }
function Request ({ authId, className, isFirst, onAction, request: { origin }, url }: Props): React.ReactElement<Props> { function Request ({ authId, className, isFirst, request: { origin }, url }: Props): React.ReactElement<Props> {
const onAction = useContext(ActionContext);
const onApprove = (): Promise<void> => const onApprove = (): Promise<void> =>
approveAuthRequest(authId) approveAuthRequest(authId)
.then((): void => onAction()) .then((): void => onAction())
@ -59,7 +58,7 @@ function Request ({ authId, className, isFirst, onAction, request: { origin }, u
); );
} }
export default withOnAction(styled(Request)` export default styled(Request)`
.icon { .icon {
background: ${defaults.btnBgDanger}; background: ${defaults.btnBgDanger};
color: ${defaults.btnColorDanger}; color: ${defaults.btnColorDanger};
@ -78,4 +77,4 @@ export default withOnAction(styled(Request)`
text-overflow: ellipsis; text-overflow: ellipsis;
vertical-align: top; vertical-align: top;
} }
`); `;

View File

@ -2,18 +2,14 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { AuthRequestsFromCtx } from '../../components/types'; import React, { useContext } from 'react';
import React from 'react'; import { AuthorizeContext, Header } from '../../components';
import { Header, withAuthRequests } from '../../components';
import Request from './Request'; import Request from './Request';
interface Props { export default function Authorize (): React.ReactElement<{}> {
requests: AuthRequestsFromCtx; const requests = useContext(AuthorizeContext);
}
function Authorize ({ requests }: Props): React.ReactElement<Props> {
return ( return (
<div> <div>
<Header label='authorize' /> <Header label='authorize' />
@ -29,5 +25,3 @@ function Authorize ({ requests }: Props): React.ReactElement<Props> {
</div> </div>
); );
} }
export default withAuthRequests(Authorize);

View File

@ -2,19 +2,16 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { OnActionFromCtx } from '../components/types'; import React, { useContext, useEffect, useState } from 'react';
import React, { useState, useEffect } from 'react'; import { ActionContext, Address, Button, Header, Loading, TextArea } from '../components';
import { Address, Button, Header, Loading, TextArea, withOnAction } from '../components';
import { createAccount, createSeed } from '../messaging'; import { createAccount, createSeed } from '../messaging';
import { Back, Name, Password } from '../partials'; import { Back, Name, Password } from '../partials';
interface Props { type Props = {};
onAction: OnActionFromCtx;
}
function Create ({ onAction }: Props): React.ReactElement<Props> { export default function Create (): React.ReactElement<Props> {
const onAction = useContext(ActionContext);
const [account, setAccount] = useState<null | { address: string; seed: string }>(null); const [account, setAccount] = useState<null | { address: string; seed: string }>(null);
const [name, setName] = useState<string | null>(null); const [name, setName] = useState<string | null>(null);
const [password, setPassword] = useState<string | null>(null); const [password, setPassword] = useState<string | null>(null);
@ -67,5 +64,3 @@ function Create ({ onAction }: Props): React.ReactElement<Props> {
</div> </div>
); );
} }
export default withOnAction(Create);

View File

@ -2,20 +2,17 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { OnActionFromCtx } from '../components/types'; import React, { useContext } from 'react';
import React from 'react';
import { RouteComponentProps, withRouter } from 'react-router'; import { RouteComponentProps, withRouter } from 'react-router';
import { Address, Button, Header, Tip, withOnAction } from '../components'; import { ActionContext, Address, Button, Header, Tip } from '../components';
import { forgetAccount } from '../messaging'; import { forgetAccount } from '../messaging';
import { Back } from '../partials'; import { Back } from '../partials';
interface Props extends RouteComponentProps<{ address: string }> { type Props = RouteComponentProps<{ address: string }>;
onAction: OnActionFromCtx;
}
function Forget ({ match: { params: { address } }, onAction }: Props): React.ReactElement<Props> { function Forget ({ match: { params: { address } } }: Props): React.ReactElement<Props> {
const onAction = useContext(ActionContext);
const onClick = (): Promise<void> => const onClick = (): Promise<void> =>
forgetAccount(address) forgetAccount(address)
.then((): void => onAction('/')) .then((): void => onAction('/'))
@ -37,4 +34,4 @@ function Forget ({ match: { params: { address } }, onAction }: Props): React.Rea
); );
} }
export default withOnAction(withRouter(Forget)); export default withRouter(Forget);

View File

@ -2,19 +2,16 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { OnActionFromCtx } from '../components/types'; import React, { useContext, useState } from 'react';
import React, { useState } from 'react'; import { ActionContext, Address, Button, Header, TextArea } from '../components';
import { Address, Button, Header, TextArea, withOnAction } from '../components';
import { createAccount, validateSeed } from '../messaging'; import { createAccount, validateSeed } from '../messaging';
import { Back, Name, Password } from '../partials'; import { Back, Name, Password } from '../partials';
interface Props { type Props = {};
onAction: OnActionFromCtx;
}
function Import ({ onAction }: Props): React.ReactElement<Props> { export default function Import (): React.ReactElement<Props> {
const onAction = useContext(ActionContext);
const [account, setAccount] = useState<null | { address: string; suri: string }>(null); const [account, setAccount] = useState<null | { address: string; suri: string }>(null);
const [name, setName] = useState<string | null>(null); const [name, setName] = useState<string | null>(null);
const [password, setPassword] = useState<string | null>(null); const [password, setPassword] = useState<string | null>(null);
@ -60,5 +57,3 @@ function Import ({ onAction }: Props): React.ReactElement<Props> {
</div> </div>
); );
} }
export default withOnAction(Import);

View File

@ -3,25 +3,24 @@
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { MessageExtrinsicSign } from '@polkadot/extension/background/types'; import { MessageExtrinsicSign } from '@polkadot/extension/background/types';
import { OnActionFromCtx } from '../../components/types';
import React from 'react'; import React, { useContext } from 'react';
import { createType } from '@polkadot/types'; import { createType } from '@polkadot/types';
import { ActionBar, Address, Link, withOnAction } from '../../components'; import { ActionBar, ActionContext, Address, Link } from '../../components';
import { approveSignRequest, cancelSignRequest } from '../../messaging'; import { approveSignRequest, cancelSignRequest } from '../../messaging';
import Details from './Details'; import Details from './Details';
import Unlock from './Unlock'; import Unlock from './Unlock';
interface Props { interface Props {
isFirst: boolean; isFirst: boolean;
onAction: OnActionFromCtx;
request: MessageExtrinsicSign; request: MessageExtrinsicSign;
signId: string; signId: string;
url: string; url: string;
} }
function Request ({ isFirst, onAction, request, signId, url }: Props): React.ReactElement<Props> { export default function Request ({ isFirst, request, signId, url }: Props): React.ReactElement<Props> {
const onAction = useContext(ActionContext);
const onCancel = (): Promise<void> => const onCancel = (): Promise<void> =>
cancelSignRequest(signId) cancelSignRequest(signId)
.then((): void => onAction()) .then((): void => onAction())
@ -49,5 +48,3 @@ function Request ({ isFirst, onAction, request, signId, url }: Props): React.Rea
</Address> </Address>
); );
} }
export default withOnAction(Request);

View File

@ -2,18 +2,14 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { SignRequestsFromCtx } from '../../components/types'; import React, { useContext } from 'react';
import React from 'react'; import { Header, SigningContext } from '../../components';
import { Header, withSignRequests } from '../../components';
import Request from './Request'; import Request from './Request';
interface Props { export default function Signing (): React.ReactElement<{}> {
requests: SignRequestsFromCtx; const requests = useContext(SigningContext);
}
function Signing ({ requests }: Props): React.ReactElement<Props> {
return ( return (
<div> <div>
<Header label='transactions' /> <Header label='transactions' />
@ -29,5 +25,3 @@ function Signing ({ requests }: Props): React.ReactElement<Props> {
</div> </div>
); );
} }
export default withSignRequests(Signing);

View File

@ -2,17 +2,14 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { OnActionFromCtx } from '../components/types'; import React, { useContext } from 'react';
import React from 'react'; import { ActionContext, Box, Button, Header } from '../components';
import { Box, Button, Header, withOnAction } from '../components'; type Props = {};
interface Props { export default function Welcome (): React.ReactElement<Props> {
onAction: OnActionFromCtx; const onAction = useContext(ActionContext);
}
function Welcome ({ onAction }: Props): React.ReactElement<Props> {
const onClick = (): void => { const onClick = (): void => {
window.localStorage.setItem('welcome_read', 'ok'); window.localStorage.setItem('welcome_read', 'ok');
onAction(); onAction();
@ -37,5 +34,3 @@ function Welcome ({ onAction }: Props): React.ReactElement<Props> {
</div> </div>
); );
} }
export default withOnAction(Welcome);

View File

@ -4,19 +4,17 @@
import { KeyringJson } from '@polkadot/ui-keyring/types'; import { KeyringJson } from '@polkadot/ui-keyring/types';
import { Prefix } from '@polkadot/util-crypto/address/types'; import { Prefix } from '@polkadot/util-crypto/address/types';
import { AccountsFromCtx } from './types';
import React, { useState, useEffect } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import Identicon from '@polkadot/react-identicon'; import Identicon from '@polkadot/react-identicon';
import settings from '@polkadot/ui-settings'; import settings from '@polkadot/ui-settings';
import IconBox from './IconBox'; import IconBox from './IconBox';
import { withAccounts } from './contexts'; import { AccountContext } from './contexts';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
interface Props { interface Props {
accounts: AccountsFromCtx;
address?: string | null; address?: string | null;
children?: React.ReactNode; children?: React.ReactNode;
className?: string; className?: string;
@ -24,7 +22,8 @@ interface Props {
theme?: 'polkadot' | 'substrate'; theme?: 'polkadot' | 'substrate';
} }
function Address ({ accounts, address, children, className, name, theme = 'polkadot' }: Props): React.ReactElement<Props> { function Address ({ address, children, className, name, theme = 'polkadot' }: Props): React.ReactElement<Props> {
const accounts = useContext(AccountContext);
const [account, setAccount] = useState<KeyringJson | null>(null); const [account, setAccount] = useState<KeyringJson | null>(null);
const [formatted, setFormatted] = useState<string | null>(null); const [formatted, setFormatted] = useState<string | null>(null);
@ -69,7 +68,7 @@ function Address ({ accounts, address, children, className, name, theme = 'polka
); );
} }
export default withAccounts(styled(Address)` export default styled(Address)`
.address { .address {
opacity: 0.5; opacity: 0.5;
overflow: hidden; overflow: hidden;
@ -79,4 +78,4 @@ export default withAccounts(styled(Address)`
.name { .name {
padding-bottom: 0.5rem; padding-bottom: 0.5rem;
} }
`); `;

View File

@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { AccountsFromCtx, OnActionFromCtx, AuthRequestsFromCtx, SignRequestsFromCtx, SubtractProps } from './types'; import { AccountsFromCtx, OnActionFromCtx, AuthRequestsFromCtx, SignRequestsFromCtx } from './types';
import React from 'react'; import React from 'react';
@ -22,71 +22,3 @@ export {
AuthorizeContext, AuthorizeContext,
SigningContext SigningContext
}; };
export function withAccounts<P extends { accounts: AccountsFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { accounts: AccountsFromCtx }>> {
// eslint-disable-next-line react/display-name,@typescript-eslint/explicit-function-return-type
return (props: SubtractProps<P, { accounts: AccountsFromCtx }>) => {
return (
<AccountContext.Consumer>
{(accounts): React.ReactNode => (
<Component
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{...props as any}
accounts={accounts}
/>
)}
</AccountContext.Consumer>
);
};
}
export function withOnAction<P extends { onAction: OnActionFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { onAction: OnActionFromCtx }>> {
// eslint-disable-next-line react/display-name,@typescript-eslint/explicit-function-return-type
return (props: SubtractProps<P, { onAction: OnActionFromCtx }>) => {
return (
<ActionContext.Consumer>
{(onAction): React.ReactNode => (
<Component
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{...props as any}
onAction={onAction}
/>
)}
</ActionContext.Consumer>
);
};
}
export function withAuthRequests<P extends { requests: AuthRequestsFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { requests: AuthRequestsFromCtx }>> {
// eslint-disable-next-line react/display-name,@typescript-eslint/explicit-function-return-type
return (props: SubtractProps<P, { requests: AuthRequestsFromCtx }>) => {
return (
<AuthorizeContext.Consumer>
{(requests): React.ReactNode => (
<Component
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{...props as any}
requests={requests}
/>
)}
</AuthorizeContext.Consumer>
);
};
}
export function withSignRequests<P extends { requests: SignRequestsFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { requests: SignRequestsFromCtx }>> {
// eslint-disable-next-line react/display-name,@typescript-eslint/explicit-function-return-type
return (props: SubtractProps<P, { requests: SignRequestsFromCtx }>) => {
return (
<SigningContext.Consumer>
{(requests): React.ReactNode => (
<Component
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{...props as any}
requests={requests}
/>
)}
</SigningContext.Consumer>
);
};
}

View File

@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { AccountsFromCtx } from '../components/types'; import React, { useContext, useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react'; import { AccountContext, Input } from '../components';
import { Input, withAccounts } from '../components';
interface Props { interface Props {
accounts: AccountsFromCtx;
address?: string; address?: string;
className?: string; className?: string;
defaultValue?: string | null; defaultValue?: string | null;
@ -21,7 +18,8 @@ interface Props {
const MIN_LENGTH = 3; const MIN_LENGTH = 3;
function Name ({ accounts, address, className, defaultValue, isFocussed, label = 'a descriptive name for this account', onBlur, onChange }: Props): React.ReactElement<Props> { export default function Name ({ address, className, defaultValue, isFocussed, label = 'a descriptive name for this account', onBlur, onChange }: Props): React.ReactElement<Props> {
const accounts = useContext(AccountContext);
const [name, setName] = useState(''); const [name, setName] = useState('');
const account = accounts.find((account): boolean => account.address === address); const account = accounts.find((account): boolean => account.address === address);
const startValue = (account && account.meta.name) || defaultValue; const startValue = (account && account.meta.name) || defaultValue;
@ -50,5 +48,3 @@ function Name ({ accounts, address, className, defaultValue, isFocussed, label =
/> />
); );
} }
export default withAccounts(Name);