Allow for local IPFS gateway (#2925)
* Allow for local IPFS gateway * Trim ports
This commit is contained in:
parent
18f22ab47e
commit
63a3b6dd29
@ -11,6 +11,7 @@ interface LinkOption extends Option {
|
||||
function createDev (t: <T= string> (key: string, text: string, options: { ns: string }) => T): LinkOption[] {
|
||||
return [
|
||||
{
|
||||
dnslink: 'local',
|
||||
info: 'local',
|
||||
text: t<string>('rpc.local', 'Local Node (Own, 127.0.0.1:9944)', { ns: 'apps-config' }),
|
||||
value: 'ws://127.0.0.1:9944/'
|
||||
|
||||
@ -6,6 +6,8 @@ import { useState } from 'react';
|
||||
|
||||
const KNOWN = ['ipfs', 'ipns'];
|
||||
const SECTIONS = KNOWN.map((part) => `/${part}/`);
|
||||
const LOCAL_IPFS = '.ipfs.localhost';
|
||||
const LOCAL_IPNS = '.ipns.localhost';
|
||||
|
||||
interface State {
|
||||
ipnsChain: string | null;
|
||||
@ -16,9 +18,47 @@ interface State {
|
||||
isIpns: boolean;
|
||||
}
|
||||
|
||||
export function extractIpfsDetails (): State {
|
||||
// get url and check to see if we are ipfs/ipns
|
||||
const [url] = window.location.href.split('#');
|
||||
function extractLocalIpfs (url: string): State {
|
||||
const [,, _ipfsPath] = url.split('/');
|
||||
const ipfsPath = _ipfsPath.split(':')[0];
|
||||
|
||||
return {
|
||||
ipfsHash: ipfsPath.replace(LOCAL_IPFS, ''),
|
||||
ipfsPath,
|
||||
ipnsChain: null,
|
||||
ipnsDomain: null,
|
||||
isIpfs: true,
|
||||
isIpns: false
|
||||
};
|
||||
}
|
||||
|
||||
function extractLocalIpns (url: string): State {
|
||||
const [,, _ipfsPath] = url.split('/');
|
||||
const ipfsPath = _ipfsPath.split(':')[0];
|
||||
|
||||
const dnsLink = ipfsPath.replace(LOCAL_IPNS, '');
|
||||
const linkParts = dnsLink.split('.');
|
||||
let ipnsChain: string | null = null;
|
||||
let ipnsDomain: string | null = null;
|
||||
|
||||
if (linkParts.length > 2) {
|
||||
ipnsChain = linkParts[0];
|
||||
ipnsDomain = linkParts.slice(1).join('.');
|
||||
} else {
|
||||
ipnsDomain = dnsLink;
|
||||
}
|
||||
|
||||
return {
|
||||
ipfsHash: null,
|
||||
ipfsPath,
|
||||
ipnsChain,
|
||||
ipnsDomain,
|
||||
isIpfs: true,
|
||||
isIpns: true
|
||||
};
|
||||
}
|
||||
|
||||
function extractOther (url: string): State {
|
||||
const isIpfs = SECTIONS.some((part) => url.includes(part));
|
||||
const isIpns = url.includes(SECTIONS[1]);
|
||||
|
||||
@ -61,6 +101,17 @@ export function extractIpfsDetails (): State {
|
||||
};
|
||||
}
|
||||
|
||||
export function extractIpfsDetails (): State {
|
||||
// get url and check to see if we are ipfs/ipns
|
||||
const [url] = window.location.href.split('#');
|
||||
|
||||
return url.includes(LOCAL_IPFS)
|
||||
? extractLocalIpfs(url)
|
||||
: url.includes(LOCAL_IPNS)
|
||||
? extractLocalIpns(url)
|
||||
: extractOther(url);
|
||||
}
|
||||
|
||||
export default function useIpfs (): State {
|
||||
const [state] = useState(extractIpfsDetails());
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user