-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add domain parser hook and domain condition (#5413)
* fix: add domain parser hook and domain condition * fix: spell check error icann * fix: add validate rule for domain field
- Loading branch information
1 parent
f415bcf
commit ac51cce
Showing
3 changed files
with
74 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -621,6 +621,7 @@ | |
"Vonage", | ||
"runtimes", | ||
"cafebabe", | ||
"Icann", | ||
"limitbar", | ||
], | ||
"flagWords": [], | ||
|
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 }; | ||
} |