1
0
mirror of https://github.com/uetchy/namae.git synced 2025-07-01 22:10:04 +09:00

107 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-02-25 15:13:15 +09:00
import fetch from 'cross-fetch';
2020-08-31 08:41:53 +09:00
import React from 'react';
import { useTranslation } from 'react-i18next';
import { MdDomain } from 'react-icons/md';
2021-02-25 15:13:15 +09:00
import useSWR from 'swr';
import { normalize } from '../../../util/text';
2020-08-31 08:41:53 +09:00
import { zones } from '../../../util/zones';
2021-02-25 15:13:15 +09:00
import { Card, DedicatedAvailability, Repeater } from '../core';
export interface DomainrResponse {
results: {
domain: string;
host: string;
subdomain: string;
zone: string;
path: string;
registerURL: string;
}[];
}
function fetcher(url: string) {
return fetch(url, {}).then((res) => res.json());
}
2019-07-31 13:11:00 +09:00
2020-08-20 00:57:33 +09:00
const DomainCard: React.FC<{ query: string }> = ({ query }) => {
2020-08-31 08:41:53 +09:00
const { t } = useTranslation();
2020-06-11 22:00:16 +09:00
2021-02-25 15:13:15 +09:00
const normalizedQuery = normalize(query, {
alphanumeric: false,
allowUnderscore: false,
});
const lowerCase = normalizedQuery.toLowerCase();
2019-07-31 13:11:00 +09:00
2020-02-08 18:10:45 +09:00
const domainHackSuggestions = zones
2020-02-09 09:07:28 +09:00
.map((zone) => new RegExp(`${zone}$`).exec(lowerCase.slice(1)))
2020-02-08 18:10:45 +09:00
.filter((s): s is RegExpExecArray => s !== null)
.map(
(m) =>
2020-02-09 09:07:28 +09:00
lowerCase.substring(0, m.index + 1) +
'.' +
2020-08-20 00:57:33 +09:00
lowerCase.substring(m.index + 1)
2020-08-31 08:41:53 +09:00
);
2020-02-08 18:10:45 +09:00
2021-02-25 15:13:15 +09:00
const { data } = useSWR<DomainrResponse>(
`/api/list/domain/${encodeURIComponent(query)}`,
fetcher
);
const cctldSuggestions =
data?.results
?.filter((res) => res.subdomain !== '' && res.path === '')
?.map((res) => res.domain) ?? [];
const names =
// use Set() to eliminate dupes
new Set([
...['com', 'org', 'app', 'io'].map((tld) => lowerCase + '.' + tld),
...domainHackSuggestions,
...cctldSuggestions,
]);
const moreNames = new Set([
2020-09-06 18:15:22 +09:00
`${lowerCase}app.com`,
`get${lowerCase}.com`,
2021-02-25 15:13:15 +09:00
...[
'co',
'dev',
'sh',
'cloud',
'tools',
'build',
'run',
'ai',
'design',
'directory',
'guru',
'ninja',
'info',
'biz',
].map((tld) => lowerCase + '.' + tld),
]);
for (const name of moreNames) {
if (names.has(name)) {
moreNames.delete(name);
}
}
2019-08-06 00:45:18 +09:00
2019-07-31 13:11:00 +09:00
return (
2019-08-06 00:45:18 +09:00
<Card title={t('providers.domains')}>
2021-02-25 15:13:15 +09:00
<Repeater items={Array.from(names)} moreItems={Array.from(moreNames)}>
2019-08-06 00:45:18 +09:00
{(name) => (
<DedicatedAvailability
name={name}
2019-08-07 22:25:51 +09:00
message="Go to Domainr.com"
2019-08-06 00:45:18 +09:00
service="domain"
link={`https://domainr.com/?q=${name}`}
2020-02-06 13:16:30 +09:00
icon={<MdDomain />}
2019-08-06 00:45:18 +09:00
/>
)}
</Repeater>
2019-07-31 13:11:00 +09:00
</Card>
2020-08-31 08:41:53 +09:00
);
};
2019-09-01 01:28:24 +09:00
2020-08-31 08:41:53 +09:00
export default DomainCard;