-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'information-architecture' into nv-3455-settings-brandin…
…g-page
- Loading branch information
Showing
10 changed files
with
83 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
interface DomainInfo { | ||
domain: string; | ||
domainWithoutSuffix: string; | ||
hostname: string; | ||
isIcann: boolean; | ||
isIp: boolean; | ||
isPrivate: boolean; | ||
publicSuffix: string; | ||
subdomain: string; | ||
} | ||
|
||
function stripProtocol(url: string): string { | ||
return (url || '').replace(/(https?)?(:\/+)?/, ''); | ||
} | ||
|
||
function fallbackParser(url: string) { | ||
const parts = stripProtocol(url).split('.'); | ||
|
||
if (parts.length > 2) { | ||
const [subdomain, ...domainParts] = parts; | ||
|
||
return { | ||
subdomain: subdomain || '', | ||
domain: domainParts.join('.'), | ||
}; | ||
} | ||
|
||
return { | ||
subdomain: '', | ||
domain: url, | ||
}; | ||
} | ||
|
||
export function useDomainParser(): { parse: (url: string) => Partial<DomainInfo> } { | ||
const tldParser = useRef(null); | ||
|
||
useEffect(() => { | ||
import( | ||
/* webpackIgnore: true */ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line import/extensions | ||
'https://unpkg.com/tldts/dist/es6/index.js?module' | ||
) | ||
.then((mod) => (tldParser.current = mod)) | ||
.catch(() => (tldParser.current = null)); | ||
}, []); | ||
|
||
const parse = useCallback((url: string) => { | ||
url = stripProtocol(url); | ||
|
||
if (tldParser.current) { | ||
return (tldParser.current as any).parse(url, { allowPrivateDomains: true }) as DomainInfo; | ||
} | ||
|
||
return fallbackParser(url); | ||
}, []); | ||
|
||
return { parse }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters