diff --git a/packages/apps/src/initSettings.ts b/packages/apps/src/initSettings.ts index 86d6ca1ffc..3be57a16f1 100644 --- a/packages/apps/src/initSettings.ts +++ b/packages/apps/src/initSettings.ts @@ -6,24 +6,48 @@ import queryString from 'query-string'; import store from 'store'; import { createEndpoints } from '@polkadot/apps-config/settings'; import { registry } from '@polkadot/react-api'; +import { extractIpfsDetails } from '@polkadot/react-hooks/useIpfs'; import settings from '@polkadot/ui-settings'; -// we split here so that both these forms are allowed -// - http://localhost:3000/?rpc=wss://substrate-rpc.parity.io/#/explorer -// - http://localhost:3000/#/explorer?rpc=wss://substrate-rpc.parity.io -const urlOptions = queryString.parse(location.href.split('?')[1]); -const stored = store.get('settings') as Record || {}; +function getApiUrl (): string { + // we split here so that both these forms are allowed + // - http://localhost:3000/?rpc=wss://substrate-rpc.parity.io/#/explorer + // - http://localhost:3000/#/explorer?rpc=wss://substrate-rpc.parity.io + const urlOptions = queryString.parse(location.href.split('?')[1]); -if (Array.isArray(urlOptions.rpc)) { - throw new Error('Invalid WS endpoint specified'); + // if specified, this takes priority + if (urlOptions.rpc) { + if (Array.isArray(urlOptions.rpc)) { + throw new Error('Invalid WS endpoint specified'); + } + + return urlOptions.rpc.split('#')[0]; // https://polkadot.js.org/apps/?rpc=ws://127.0.0.1:9944#/explorer; + } + + const endpoints = createEndpoints((): T => ('' as unknown as T)); + const { ipnsChain } = extractIpfsDetails(); + + // check against ipns domains (could be expanded to others) + if (ipnsChain) { + const option = endpoints.find(({ dnslink }) => dnslink === ipnsChain); + + if (option) { + return option.value as string; + } + } + + const stored = store.get('settings') as Record || {}; + const fallbackUrl = endpoints.find(({ value }) => !!value); + + // via settings, or the default chain + return [stored.apiUrl, process.env.WS_URL].includes(settings.apiUrl) + ? settings.apiUrl // keep as-is + : fallbackUrl + ? fallbackUrl.value as string // grab the fallback + : 'ws://127.0.0.1:9944'; // nothing found, go local } -const fallbackUrl = createEndpoints((): T => ('' as unknown as T)).find(({ value }) => !!value) || { value: 'ws://127.0.0.1:9944' }; -const apiUrl = urlOptions.rpc // we have a supplied value - ? urlOptions.rpc.split('#')[0] // https://polkadot.js.org/apps/?rpc=ws://127.0.0.1:9944#/explorer - : [stored.apiUrl, process.env.WS_URL].includes(settings.apiUrl) // overridden, or stored - ? settings.apiUrl // keep as-is - : fallbackUrl.value as string; // grab the fallback +const apiUrl = getApiUrl(); // set the default as retrieved here settings.set({ apiUrl }); diff --git a/packages/page-accounts/src/Accounts/index.tsx b/packages/page-accounts/src/Accounts/index.tsx index 01d514645b..1d9d2c0395 100644 --- a/packages/page-accounts/src/Accounts/index.tsx +++ b/packages/page-accounts/src/Accounts/index.tsx @@ -11,12 +11,11 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react'; import styled from 'styled-components'; import keyring from '@polkadot/ui-keyring'; import { getLedger, isLedger } from '@polkadot/react-api'; -import { useApi, useAccounts, useFavorites, useToggle } from '@polkadot/react-hooks'; +import { useApi, useAccounts, useFavorites, useIpfs, useToggle } from '@polkadot/react-hooks'; import { FormatBalance } from '@polkadot/react-query'; import { Button, Input, Table } from '@polkadot/react-components'; import { useTranslation } from '../translate'; -import useIsIpfs from './useIsIpfs'; import CreateModal from './modals/Create'; import ImportModal from './modals/Import'; import Multisig from './modals/MultisigCreate'; @@ -96,7 +95,7 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen const { t } = useTranslation(); const { api } = useApi(); const { allAccounts } = useAccounts(); - const isIpfs = useIsIpfs(); + const { isIpfs } = useIpfs(); const [isCreateOpen, toggleCreate] = useToggle(); const [isImportOpen, toggleImport] = useToggle(); const [isMultisigOpen, toggleMultisig] = useToggle(); diff --git a/packages/page-accounts/src/Accounts/useIsIpfs.ts b/packages/page-accounts/src/Accounts/useIsIpfs.ts deleted file mode 100644 index be0bef8638..0000000000 --- a/packages/page-accounts/src/Accounts/useIsIpfs.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2020 @polkadot/app-accounts authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { useState } from 'react'; - -const KNOWN = ['/ipfs/', '/ipns/']; - -export default function useIsIpfs (): boolean { - const [isIpfs] = useState(KNOWN.some((part) => window.location.href.includes(part))); - - return isIpfs; -} diff --git a/packages/react-hooks/src/index.ts b/packages/react-hooks/src/index.ts index c631ac5030..5a4a0cbd19 100644 --- a/packages/react-hooks/src/index.ts +++ b/packages/react-hooks/src/index.ts @@ -13,6 +13,7 @@ export { default as useFormField } from './useFormField'; export { default as useDebounce } from './useDebounce'; export { default as useFavorites } from './useFavorites'; export { default as useIncrement } from './useIncrement'; +export { default as useIpfs } from './useIpfs'; export { default as useIsMountedRef } from './useIsMountedRef'; export { default as useMembers } from './useMembers'; export { default as useModal } from './useModal'; diff --git a/packages/react-hooks/src/useIpfs.ts b/packages/react-hooks/src/useIpfs.ts new file mode 100644 index 0000000000..3ee96f16b9 --- /dev/null +++ b/packages/react-hooks/src/useIpfs.ts @@ -0,0 +1,68 @@ +// Copyright 2017-2020 @polkadot/app-accounts authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { useState } from 'react'; + +const KNOWN = ['ipfs', 'ipns']; +const SECTIONS = KNOWN.map((part) => `/${part}/`); + +interface State { + ipnsChain: string | null; + ipnsDomain: string | null; + ipfsHash: string | null; + ipfsPath: string | null; + isIpfs: boolean; + isIpns: boolean; +} + +export function extractIpfsDetails (): State { + // get url and check to see if we are ipfs/ipns + const [url] = window.location.href.split('#'); + const isIpfs = SECTIONS.some((part) => url.includes(part)); + const isIpns = url.includes(SECTIONS[1]); + + // individual sections, with the index of the ipfs portion + const urlParts = url.split('/'); + const index = urlParts.indexOf(isIpns ? KNOWN[1] : KNOWN[0]); + + // the parts of the path for ipfs re-construction + let ipfsHash: string | null = null; + let ipfsPath: string | null = null; + let ipnsChain: string | null = null; + let ipnsDomain: string | null = null; + + // setup the ipfs part and dnslink domain (if available) + if (index !== -1) { + ipfsPath = urlParts.slice(0, index + 1).join('/'); + + if (isIpns) { + const dnsLink = urlParts[index + 1]; + const linkParts = dnsLink.split('.'); + + if (linkParts.length > 2) { + ipnsChain = linkParts[0]; + ipnsDomain = linkParts.slice(1).join('.'); + } else { + ipnsDomain = dnsLink; + } + } else { + ipfsHash = urlParts[index + 1]; + } + } + + return { + ipfsHash, + ipfsPath, + ipnsChain, + ipnsDomain, + isIpfs, + isIpns + }; +} + +export default function useIpfs (): State { + const [state] = useState(extractIpfsDetails()); + + return state; +}