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

389 lines
8.2 KiB
TypeScript
Raw Normal View History

2019-09-17 14:30:26 +09:00
import React, {useState, useEffect, Suspense} from 'react';
import styled from 'styled-components';
import useFetch from 'fetch-suspense';
import {Tooltip} from 'react-tippy';
import 'react-tippy/dist/tippy.css';
import BarLoader from 'react-spinners/BarLoader';
import {GoInfo} from 'react-icons/go';
2020-02-05 17:46:23 +09:00
import {IoIosFlash} from 'react-icons/io';
2019-09-17 14:30:26 +09:00
import {useTranslation} from 'react-i18next';
2020-03-07 12:06:55 +09:00
import {OutboundLink} from 'react-ga';
2019-09-17 14:30:26 +09:00
2020-03-07 12:06:55 +09:00
import {sendError, sendExpandEvent} from '../../util/analytics';
2019-09-17 14:30:26 +09:00
import {mobile} from '../../util/css';
2019-08-07 13:38:37 +09:00
2020-03-26 15:34:34 +09:00
export const COLORS = {
2019-08-07 13:38:37 +09:00
available: '#6e00ff',
unavailable: 'darkgrey',
error: '#ff388b',
2019-09-17 14:30:26 +09:00
};
2019-08-05 22:59:39 +09:00
2019-09-17 14:30:26 +09:00
export const Card: React.FC<{title: string}> = ({title, children}) => {
2019-08-06 00:45:18 +09:00
return (
<CardContainer>
<CardTitle>{title}</CardTitle>
<CardContent>
2019-09-24 13:55:07 +09:00
<ErrorHandler>{children}</ErrorHandler>
2019-08-06 00:45:18 +09:00
</CardContent>
</CardContainer>
2019-09-17 14:30:26 +09:00
);
};
2019-08-06 00:45:18 +09:00
2019-09-01 01:28:24 +09:00
export const Repeater: React.FC<{
2019-09-17 14:30:26 +09:00
items: string[];
moreItems?: string[];
children: (name: string) => React.ReactNode;
}> = ({items = [], moreItems = [], children}) => {
const [revealAlternatives, setRevealAlternatives] = useState(false);
const {t} = useTranslation();
2019-08-06 00:45:18 +09:00
function onClick() {
2020-03-05 22:09:12 +09:00
sendExpandEvent();
2019-09-17 14:30:26 +09:00
setRevealAlternatives(true);
2019-08-06 00:45:18 +09:00
}
2019-08-07 13:38:37 +09:00
useEffect(() => {
2019-09-17 14:30:26 +09:00
setRevealAlternatives(false);
}, [items, moreItems]);
2019-08-07 13:38:37 +09:00
2019-08-06 00:45:18 +09:00
return (
<>
{items.map((name) => (
2019-09-24 13:55:07 +09:00
<ErrorHandler key={name}>{children(name)}</ErrorHandler>
2019-08-06 00:45:18 +09:00
))}
{revealAlternatives
? moreItems.map((name) => (
2019-09-24 13:55:07 +09:00
<ErrorHandler key={name}>{children(name)}</ErrorHandler>
2019-08-06 00:45:18 +09:00
))
: null}
{moreItems.length > 0 && !revealAlternatives ? (
2019-08-27 03:22:47 +09:00
<Button onClick={onClick}>{t('showMore')}</Button>
2019-08-06 00:45:18 +09:00
) : null}
</>
2019-09-17 14:30:26 +09:00
);
};
2019-08-06 00:45:18 +09:00
2019-09-01 01:28:24 +09:00
interface Response {
2019-09-17 14:30:26 +09:00
error?: string;
availability: boolean;
2019-09-01 01:28:24 +09:00
}
class APIError extends Error {
constructor(message?: string) {
super(message);
Object.setPrototypeOf(this, APIError.prototype);
}
}
class NotFoundError extends Error {
constructor(message?: string) {
super(message);
Object.setPrototypeOf(this, NotFoundError.prototype);
}
}
2019-09-01 01:28:24 +09:00
export const DedicatedAvailability: React.FC<{
2019-09-17 14:30:26 +09:00
name: string;
query?: string;
message?: string;
messageIfTaken?: string;
service: string;
link: string;
linkIfTaken?: string;
prefix?: string;
suffix?: string;
icon: React.ReactNode;
2019-09-01 01:28:24 +09:00
}> = ({
2019-08-05 22:59:39 +09:00
name,
2019-08-27 03:15:35 +09:00
query = undefined,
2019-08-06 00:45:18 +09:00
message = '',
2019-08-07 22:25:51 +09:00
messageIfTaken = undefined,
2019-08-05 22:59:39 +09:00
service,
link,
2019-08-07 22:25:51 +09:00
linkIfTaken = undefined,
2019-08-05 22:59:39 +09:00
prefix = '',
suffix = '',
icon,
2019-09-01 01:28:24 +09:00
}) => {
2019-08-27 03:15:35 +09:00
const response = useFetch(
2019-09-17 14:30:26 +09:00
`/availability/${service}/${encodeURIComponent(query || name)}`,
) as Response;
2019-08-05 22:59:39 +09:00
if (response.error) {
throw new APIError(`${service}: ${response.error}`);
2019-08-05 22:59:39 +09:00
}
return (
2019-08-06 00:45:18 +09:00
<Result
title={name}
2019-08-07 22:25:51 +09:00
message={response.availability ? message : messageIfTaken || message}
link={response.availability ? link : linkIfTaken || link}
2019-08-06 00:45:18 +09:00
icon={icon}
2019-08-05 22:59:39 +09:00
prefix={prefix}
suffix={suffix}
2020-02-05 17:46:23 +09:00
availability={response.availability}
2019-08-05 22:59:39 +09:00
/>
2019-09-17 14:30:26 +09:00
);
};
2019-08-05 22:59:39 +09:00
2019-09-01 01:28:24 +09:00
export const ExistentialAvailability: React.FC<{
2019-09-17 14:30:26 +09:00
name: string;
target: string;
message?: string;
messageIfTaken?: string;
link: string;
linkIfTaken?: string;
prefix?: string;
suffix?: string;
icon: React.ReactNode;
2019-09-01 01:28:24 +09:00
}> = ({
2019-08-05 22:59:39 +09:00
name,
2019-08-06 00:45:18 +09:00
message = '',
2019-08-07 22:25:51 +09:00
messageIfTaken = undefined,
2019-08-05 22:59:39 +09:00
target,
link,
2019-08-07 22:25:51 +09:00
linkIfTaken = undefined,
2019-08-05 22:59:39 +09:00
prefix = '',
suffix = '',
icon,
2019-09-01 01:28:24 +09:00
}) => {
2019-09-17 14:30:26 +09:00
const response = useFetch(target, undefined, {metadata: true});
2019-08-05 22:59:39 +09:00
if (response.status !== 404 && response.status !== 200) {
throw new NotFoundError(`${name}: ${response.status}`);
2019-08-05 22:59:39 +09:00
}
2019-09-17 14:30:26 +09:00
const availability = response.status === 404;
2019-08-05 22:59:39 +09:00
return (
2019-08-06 00:45:18 +09:00
<Result
title={name}
2019-08-07 22:25:51 +09:00
message={availability ? message : messageIfTaken || message}
link={availability ? link : linkIfTaken || link}
2019-08-06 00:45:18 +09:00
icon={icon}
2019-08-05 22:59:39 +09:00
prefix={prefix}
suffix={suffix}
2020-02-05 17:46:23 +09:00
availability={availability}
2019-08-05 22:59:39 +09:00
/>
2019-09-17 14:30:26 +09:00
);
};
2019-08-05 22:59:39 +09:00
2019-09-01 01:28:24 +09:00
export const Result: React.FC<{
2019-09-17 14:30:26 +09:00
title: string;
message?: string;
link?: string;
icon: React.ReactNode;
prefix?: string;
suffix?: string;
2020-02-05 17:46:23 +09:00
availability?: boolean;
2019-09-01 01:28:24 +09:00
}> = ({
2019-08-05 22:59:39 +09:00
title,
2019-08-06 00:45:18 +09:00
message = '',
2019-08-05 22:59:39 +09:00
link,
2019-08-06 00:45:18 +09:00
icon,
2019-08-05 22:59:39 +09:00
prefix = '',
suffix = '',
2020-02-05 17:46:23 +09:00
availability,
2019-08-06 00:45:18 +09:00
}) => {
const content = (
<>
{prefix}
{title}
{suffix}
</>
2019-09-17 14:30:26 +09:00
);
2020-02-05 17:46:23 +09:00
const itemColor =
availability === undefined
? 'inherit'
: availability
? COLORS.available
: COLORS.unavailable;
2019-07-31 13:11:00 +09:00
return (
2020-02-08 19:17:02 +09:00
<ResultContainer>
<Tooltip
title={message}
position="bottom"
arrow={true}
animation="shift"
2020-03-26 15:34:34 +09:00
duration="200"
>
2020-02-05 17:46:23 +09:00
<ResultItem color={itemColor}>
2019-08-07 13:38:37 +09:00
<ResultIcon>{icon}</ResultIcon>
2019-08-06 00:45:18 +09:00
<ResultName>
{link ? (
2020-03-07 12:06:55 +09:00
<OutboundLink
to={link}
eventLabel={link.split('/')[2]}
2020-03-26 15:34:34 +09:00
target="_blank"
>
2020-03-07 12:06:55 +09:00
{content}
</OutboundLink>
2019-08-06 00:45:18 +09:00
) : (
content
)}
</ResultName>
2020-02-06 13:16:30 +09:00
{availability === true ? (
<AvailableIcon>
<IoIosFlash />{' '}
</AvailableIcon>
) : null}
2019-08-06 00:45:18 +09:00
</ResultItem>
2020-02-08 19:17:02 +09:00
</Tooltip>
</ResultContainer>
2019-09-17 14:30:26 +09:00
);
};
2019-07-31 13:11:00 +09:00
2019-09-24 13:55:07 +09:00
// 1. getDerivedStateFromError
// 2. render()
// 3. componentDidCatch() send errorInfo to Sentry
// 4. render(), now with eventId provided from Sentry
2019-09-01 01:28:24 +09:00
class ErrorBoundary extends React.Component<
{},
2019-09-24 13:55:07 +09:00
{hasError: boolean; message: string; eventId?: string}
2019-09-01 01:28:24 +09:00
> {
constructor(props: {}) {
2019-09-17 14:30:26 +09:00
super(props);
2019-09-24 13:55:07 +09:00
this.state = {hasError: false, message: '', eventId: undefined};
2019-08-05 22:59:39 +09:00
}
2019-08-01 13:21:23 +09:00
2019-09-24 13:55:07 +09:00
// used in SSR
2019-09-01 01:28:24 +09:00
static getDerivedStateFromError(error: Error) {
2019-09-17 14:30:26 +09:00
return {hasError: true, message: error.message};
2019-08-05 22:59:39 +09:00
}
2019-08-01 13:21:23 +09:00
2019-12-24 01:57:07 +09:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2019-09-24 13:55:07 +09:00
componentDidCatch(error: Error, errorInfo: any) {
if (error instanceof APIError || error instanceof NotFoundError) {
return;
}
2019-09-24 13:55:07 +09:00
sendError(error, errorInfo).then((eventId) => {
this.setState({eventId});
});
}
2019-08-05 22:59:39 +09:00
render() {
if (this.state.hasError) {
return (
2020-02-08 19:17:02 +09:00
<ResultContainer>
<Tooltip
title={`${this.state.message}${
this.state.eventId ? ` (${this.state.eventId})` : ''
}`}
position="bottom"
arrow={true}
animation="shift"
2020-03-26 15:34:34 +09:00
duration="200"
>
2019-08-07 13:38:37 +09:00
<ResultItem color={COLORS.error}>
<ResultIcon>
<GoInfo />
</ResultIcon>
2019-08-05 22:59:39 +09:00
<ResultName>Error</ResultName>
</ResultItem>
2020-02-08 19:17:02 +09:00
</Tooltip>
</ResultContainer>
2019-09-17 14:30:26 +09:00
);
2019-08-05 22:59:39 +09:00
}
2019-09-17 14:30:26 +09:00
return this.props.children;
2019-08-01 13:21:23 +09:00
}
2019-08-05 22:59:39 +09:00
}
2019-09-24 13:55:07 +09:00
const ErrorHandler: React.FC = ({children}) => (
2019-08-05 22:59:39 +09:00
<ErrorBoundary>
<Suspense
fallback={
<ResultContainer>
<BarLoader />
</ResultContainer>
2020-03-26 15:34:34 +09:00
}
>
2019-08-05 22:59:39 +09:00
{children}
</Suspense>
</ErrorBoundary>
2019-09-17 14:30:26 +09:00
);
2019-08-05 22:59:39 +09:00
const CardContainer = styled.div`
2019-07-31 13:11:00 +09:00
padding: 40px;
2020-02-06 13:16:30 +09:00
font-size: 1rem;
line-height: 1rem;
2019-07-31 13:11:00 +09:00
${mobile} {
2019-08-01 13:21:23 +09:00
margin-bottom: 40px;
2019-08-01 00:58:44 +09:00
padding: 0px;
2019-07-31 13:11:00 +09:00
}
2019-09-17 14:30:26 +09:00
`;
2019-07-31 13:11:00 +09:00
2019-07-31 18:41:44 +09:00
const CardTitle = styled.div`
2019-08-01 01:48:55 +09:00
margin-bottom: 15px;
2020-02-06 13:16:30 +09:00
font-size: 1em;
2019-07-31 18:41:44 +09:00
font-weight: bold;
2019-08-01 00:58:44 +09:00
${mobile} {
2020-02-06 13:16:30 +09:00
padding: 0 20px;
margin-bottom: 20px;
font-size: 1.2rem;
font-weight: 600;
2019-08-01 00:58:44 +09:00
}
2019-09-17 14:30:26 +09:00
`;
2019-08-01 00:58:44 +09:00
2019-08-06 00:45:18 +09:00
const CardContent = styled.div`
2019-08-01 00:58:44 +09:00
border-radius: 2px;
${mobile} {
padding: 20px;
2020-02-06 13:16:30 +09:00
box-shadow: 0px 2px 20px rgba(0, 0, 0, 0.1);
2019-08-01 01:48:55 +09:00
background: white;
2019-08-01 00:58:44 +09:00
border-radius: 0;
2020-02-06 13:16:30 +09:00
font-size: 1.2em;
2019-08-01 00:58:44 +09:00
}
2019-09-17 14:30:26 +09:00
`;
2019-07-31 18:41:44 +09:00
2019-08-05 22:59:39 +09:00
const Button = styled.div`
2019-08-02 16:48:54 +09:00
margin-top: 5px;
2019-07-31 13:11:00 +09:00
display: inline-block;
padding: 5px 0;
border: none;
border-bottom: 1px dashed black;
cursor: pointer;
font-family: monospace;
2019-08-02 16:27:05 +09:00
font-size: 0.8em;
2019-09-17 14:30:26 +09:00
`;
2019-08-05 22:59:39 +09:00
const ResultContainer = styled.div`
2020-02-08 19:17:02 +09:00
display: flex;
align-items: center;
2020-02-06 13:16:30 +09:00
padding: 4px 0;
2019-09-17 14:30:26 +09:00
`;
2019-08-05 22:59:39 +09:00
2020-03-26 15:34:34 +09:00
export const ResultIcon = styled.div`
2019-08-07 13:38:37 +09:00
width: 1em;
2019-09-17 14:30:26 +09:00
`;
2019-08-07 13:38:37 +09:00
2020-03-26 15:34:34 +09:00
export const ResultItem = styled.div`
2019-08-05 22:59:39 +09:00
display: flex;
flex-direction: row;
align-items: flex-start;
word-break: break-all;
2019-09-17 14:30:26 +09:00
color: ${({color}) => color};
`;
2019-08-05 22:59:39 +09:00
2020-03-26 15:34:34 +09:00
export const ResultName = styled.div`
2019-08-05 22:59:39 +09:00
margin-left: 6px;
font-family: monospace;
a {
text-decoration: none;
color: inherit;
}
2019-09-17 14:30:26 +09:00
`;
2020-02-06 13:16:30 +09:00
2020-03-26 15:34:34 +09:00
export const AvailableIcon = styled.div`
2020-02-06 13:16:30 +09:00
margin-top: 2px;
margin-left: 3px;
padding: 0;
width: 15px;
text-align: center;
font-size: 13px;
height: 15px;
`;