diff --git a/__mocks__/@zondax/filecoin-signing-tools.js b/__mocks__/@zondax/filecoin-signing-tools.js index cb619880d..bd2db3a6d 100644 --- a/__mocks__/@zondax/filecoin-signing-tools.js +++ b/__mocks__/@zondax/filecoin-signing-tools.js @@ -1,4 +1,4 @@ -import createHash from '../../utils/createHash' +import { createHash } from '@glif/react-components' const generateMnemonic = jest .fn() diff --git a/components/Wallet/Replace.tsx b/components/Wallet/Replace.tsx index dc891541f..e0bfb896d 100644 --- a/components/Wallet/Replace.tsx +++ b/components/Wallet/Replace.tsx @@ -17,10 +17,10 @@ import { MessagePending, TxState, useWallet, - useWalletProvider + useWalletProvider, + navigate } from '@glif/react-components' -import { navigate } from '../../utils/urlParams' import { PAGE } from '../../constants' import { logger } from '../../logger' @@ -228,10 +228,14 @@ export enum ReplaceStrategy { CANCEL = 'CANCEL' } +const REPLACE_STRATEGY_PROPTYPE = PropTypes.oneOf( + Object.values(ReplaceStrategy) as Array +) + interface ReplaceProps { strategy: ReplaceStrategy } Replace.propTypes = { - strategy: PropTypes.string.isRequired + strategy: REPLACE_STRATEGY_PROPTYPE.isRequired } diff --git a/components/Wallet/Send.test.tsx b/components/Wallet/Send.test.tsx index 3f5a1474e..6dd2db2fd 100644 --- a/components/Wallet/Send.test.tsx +++ b/components/Wallet/Send.test.tsx @@ -56,6 +56,8 @@ describe('Send', () => { ) + await flushPromises() + // Get HTML elements const header = getByRole(result.container, 'heading') const [recipient, params] = getAllByRole(result.container, 'textbox') diff --git a/components/Wallet/Send.tsx b/components/Wallet/Send.tsx index 9507eef2f..e8783ad22 100644 --- a/components/Wallet/Send.tsx +++ b/components/Wallet/Send.tsx @@ -13,10 +13,10 @@ import { LoginOption, TxState, WalletProviderOpts, - PendingMsgContextType + PendingMsgContextType, + navigate } from '@glif/react-components' -import { navigate } from '../../utils/urlParams' import { PAGE } from '../../constants' export const Send = ({ walletProviderOpts, pendingMsgContext }: SendProps) => { diff --git a/components/Wallet/__snapshots__/Send.test.tsx.snap b/components/Wallet/__snapshots__/Send.test.tsx.snap index db76c28d4..c81879d12 100644 --- a/components/Wallet/__snapshots__/Send.test.tsx.snap +++ b/components/Wallet/__snapshots__/Send.test.tsx.snap @@ -340,6 +340,31 @@ exports[`Send it allows a user to send a message 1`] = ` color: var(--gray-dark) !important; } +.c7 .select-wrapper { + position: relative; +} + +.c7 .select-wrapper select { + width: 100%; + -webkit-appearance: none; + -moz-appearance: none; + appearance: none; +} + +.c7 .select-wrapper .select-arrow { + position: absolute; + pointer-events: none; + top: 50%; + right: 1.2em; + width: 0.6em; + height: 0.6em; + border-right: 1.5px solid var(--black); + border-bottom: 1.5px solid var(--black); + -webkit-transform: translateY(-0.35em) rotate(45deg); + -ms-transform: translateY(-0.35em) rotate(45deg); + transform: translateY(-0.35em) rotate(45deg); +} + .c8 .toggle-wrapper { position: relative; padding: 0.5em 0; @@ -982,6 +1007,31 @@ exports[`Send snapshots after enter recipient state 1`] = ` color: var(--purple-medium); } +.c5 .select-wrapper { + position: relative; +} + +.c5 .select-wrapper select { + width: 100%; + -webkit-appearance: none; + -moz-appearance: none; + appearance: none; +} + +.c5 .select-wrapper .select-arrow { + position: absolute; + pointer-events: none; + top: 50%; + right: 1.2em; + width: 0.6em; + height: 0.6em; + border-right: 1.5px solid var(--black); + border-bottom: 1.5px solid var(--black); + -webkit-transform: translateY(-0.35em) rotate(45deg); + -ms-transform: translateY(-0.35em) rotate(45deg); + transform: translateY(-0.35em) rotate(45deg); +} + .c4 { display: -webkit-box; display: -webkit-flex; @@ -1494,6 +1544,31 @@ exports[`Send snapshots initial state 1`] = ` color: var(--purple-medium); } +.c5 .select-wrapper { + position: relative; +} + +.c5 .select-wrapper select { + width: 100%; + -webkit-appearance: none; + -moz-appearance: none; + appearance: none; +} + +.c5 .select-wrapper .select-arrow { + position: absolute; + pointer-events: none; + top: 50%; + right: 1.2em; + width: 0.6em; + height: 0.6em; + border-right: 1.5px solid var(--black); + border-bottom: 1.5px solid var(--black); + -webkit-transform: translateY(-0.35em) rotate(45deg); + -ms-transform: translateY(-0.35em) rotate(45deg); + transform: translateY(-0.35em) rotate(45deg); +} + .c4 { display: -webkit-box; display: -webkit-flex; diff --git a/components/Wallet/index.tsx b/components/Wallet/index.tsx index 7aea4ab72..d578d2285 100644 --- a/components/Wallet/index.tsx +++ b/components/Wallet/index.tsx @@ -12,14 +12,12 @@ import { useWalletProvider, useWallet, hasLedgerError, - reportLedgerConfigError + reportLedgerConfigError, + generateRouteWithRequiredUrlParams, + navigate } from '@glif/react-components' import { logger } from '../../logger' -import { - generateRouteWithRequiredUrlParams, - navigate -} from '../../utils/urlParams' import { PAGE } from '../../constants' const Cards = styled.div` diff --git a/components/WalletPageLoggedIn.tsx b/components/WalletPageLoggedIn.tsx index f4c8c631e..80926a9ab 100644 --- a/components/WalletPageLoggedIn.tsx +++ b/components/WalletPageLoggedIn.tsx @@ -4,11 +4,12 @@ import { LoginOption, NetworkConnection, useWallet, - useWalletProvider + useWalletProvider, + resetWallet, + navigate } from '@glif/react-components' import { useRouter } from 'next/router' -import { resetWallet, navigate } from '../utils/urlParams' import { GLIF_DISCORD, PAGE } from '../constants' import WalletPage from './WalletPage' diff --git a/constants.ts b/constants.ts index 656dc5a29..92c199b23 100644 --- a/constants.ts +++ b/constants.ts @@ -1,5 +1,3 @@ -import { FilecoinNumber } from '@glif/filecoin-number' - /* WALLET TYPES */ export const LEDGER = 'LEDGER' export const HD_WALLET = 'HD_WALLET' @@ -23,29 +21,14 @@ export const GLIF_TWITTER = 'https://twitter.com/glifio' /* NETWORK VARS */ export const MAINNET = 'f' export const TESTNET = 't' - export const MAINNET_PATH_CODE = 461 export const TESTNET_PATH_CODE = 1 -export const EXEC_ACTOR = 'f01' - -/* TX METHOD TYPES */ -export const SEND = 'SEND' -export const PROPOSE = 'PROPOSE' -export const EXEC = 'EXEC' /* FILECOIN APP VERSION MIN */ export const LEDGER_VERSION_MAJOR = 0 export const LEDGER_VERSION_MINOR = 20 export const LEDGER_VERSION_PATCH = 0 -/* GAS CONSTANTS */ -export const emptyGasInfo = { - estimatedTransactionFee: new FilecoinNumber('0', 'attofil'), - gasPremium: new FilecoinNumber('0', 'attofil'), - gasFeeCap: new FilecoinNumber('0', 'attofil'), - gasLimit: new FilecoinNumber('0', 'attofil') -} - /* PAGES */ /* eslint-disable no-unused-vars */ export enum PAGE { diff --git a/customPropTypes.js b/customPropTypes.js deleted file mode 100644 index 13d6265a1..000000000 --- a/customPropTypes.js +++ /dev/null @@ -1,94 +0,0 @@ -import { shape, string, oneOfType, number, oneOf, object } from 'prop-types' -import { validateAddressString } from '@glif/filecoin-address' -import { validatePath } from '@glif/filecoin-wallet-provider' -import { validateMnemonic } from 'bip39' -import { - SINGLE_KEY, - IMPORT_MNEMONIC, - IMPORT_SINGLE_KEY, - LEDGER -} from './constants' - -export const ADDRESS_PROPTYPE = (props, propName, componentName) => { - if (!validateAddressString(props[propName])) - return new Error( - `Invalid prop: ${propName} supplied to ${componentName}. Validation failed.` - ) - - return null -} - -export const MNEMONIC_PROPTYPE = (props, propName, componentName) => { - if (!validateMnemonic(props[propName])) - return new Error( - `Invalid prop: ${propName} supplied to ${componentName}. Validation failed.` - ) - - return null -} - -export const FILECOIN_NUMBER_PROP = (props, propName, componentName) => { - // instanceof prop checking is broken in nextjs on server side render cycles - const representsANum = Number.isNaN(Number(props[propName].toString())) - const hasFilecoinNumMethods = !!( - props[propName].toFil && - props[propName].toAttoFil && - props[propName].toPicoFil - ) - if (!(representsANum || hasFilecoinNumMethods)) - return new Error( - `Invalid prop: ${propName} supplied to ${componentName}. Validation failed.` - ) - - return null -} - -export const HD_PATH_PROP = (props, propName, componentName) => { - if (!validatePath(props[propName]) && props[propName] !== SINGLE_KEY) - return new Error( - `Invalid prop: ${propName} supplied to ${componentName}. Validation failed.` - ) - - return null -} - -export const WALLET_PROP_TYPE = shape({ - balance: FILECOIN_NUMBER_PROP, - address: ADDRESS_PROPTYPE, - path: HD_PATH_PROP, - type: oneOf([IMPORT_MNEMONIC, IMPORT_SINGLE_KEY, LEDGER]), - index: number -}) - -export const NO_WALLET_PROP_TYPE = shape({ - balance: FILECOIN_NUMBER_PROP, - address: string, - path: string, - index: number -}) - -export const MESSAGE_PROPS = shape({ - /** - * Message sent to this address - */ - to: ADDRESS_PROPTYPE, - /** - * Message sent from this address - */ - from: ADDRESS_PROPTYPE, - /** - * The amount of FIL sent in the message - */ - value: string.isRequired, - /** - * The message's cid - */ - cid: string.isRequired, - /** - * Either pending or confirmed - */ - status: oneOf(['confirmed', 'pending']).isRequired, - timestamp: oneOfType([string, number]).isRequired, - method: string.isRequired, - params: object.isRequired -}) diff --git a/package-lock.json b/package-lock.json index 2464418a2..ec0a7f2c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,14 +10,14 @@ "license": "(Apache-2.0 OR MIT)", "dependencies": { "@apollo/client": "^3.5.7", - "@glif/base-css": "^0.0.21", + "@glif/base-css": "^0.0.23", "@glif/filecoin-address": "^2.0.0-beta.3", "@glif/filecoin-message": "^2.0.0-beta.12", "@glif/filecoin-message-confirmer": "^2.0.0-beta.8", "@glif/filecoin-number": "^2.0.0-beta.0", "@glif/filecoin-rpc-client": "^2.0.0-beta.8", "@glif/filecoin-wallet-provider": "^2.0.0-beta.15", - "@glif/react-components": "^2.0.0-alpha.94", + "@glif/react-components": "^2.0.0-alpha.98", "@ledgerhq/hw-transport-webhid": "^6.4.1", "@zondax/filecoin-signing-tools": "^0.20.1", "@zondax/ledger-filecoin": "^0.11.2", @@ -988,9 +988,9 @@ } }, "node_modules/@glif/base-css": { - "version": "0.0.21", - "resolved": "https://registry.npmjs.org/@glif/base-css/-/base-css-0.0.21.tgz", - "integrity": "sha512-M6XSyPU8wfEALRcnMEbPfmNb6P0rTsbsfpDMYoJQdPvXzb/fyAxvB0TNBJpcf2+r4riZ8HsO6Terlqkv+VmjUg==", + "version": "0.0.23", + "resolved": "https://registry.npmjs.org/@glif/base-css/-/base-css-0.0.23.tgz", + "integrity": "sha512-CLYwpWDXhk5XAm5L6IsGIpAMOccYOo/0eAEL3JobyZQdFCM4L7IhVNZuRxsjiEF4tP8bo12/XQ/ujAqDS4GVPw==", "dependencies": { "normalize.css": "^8.0.1", "postcss": "^8.4.6", @@ -1150,14 +1150,14 @@ } }, "node_modules/@glif/react-components": { - "version": "2.0.0-alpha.94", - "resolved": "https://registry.npmjs.org/@glif/react-components/-/react-components-2.0.0-alpha.94.tgz", - "integrity": "sha512-QsOncoC+zuW2jWsqhmFIUltlLAkYeJ7wIm51ylOKEM6Ezgjlk3aPcpdnUCdbLgeJMoAJ/Lqfxl4Y7B+T02Z6Kw==", + "version": "2.0.0-alpha.98", + "resolved": "https://registry.npmjs.org/@glif/react-components/-/react-components-2.0.0-alpha.98.tgz", + "integrity": "sha512-Z5jA3Q5LXB4DsNkVM31d0q+twDWl1zvWmqLTtlpQV75pcQF6sqwtgiQMEj8bHgJqveKbVVn6dqfgnpor7pSrwA==", "dependencies": { "@apollo/client": "^3.5.10", "@chainsafe/filsnap-adapter": "^2.1.1", "@chainsafe/filsnap-types": "^2.1.2", - "@glif/base-css": "^0.0.22", + "@glif/base-css": "^0.0.23", "@glif/filecoin-address": "^2.0.0-beta.3", "@glif/filecoin-message": "^2.0.0-beta.12", "@glif/filecoin-number": "^2.0.0-beta.0", @@ -1187,17 +1187,6 @@ "styled-components": ">=5.2.1" } }, - "node_modules/@glif/react-components/node_modules/@glif/base-css": { - "version": "0.0.22", - "resolved": "https://registry.npmjs.org/@glif/base-css/-/base-css-0.0.22.tgz", - "integrity": "sha512-bSZsXB87WbmRwykqhZWvRlGSyjbzYh/IOHLkFVfIAcuZtkq8+QWfhPR0jzPtHnPE70AoqpeUai96F5CBSzMcJA==", - "dependencies": { - "normalize.css": "^8.0.1", - "postcss": "^8.4.6", - "postcss-nesting": "^10.1.7", - "postcss-url": "^10.1.3" - } - }, "node_modules/@glif/react-components/node_modules/glob-parent": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", @@ -13152,9 +13141,9 @@ } }, "@glif/base-css": { - "version": "0.0.21", - "resolved": "https://registry.npmjs.org/@glif/base-css/-/base-css-0.0.21.tgz", - "integrity": "sha512-M6XSyPU8wfEALRcnMEbPfmNb6P0rTsbsfpDMYoJQdPvXzb/fyAxvB0TNBJpcf2+r4riZ8HsO6Terlqkv+VmjUg==", + "version": "0.0.23", + "resolved": "https://registry.npmjs.org/@glif/base-css/-/base-css-0.0.23.tgz", + "integrity": "sha512-CLYwpWDXhk5XAm5L6IsGIpAMOccYOo/0eAEL3JobyZQdFCM4L7IhVNZuRxsjiEF4tP8bo12/XQ/ujAqDS4GVPw==", "requires": { "normalize.css": "^8.0.1", "postcss": "^8.4.6", @@ -13313,14 +13302,14 @@ } }, "@glif/react-components": { - "version": "2.0.0-alpha.94", - "resolved": "https://registry.npmjs.org/@glif/react-components/-/react-components-2.0.0-alpha.94.tgz", - "integrity": "sha512-QsOncoC+zuW2jWsqhmFIUltlLAkYeJ7wIm51ylOKEM6Ezgjlk3aPcpdnUCdbLgeJMoAJ/Lqfxl4Y7B+T02Z6Kw==", + "version": "2.0.0-alpha.98", + "resolved": "https://registry.npmjs.org/@glif/react-components/-/react-components-2.0.0-alpha.98.tgz", + "integrity": "sha512-Z5jA3Q5LXB4DsNkVM31d0q+twDWl1zvWmqLTtlpQV75pcQF6sqwtgiQMEj8bHgJqveKbVVn6dqfgnpor7pSrwA==", "requires": { "@apollo/client": "^3.5.10", "@chainsafe/filsnap-adapter": "^2.1.1", "@chainsafe/filsnap-types": "^2.1.2", - "@glif/base-css": "^0.0.22", + "@glif/base-css": "^0.0.23", "@glif/filecoin-address": "^2.0.0-beta.3", "@glif/filecoin-message": "^2.0.0-beta.12", "@glif/filecoin-number": "^2.0.0-beta.0", @@ -13344,17 +13333,6 @@ "ws": "^8.4.2" }, "dependencies": { - "@glif/base-css": { - "version": "0.0.22", - "resolved": "https://registry.npmjs.org/@glif/base-css/-/base-css-0.0.22.tgz", - "integrity": "sha512-bSZsXB87WbmRwykqhZWvRlGSyjbzYh/IOHLkFVfIAcuZtkq8+QWfhPR0jzPtHnPE70AoqpeUai96F5CBSzMcJA==", - "requires": { - "normalize.css": "^8.0.1", - "postcss": "^8.4.6", - "postcss-nesting": "^10.1.7", - "postcss-url": "^10.1.3" - } - }, "glob-parent": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", diff --git a/package.json b/package.json index d0552987e..9708bffea 100644 --- a/package.json +++ b/package.json @@ -21,14 +21,14 @@ }, "dependencies": { "@apollo/client": "^3.5.7", - "@glif/base-css": "^0.0.21", + "@glif/base-css": "^0.0.23", "@glif/filecoin-address": "^2.0.0-beta.3", "@glif/filecoin-message": "^2.0.0-beta.12", "@glif/filecoin-message-confirmer": "^2.0.0-beta.8", "@glif/filecoin-number": "^2.0.0-beta.0", "@glif/filecoin-rpc-client": "^2.0.0-beta.8", "@glif/filecoin-wallet-provider": "^2.0.0-beta.15", - "@glif/react-components": "^2.0.0-alpha.94", + "@glif/react-components": "^2.0.0-alpha.98", "@ledgerhq/hw-transport-webhid": "^6.4.1", "@zondax/filecoin-signing-tools": "^0.20.1", "@zondax/ledger-filecoin": "^0.11.2", diff --git a/pages/connect/burner/create-seed.tsx b/pages/connect/burner/create-seed.tsx index 41fa61269..082420ee0 100644 --- a/pages/connect/burner/create-seed.tsx +++ b/pages/connect/burner/create-seed.tsx @@ -2,18 +2,18 @@ import React, { useCallback } from 'react' import { ConnectWallet, OneColumnCentered, - useDesktopBrowser + useDesktopBrowser, + useWalletProvider, + navigate } from '@glif/react-components' import { useRouter } from 'next/router' import WalletPage from '../../../components/WalletPage' -import useReset from '../../../utils/useReset' -import { navigate } from '../../../utils/urlParams' import { PAGE } from '../../../constants' export default function CreateSeed() { useDesktopBrowser() const router = useRouter() - const resetState = useReset() + const { resetState } = useWalletProvider() const back = useCallback(() => { router.replace('/') resetState() diff --git a/pages/connect/burner/import-private-key.tsx b/pages/connect/burner/import-private-key.tsx index 96bc871d6..059b6cf7e 100644 --- a/pages/connect/burner/import-private-key.tsx +++ b/pages/connect/burner/import-private-key.tsx @@ -2,18 +2,18 @@ import React, { useCallback } from 'react' import { ConnectWallet, OneColumnCentered, - useDesktopBrowser + useDesktopBrowser, + useWalletProvider, + navigate } from '@glif/react-components' import { useRouter } from 'next/router' import WalletPage from '../../../components/WalletPage' -import useReset from '../../../utils/useReset' -import { navigate } from '../../../utils/urlParams' import { PAGE } from '../../../constants' export default function ImportPK() { useDesktopBrowser() const router = useRouter() - const resetState = useReset() + const { resetState } = useWalletProvider() const back = useCallback(() => { router.replace('/') resetState() diff --git a/pages/connect/burner/import-seed.tsx b/pages/connect/burner/import-seed.tsx index ae21d44f3..bff65187a 100644 --- a/pages/connect/burner/import-seed.tsx +++ b/pages/connect/burner/import-seed.tsx @@ -2,18 +2,18 @@ import React, { useCallback } from 'react' import { ConnectWallet, OneColumnCentered, - useDesktopBrowser + useDesktopBrowser, + useWalletProvider, + navigate } from '@glif/react-components' import { useRouter } from 'next/router' import WalletPage from '../../../components/WalletPage' -import useReset from '../../../utils/useReset' -import { navigate } from '../../../utils/urlParams' import { PAGE } from '../../../constants' export default function ImportSeed() { useDesktopBrowser() const router = useRouter() - const resetState = useReset() + const { resetState } = useWalletProvider() const back = useCallback(() => { router.replace('/') resetState() diff --git a/pages/connect/ledger.tsx b/pages/connect/ledger.tsx index d54ba2c7c..e471b0609 100644 --- a/pages/connect/ledger.tsx +++ b/pages/connect/ledger.tsx @@ -2,18 +2,18 @@ import React, { useCallback } from 'react' import { OneColumnCentered, useChromeDesktopBrowser, - ConnectWallet + useWalletProvider, + ConnectWallet, + navigate } from '@glif/react-components' import { useRouter } from 'next/router' import WalletPage from '../../components/WalletPage' -import useReset from '../../utils/useReset' -import { navigate } from '../../utils/urlParams' import { PAGE } from '../../constants' export default function ConnectLedger() { useChromeDesktopBrowser() const router = useRouter() - const resetState = useReset() + const { resetState } = useWalletProvider() const back = useCallback(() => { router.replace('/') resetState() diff --git a/pages/connect/metamask.tsx b/pages/connect/metamask.tsx index e5a282465..f445322a8 100644 --- a/pages/connect/metamask.tsx +++ b/pages/connect/metamask.tsx @@ -2,18 +2,18 @@ import { useCallback } from 'react' import { OneColumnCentered, useDesktopBrowser, - ConnectWallet + useWalletProvider, + ConnectWallet, + navigate } from '@glif/react-components' import { useRouter } from 'next/router' import WalletPage from '../../components/WalletPage' -import useReset from '../../utils/useReset' -import { navigate } from '../../utils/urlParams' import { PAGE } from '../../constants' export default function ConnectMetaMask() { useDesktopBrowser() const router = useRouter() - const resetState = useReset() + const { resetState } = useWalletProvider() const back = useCallback(() => { router.replace('/') resetState() diff --git a/pages/home/accounts.tsx b/pages/home/accounts.tsx index 3cc712352..d35cb91f7 100644 --- a/pages/home/accounts.tsx +++ b/pages/home/accounts.tsx @@ -4,12 +4,12 @@ import { CoinType } from '@glif/filecoin-address' import { AccountSelector, RequireWallet, - OneColumn + OneColumn, + navigate } from '@glif/react-components' import WalletPageLoggedIn from '../../components/WalletPageLoggedIn' import { PAGE } from '../../constants' -import { navigate } from '../../utils/urlParams' const COIN_TYPE = process.env.NEXT_PUBLIC_COIN_TYPE! as CoinType const IS_PROD = process.env.NEXT_PUBLIC_IS_PROD diff --git a/pages/home/index.tsx b/pages/home/index.tsx index b9d4ac868..763ac7f33 100644 --- a/pages/home/index.tsx +++ b/pages/home/index.tsx @@ -1,10 +1,9 @@ import { useCallback } from 'react' import { useRouter } from 'next/router' -import { RequireWallet } from '@glif/react-components' +import { RequireWallet, navigate } from '@glif/react-components' import WalletPageLoggedIn from '../../components/WalletPageLoggedIn' import { WalletView } from '../../components' -import { navigate } from '../../utils/urlParams' import { PAGE } from '../../constants' const Home = () => { diff --git a/pages/message/cancel.tsx b/pages/message/cancel.tsx index 7f01a24c0..fa7f66214 100644 --- a/pages/message/cancel.tsx +++ b/pages/message/cancel.tsx @@ -1,10 +1,13 @@ import { useCallback } from 'react' import { useRouter } from 'next/router' -import { RequireWallet, OneColumnCentered } from '@glif/react-components' +import { + RequireWallet, + OneColumnCentered, + navigate +} from '@glif/react-components' import WalletPageLoggedIn from '../../components/WalletPageLoggedIn' import { Replace, ReplaceStrategy } from '../../components/Wallet/Replace' -import { navigate } from '../../utils/urlParams' import { PAGE } from '../../constants' const CancelPage = () => { diff --git a/pages/message/speed-up.tsx b/pages/message/speed-up.tsx index d26b5f08e..ed3097814 100644 --- a/pages/message/speed-up.tsx +++ b/pages/message/speed-up.tsx @@ -1,10 +1,13 @@ import { useCallback } from 'react' import { useRouter } from 'next/router' -import { RequireWallet, OneColumnCentered } from '@glif/react-components' +import { + RequireWallet, + OneColumnCentered, + navigate +} from '@glif/react-components' import WalletPageLoggedIn from '../../components/WalletPageLoggedIn' import { Replace, ReplaceStrategy } from '../../components/Wallet/Replace' -import { navigate } from '../../utils/urlParams' import { PAGE } from '../../constants' const SpeedUpPage = () => { diff --git a/pages/send.tsx b/pages/send.tsx index bb762cf2a..da98da0dd 100644 --- a/pages/send.tsx +++ b/pages/send.tsx @@ -1,10 +1,13 @@ import { useCallback } from 'react' import { useRouter } from 'next/router' -import { OneColumnCentered, RequireWallet } from '@glif/react-components' +import { + OneColumnCentered, + RequireWallet, + navigate +} from '@glif/react-components' import WalletPageLoggedIn from '../components/WalletPageLoggedIn' import { Send } from '../components/Wallet/Send' -import { navigate } from '../utils/urlParams' import { PAGE } from '../constants' const SendPage = () => { diff --git a/test-utils/composeMockAppTree/composeState.ts b/test-utils/composeMockAppTree/composeState.ts index b878e5df2..926f21e79 100644 --- a/test-utils/composeMockAppTree/composeState.ts +++ b/test-utils/composeMockAppTree/composeState.ts @@ -1,7 +1,6 @@ import { FilecoinNumber } from '@glif/filecoin-number' -import { WalletProviderState } from '@glif/react-components' +import { WalletProviderState, createPath } from '@glif/react-components' -import createPath from '../../utils/createPath' import { IMPORT_MNEMONIC, IMPORT_SINGLE_KEY } from '../../constants' import { mockWalletProviderInstance } from '../../__mocks__/@glif/filecoin-wallet-provider' import { WALLET_ADDRESS } from '../constants' diff --git a/test-utils/composeMockAppTree/createWalletProviderContextFuncs.js b/test-utils/composeMockAppTree/createWalletProviderContextFuncs.js index e70055f31..c0782695d 100644 --- a/test-utils/composeMockAppTree/createWalletProviderContextFuncs.js +++ b/test-utils/composeMockAppTree/createWalletProviderContextFuncs.js @@ -1,6 +1,6 @@ import { FilecoinNumber } from '@glif/filecoin-number' +import { createPath } from '@glif/react-components' import { TESTNET_PATH_CODE } from '../../constants' -import createPath from '../../utils/createPath' import { setLoginOption, diff --git a/utils/convertAddrToFPrefix/index.js b/utils/convertAddrToFPrefix/index.js deleted file mode 100644 index 4e7ab9964..000000000 --- a/utils/convertAddrToFPrefix/index.js +++ /dev/null @@ -1,3 +0,0 @@ -const converAddrToFPrefix = (address) => `f${address.slice(1)}` - -export default converAddrToFPrefix diff --git a/utils/convertAddrToFPrefix/index.test.js b/utils/convertAddrToFPrefix/index.test.js deleted file mode 100644 index 1f9d22fe9..000000000 --- a/utils/convertAddrToFPrefix/index.test.js +++ /dev/null @@ -1,10 +0,0 @@ -import convertAddressToF from '.' - -describe('convertAddrToF', () => { - test('it converts a t address to an f address', () => { - expect(convertAddressToF('t033525')).toBe('f033525') - }) - test('it keeps an f address to an f address', () => { - expect(convertAddressToF('f033525')).toBe('f033525') - }) -}) diff --git a/utils/copyToClipboard/index.js b/utils/copyToClipboard/index.js deleted file mode 100644 index 91be958cb..000000000 --- a/utils/copyToClipboard/index.js +++ /dev/null @@ -1,19 +0,0 @@ -const fallbackCopyTextToClipboard = (text) => { - const textArea = document.createElement('textarea') - textArea.value = text - textArea.style.position = 'fixed' // avoid scrolling to bottom - document.body.appendChild(textArea) - textArea.focus() - textArea.select() - document.execCommand('copy') - document.body.removeChild(textArea) -} - -export default (text) => - new Promise((resolve, reject) => { - if (!navigator.clipboard) { - fallbackCopyTextToClipboard(text) - return resolve() - } - navigator.clipboard.writeText(text).then(resolve, reject) - }) diff --git a/utils/copyToClipboard/index.test.js b/utils/copyToClipboard/index.test.js deleted file mode 100644 index a5df8c580..000000000 --- a/utils/copyToClipboard/index.test.js +++ /dev/null @@ -1,32 +0,0 @@ -import copyToClipboard from '.' - -describe('copyToClipboard', () => { - test('it calls the navigator.clipboard.writeText method with the passed text', async () => { - const { navigator } = window - const mockWriteText = jest.fn(() => Promise.resolve()) - const mockNavigator = { - clipboard: { - writeText: mockWriteText - } - } - delete window.navigator - window.navigator = mockNavigator - await copyToClipboard('text') - window.navigator = navigator - - expect(mockWriteText).toHaveBeenCalled() - }) - - test('it calls the document.execCommand method with "copy" when navigator.clipboard is undefined', async () => { - const mockDocExec = jest.fn() - window.document.execCommand = mockDocExec - const { navigator } = window - const mockNavigator = {} - delete window.navigator - window.navigator = mockNavigator - await copyToClipboard('text') - expect(mockDocExec).toHaveBeenCalledWith('copy') - window.navigator = navigator - delete window.document.execCommand - }) -}) diff --git a/utils/createHash/index.test.ts b/utils/createHash/index.test.ts deleted file mode 100644 index c5a8e22d0..000000000 --- a/utils/createHash/index.test.ts +++ /dev/null @@ -1,11 +0,0 @@ -import createHash from '.' - -describe('createHash', () => { - test('it hashes properly according to predefined values', () => { - const val = 'Qmcv45ZPc3oEwsbcHMRcs3AG59Rp8EUFr6Dm512KUswpRA' - - expect(createHash(val)).toBe( - 'fb97c41fb9a1e8fce8ed5386b2201abecbf5d90c6fa5b1a9f246034f9ba4c9a1' - ) - }) -}) diff --git a/utils/createHash/index.ts b/utils/createHash/index.ts deleted file mode 100644 index ed64e66c9..000000000 --- a/utils/createHash/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -import crypto from 'crypto' - -export const createHash = (val) => - crypto.createHash('sha256').update(val).digest('hex') - -export default createHash diff --git a/utils/createPath/index.test.js b/utils/createPath/index.test.js deleted file mode 100644 index d80a6c3aa..000000000 --- a/utils/createPath/index.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import createPath from '.' - -// bip44 path spec https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#path-levels - -describe('createPath', () => { - test('it creates a path based on an index and networkID', () => { - expect(createPath(1, 1)).toBe("m/44'/1'/0'/0/1") - expect(createPath(461, 2)).toBe("m/44'/461'/0'/0/2") - }) - - test('it hardens the first three path values', () => { - const path = createPath(1, 1).split('/') - expect(path[0][path[0].length - 1]).not.toBe("'") - expect(path[1][path[1].length - 1]).toBe("'") - expect(path[2][path[2].length - 1]).toBe("'") - expect(path[3][path[3].length - 1]).toBe("'") - expect(path[4][path[4].length - 1]).not.toBe("'") - expect(path[5][path[5].length - 1]).not.toBe("'") - }) - - test('it throws an error when a bad network code is passed', () => { - expect(() => createPath(0, 1)).toThrow() - expect(() => createPath(461, 2)).not.toThrow() - }) -}) diff --git a/utils/createPath/index.ts b/utils/createPath/index.ts deleted file mode 100644 index 1bc645c1f..000000000 --- a/utils/createPath/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { CoinType } from '@glif/filecoin-address' -import { MAINNET_PATH_CODE, TESTNET_PATH_CODE } from '../../constants' - -type CoinTypeCode = 461 | 1 - -const createPath = (coinTypeCode: CoinTypeCode, i: number) => { - if (coinTypeCode !== MAINNET_PATH_CODE && coinTypeCode !== TESTNET_PATH_CODE) - throw new Error('Invalid cointype code passed') - return `m/44'/${coinTypeCode}'/0'/0/${i}` -} - -export const coinTypeCode = (coinType: CoinType): CoinTypeCode => { - if (coinType === 't') return 1 - if (coinType === 'f') return 461 - throw new Error('Unrecognized CoinType') -} - -export default createPath diff --git a/utils/isBase64/index.js b/utils/isBase64/index.js deleted file mode 100644 index 70d8a3562..000000000 --- a/utils/isBase64/index.js +++ /dev/null @@ -1,10 +0,0 @@ -export default function isBase64(str) { - if (str === '' || str.trim() === '') { - return false - } - try { - return btoa(atob(str)) === str - } catch (err) { - return false - } -} diff --git a/utils/isBase64/index.test.js b/utils/isBase64/index.test.js deleted file mode 100644 index 7031587b0..000000000 --- a/utils/isBase64/index.test.js +++ /dev/null @@ -1,34 +0,0 @@ -import isBase64 from '.' -describe('isBase64', () => { - test('it returns true for valid base64', () => { - expect( - isBase64('WTVkaFdoSXJFS0N1RkJGZmdKVG1Vc1RHTlZBVzZnV1laa1MwaERWTHN1VQ==') - ).toBe(true) - expect( - isBase64('SWlZT0FacmY4SmNIbUFRUjVDMld2VnBKNkVkblFSeDFGUHgwcV9QakJHVQ==') - ).toBe(true) - expect( - isBase64('TU9hY0pFdXhTbkRncTQ0eXpaRmMzdlFKdkN1Tm9sZ3hPYldHWWxQSGdBSQ==') - ).toBe(true) - expect( - isBase64('b013QnpMSWY2b1Y2WnVfS09RNGYxMXZQZG1RcDM2eUNocm10dGpzRGpZVQ==') - ).toBe(true) - expect( - isBase64('bWJydW0zelBMNWwzTkhJRWRSSnJiV0ktV3B0WFJVdkhObng1SEdaZFY3bw==') - ).toBe(true) - }) - - test('it returns false for invalid base64', () => { - expect( - isBase64('WTVkaFdoSXJFS0N1RkJGZmdKVG1Vc1RHTlZBVzZnV1laa1MwaERWTHN1VQ') - ).toBe(false) - expect(isBase64('SWlZT0FacmY4SmNIbUFRUjVDMld2VnBKNkVkblFkJHVQ==')).toBe( - false - ) - expect(isBase64('hi')).toBe(false) - expect(isBase64('fdsafgdsatreawfeawvdsab')).toBe(false) - expect( - isBase64('bWJydW0zelBMNWwzTkhJRWRSSnJiV0ktV3B0WFJVdkhObng1SEdaZFY3bw=') - ).toBe(false) - }) -}) diff --git a/utils/makeFriendlyBalance/index.js b/utils/makeFriendlyBalance/index.js deleted file mode 100644 index 56ba97d90..000000000 --- a/utils/makeFriendlyBalance/index.js +++ /dev/null @@ -1,63 +0,0 @@ -import { BigNumber } from '@glif/filecoin-number' - -const makeFriendly = (bigNumber, denom) => { - const stringifiedNumber = bigNumber.toFixed(0) - let idx = Number(stringifiedNumber.length) % 3 - if (idx === 0) idx = 3 - const base = stringifiedNumber.slice(0, idx) - const dangler = stringifiedNumber.slice(idx, idx + 3) - return `${base}.${dangler}${denom}` -} - -const addComparisonOperator = (endNum, originalNum) => { - if (new BigNumber(endNum).isNaN()) return endNum - if (new BigNumber(originalNum).isEqualTo(new BigNumber(endNum))) return endNum - if (new BigNumber(originalNum).isLessThan(new BigNumber(endNum))) - return `< ${endNum}` - if (new BigNumber(originalNum).isGreaterThan(new BigNumber(endNum))) - return endNum -} - -export default (bigNumber, dp = 3, pretty = true) => { - if (!bigNumber) throw new Error('No number passed to big number') - if (!BigNumber.isBigNumber(bigNumber)) return bigNumber - if (!pretty) return bigNumber.toString() - if (bigNumber.isEqualTo(0)) return '0' - if (bigNumber.toString() === 'NaN') { - throw new Error('Number must be a valid number') - } - if (bigNumber.isLessThan(0)) throw new Error('Cannot have a negative balance') - if (bigNumber.isGreaterThan(0) && bigNumber.isLessThanOrEqualTo(1)) { - if (bigNumber.dp(dp, BigNumber.ROUND_DOWN).isEqualTo('0')) { - let abbrev = '0.' - for (let i = 0; i < dp - 1; i += 1) { - abbrev += '0' - } - return addComparisonOperator(`${abbrev}1`, bigNumber) - } - - return addComparisonOperator( - bigNumber.dp(dp, BigNumber.ROUND_DOWN).toString(), - bigNumber - ) - } - if (bigNumber.isGreaterThan(1) && bigNumber.isLessThanOrEqualTo(1000)) { - return bigNumber.dp(dp, BigNumber.ROUND_DOWN).toString() - } - if (bigNumber.isGreaterThan(1000) && bigNumber.isLessThan(1000000)) { - return makeFriendly(bigNumber, 'K') - } - if ( - bigNumber.isGreaterThanOrEqualTo(1000000) && - bigNumber.isLessThan(1000000000) - ) { - return makeFriendly(bigNumber, 'M') - } - if ( - bigNumber.isGreaterThanOrEqualTo(1000000000) && - bigNumber.isLessThan(1000000000000) - ) { - return makeFriendly(bigNumber, 'B') - } - return '> 999.9B' -} diff --git a/utils/makeFriendlyBalance/index.test.js b/utils/makeFriendlyBalance/index.test.js deleted file mode 100644 index d0dbfc503..000000000 --- a/utils/makeFriendlyBalance/index.test.js +++ /dev/null @@ -1,137 +0,0 @@ -import { BigNumber } from '@glif/filecoin-number' -import makeFriendlyBalance from '.' - -describe('makeFriendlyBalance', () => { - test('it throws an error if no number is passed', () => { - expect(() => makeFriendlyBalance()).toThrow() - }) - - test('it returns the number with no manipulation if the number is not a BigNumber', () => { - expect(makeFriendlyBalance(1000)).toEqual(1000) - }) - - test('it throws an error if it does not receive a number', () => { - expect(() => makeFriendlyBalance(new BigNumber('hi'))).toThrow() - }) - - test('it throws an error for negative numbers', () => { - expect(() => makeFriendlyBalance(new BigNumber('-2'))).toThrow() - }) - - test('it returns the number with no manipulation if the pretty argument is passed as false', () => { - expect(makeFriendlyBalance(new BigNumber(0.000043245235), 5, false)).toBe( - '0.000043245235' - ) - expect(makeFriendlyBalance(new BigNumber('134'), 5, false)).toBe('134') - expect(makeFriendlyBalance(new BigNumber(532416423), 5, false)).toBe( - '532416423' - ) - // javascripts biggest number, after this, things get tricky - expect(makeFriendlyBalance(new BigNumber(9007199254740991), 5, false)).toBe( - '9007199254740991' - ) - }) - - test('it returns "< number" when the decimal is smaller than the num of dps passed', () => { - expect(makeFriendlyBalance(new BigNumber('0000.00001'), 2)).toEqual( - '< 0.01' - ) - - expect( - makeFriendlyBalance(new BigNumber('0.01104953007959368107188269908'), 2) - ).toEqual('0.01') - - expect( - makeFriendlyBalance(new BigNumber('.0000000001230500054'), 6) - ).toEqual('< 0.000001') - - expect(makeFriendlyBalance(new BigNumber('0000.00001'), 5)).toEqual( - '0.00001' - ) - - expect( - makeFriendlyBalance(new BigNumber('.0000000001230500054'), 16) - ).toEqual('0.00000000012305') - }) - - test('it prettifies numbers between 1-1000', () => { - expect(makeFriendlyBalance(new BigNumber('1'), 3)).toEqual('1') - expect(makeFriendlyBalance(new BigNumber('1.2'), 3)).toEqual('1.2') - expect(makeFriendlyBalance(new BigNumber('1.23'), 3)).toEqual('1.23') - expect(makeFriendlyBalance(new BigNumber('10'), 3)).toEqual('10') - expect(makeFriendlyBalance(new BigNumber('10.2'), 3)).toEqual('10.2') - expect(makeFriendlyBalance(new BigNumber('10.234'), 3)).toEqual('10.234') - expect(makeFriendlyBalance(new BigNumber('100'), 16)).toEqual('100') - expect(makeFriendlyBalance(new BigNumber('100.000124'), 4)).toEqual( - '100.0001' - ) - expect(makeFriendlyBalance(new BigNumber('100.000124'), 5)).toEqual( - '100.00012' - ) - expect(makeFriendlyBalance(new BigNumber('100.000124'), 7)).toEqual( - '100.000124' - ) - - expect(makeFriendlyBalance(new BigNumber('1000'), 7)).toEqual('1000') - expect(makeFriendlyBalance(new BigNumber('1000.23'), 3)).toEqual('1.000K') - }) - - test('it adds 3 approximation points and "K" to the end of numbers between 1000 and 999999.9999.....', () => { - expect(makeFriendlyBalance(new BigNumber('100002'), 7)).toEqual('100.002K') - expect(makeFriendlyBalance(new BigNumber('100202.02343267'), 7)).toEqual( - '100.202K' - ) - expect(makeFriendlyBalance(new BigNumber('10002.02343267'), 7)).toEqual( - '10.002K' - ) - expect(makeFriendlyBalance(new BigNumber('100102.23'), 7)).toEqual( - '100.102K' - ) - expect(makeFriendlyBalance(new BigNumber('100999.3'), 7)).toEqual( - '100.999K' - ) - expect(makeFriendlyBalance(new BigNumber('100999.3'), 7)).toEqual( - '100.999K' - ) - expect(makeFriendlyBalance(new BigNumber('1002.02343267'), 7)).toEqual( - '1.002K' - ) - }) - - test('it adds 3 approximation points and "M" to the end of numbers between 1000000 and 999999999.9999.....', () => { - expect(makeFriendlyBalance(new BigNumber('1202000'), 7)).toEqual('1.202M') - expect(makeFriendlyBalance(new BigNumber('12020002.2345'), 7)).toEqual( - '12.020M' - ) - expect(makeFriendlyBalance(new BigNumber('100002000'), 7)).toEqual( - '100.002M' - ) - expect(makeFriendlyBalance(new BigNumber('100002000.02343267'), 7)).toEqual( - '100.002M' - ) - expect(makeFriendlyBalance(new BigNumber('100102000'), 7)).toEqual( - '100.102M' - ) - expect(makeFriendlyBalance(new BigNumber('100999000'), 7)).toEqual( - '100.999M' - ) - }) - - test('it adds 3 approximation points and "B" to the end of numbers between 1000000000 and 999999999999.9999.....', () => { - expect(makeFriendlyBalance(new BigNumber('100002000234'), 7)).toEqual( - '100.002B' - ) - expect( - makeFriendlyBalance(new BigNumber('100002001230.02343267'), 7) - ).toEqual('100.002B') - expect(makeFriendlyBalance(new BigNumber('100102000432'), 7)).toEqual( - '100.102B' - ) - }) - - test('it returns > 999.9B for numbers in the trillions', () => { - expect(makeFriendlyBalance(new BigNumber('100932423412399000'), 7)).toEqual( - '> 999.9B' - ) - }) -}) diff --git a/utils/noop.js b/utils/noop.js deleted file mode 100644 index ead516c97..000000000 --- a/utils/noop.js +++ /dev/null @@ -1 +0,0 @@ -export default () => {} diff --git a/utils/truncateAddress/index.test.js b/utils/truncateAddress/index.test.js deleted file mode 100644 index be8607e5a..000000000 --- a/utils/truncateAddress/index.test.js +++ /dev/null @@ -1,16 +0,0 @@ -import truncateAddress from '.' - -describe('truncateAddress', () => { - test('it truncates the address to 12 characters', () => { - const truncated = truncateAddress( - 't1mbk7q6gm4rjlndfqw6f2vkfgqotres3fgicb2uq' - ) - expect(truncated).toBe('t1mbk7 ... icb2uq') - expect(truncated.split(' ... ').join('').trim().length).toBe(12) - }) - - test('it returns entire address is 9 characters or shorter', () => { - const truncated = truncateAddress('t12345678') - expect(truncated).toBe('t12345678') - }) -}) diff --git a/utils/truncateAddress/index.ts b/utils/truncateAddress/index.ts deleted file mode 100644 index 2d16e85c8..000000000 --- a/utils/truncateAddress/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default function truncateAddress(address: string): string { - if (address.length <= 12) return address - - return `${address.slice(0, 6)} ... ${address.slice(-6)}` -} diff --git a/utils/urlParams/index.test.ts b/utils/urlParams/index.test.ts deleted file mode 100644 index 8db83f896..000000000 --- a/utils/urlParams/index.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { generateRouteWithRequiredUrlParams } from '.' -import { PAGE } from '../../constants' - -describe('generateRouteWithRequiredUrlParams', () => { - test('it does not change the required params when generating navigation urls', () => { - const route = generateRouteWithRequiredUrlParams({ - existingQParams: { network: 't' }, - pageUrl: PAGE.MSIG_HOME - }) - - expect(route.includes(PAGE.MSIG_HOME)).toBe(true) - expect(route.includes('?network=t')).toBe(true) - }) - - test('it allows for adding new query params', () => { - const route = generateRouteWithRequiredUrlParams({ - existingQParams: { network: 't' }, - pageUrl: PAGE.MSIG_HOME, - newQueryParams: { test: 'value' } - }) - - expect(route.includes(PAGE.MSIG_HOME)).toBe(true) - expect(route.includes('network=t')).toBe(true) - expect(route.includes('test=value')).toBe(true) - - const route2 = generateRouteWithRequiredUrlParams({ - existingQParams: { network: 'f' }, - pageUrl: PAGE.MSIG_HOME, - newQueryParams: { test: 'value' } - }) - - expect(route2.includes(PAGE.MSIG_HOME)).toBe(true) - expect(route2.includes('network=f')).toBe(true) - expect(route2.includes('test=value')).toBe(true) - }) - - test('it allows for adding computed paths', () => { - const route = generateRouteWithRequiredUrlParams({ - existingQParams: { network: 't' }, - pageUrl: PAGE.MSIG_HOME, - urlPathExtension: ['extension'] - }) - - expect(route.includes(PAGE.MSIG_HOME)).toBe(true) - expect(route.includes('network=t')).toBe(true) - expect(route.includes('/extension')).toBe(true) - - const route2 = generateRouteWithRequiredUrlParams({ - existingQParams: {}, - pageUrl: PAGE.MSIG_HOME, - urlPathExtension: ['extension'] - }) - - expect(route2.includes(PAGE.MSIG_HOME)).toBe(true) - expect(route2.includes('?')).toBe(false) - expect(route2.includes('/extension')).toBe(true) - }) - - test('it deletes not required q params with the flag', () => { - const route = generateRouteWithRequiredUrlParams({ - existingQParams: { network: 't', param2: 'kobe' }, - pageUrl: PAGE.MSIG_HOME, - urlPathExtension: ['extension', 'second-extension'], - newQueryParams: { - test: 'value', - test2: 'thingy' - }, - maintainQueryParams: false - }) - - expect(route.includes(PAGE.MSIG_HOME)).toBe(true) - expect(route.includes('network=t')).toBe(false) - expect(route.includes('param2=kobe')).toBe(false) - expect(route.includes('/extension/second-extension')).toBe(true) - expect(route.includes('test=value')).toBe(true) - expect(route.includes('test2=thingy')).toBe(true) - }) - - test('it works for the most complex cases', () => { - const route = generateRouteWithRequiredUrlParams({ - existingQParams: { network: 't', param2: 'kobe' }, - pageUrl: PAGE.MSIG_HOME, - maintainQueryParams: true, - urlPathExtension: ['extension', 'second-extension'], - newQueryParams: { - test: 'value', - test2: 'thingy' - } - }) - - expect(route.includes(PAGE.MSIG_HOME)).toBe(true) - expect(route.includes('network=t')).toBe(true) - expect(route.includes('param2=kobe')).toBe(true) - expect(route.includes('/extension/second-extension')).toBe(true) - }) - - test('it does not prepend a query question mark if no params are present', () => { - const route = generateRouteWithRequiredUrlParams({ - pageUrl: PAGE.LANDING, - existingQParams: {} - }) - expect(route).toBe('/') - }) -}) diff --git a/utils/urlParams/index.ts b/utils/urlParams/index.ts deleted file mode 100644 index 013e47abe..000000000 --- a/utils/urlParams/index.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { NextRouter } from 'next/router' -import { PAGE } from '../../constants' - -const requiredUrlParamsWithDefaults = {} - -interface NavigationOptions { - existingQParams: Record - pageUrl: PAGE - urlPathExtension?: string[] - newQueryParams?: Record - maintainQueryParams?: boolean -} - -export const combineExistingNewAndRequiredQueryParams = ( - existingQParams: Record, - newQParams?: Record, - maintainQParams?: boolean -): URLSearchParams => { - const searchParams = new URLSearchParams( - existingQParams as Record - ) - - // delete q params if the maintainQParams flag is not set - if (!maintainQParams) { - for (const [key] of [...searchParams.entries()]) { - if (!requiredUrlParamsWithDefaults[key]) { - searchParams.delete(key) - } - } - } - - // add new query params - if (newQParams) { - for (const param in newQParams) { - if (newQParams[param]) searchParams.set(param, newQParams[param]) - } - } - - // patch required q params if not present - for (const param in requiredUrlParamsWithDefaults) { - if (!searchParams.get(param)) { - searchParams.set(param, requiredUrlParamsWithDefaults[param]) - } - } - - return searchParams -} - -export const generateRouteWithRequiredUrlParams = ( - opts: NavigationOptions -): string => { - let maintain: boolean = true - // default to maintain - if (typeof opts?.maintainQueryParams !== 'undefined') - maintain = opts.maintainQueryParams - - const newParams = combineExistingNewAndRequiredQueryParams( - opts.existingQParams, - opts?.newQueryParams, - maintain - ) - - if (opts?.urlPathExtension) { - let route = `${opts.pageUrl}/${opts.urlPathExtension.join('/')}` - - if (newParams.toString().length > 0) { - route += `?${newParams.toString()}` - } - - return route - } - - if (!newParams.toString()) { - return opts.pageUrl - } - - return `${opts.pageUrl}?${newParams.toString()}` -} - -// maintains the required query params while navigating to pageUrl -export const navigate = ( - router: NextRouter, - opts: Omit -): void => { - router.push( - generateRouteWithRequiredUrlParams({ - ...opts, - existingQParams: {} - }) - ) -} - -export const resetWallet = () => { - // a full page reload will reset the wallet - window?.location?.reload() -} diff --git a/utils/useReset/index.test.jsx b/utils/useReset/index.test.jsx deleted file mode 100644 index 2b8982e6f..000000000 --- a/utils/useReset/index.test.jsx +++ /dev/null @@ -1,48 +0,0 @@ -import { renderHook, act } from '@testing-library/react-hooks' -import useReset from '.' -import { - WalletProviderWrapper, - initialState as _walletProviderInitialState -} from '@glif/react-components' - -import { composeWalletProviderState } from '../../test-utils/composeMockAppTree/composeState' - -describe('useReset', () => { - test('it resets both the redux and walletprovider state when called', () => { - const statePreset = 'postOnboard' - const walletProviderInitialState = composeWalletProviderState( - _walletProviderInitialState, - statePreset - ) - - let walletProviderState = { ...walletProviderInitialState } - - const cacheWalletProviderState = (state) => { - walletProviderState = { ...state } - return <> - } - - const Tree = ({ children }) => ( - - {children} - - ) - - const { - result: { current } - } = renderHook(() => useReset(), { wrapper: Tree }) - - act(() => { - // this is the reset hook - current() - }) - - expect(JSON.stringify(walletProviderState)).toBe( - JSON.stringify(_walletProviderInitialState) - ) - }) -}) diff --git a/utils/useReset/index.ts b/utils/useReset/index.ts deleted file mode 100644 index 984663de4..000000000 --- a/utils/useReset/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { useCallback } from 'react' -import { useWalletProvider } from '@glif/react-components' - -export default function useReset() { - const { resetState } = useWalletProvider() - - return useCallback(() => { - resetState() - }, [resetState]) -}