2019-08-05 22:59:39 +09:00
|
|
|
import React, { useState, Suspense } from 'react'
|
2019-07-31 13:11:00 +09:00
|
|
|
import styled from 'styled-components'
|
|
|
|
|
|
|
|
import { mobile } from '../util/css'
|
|
|
|
|
2019-08-05 22:59:39 +09:00
|
|
|
import useFetch from 'fetch-suspense'
|
|
|
|
import BarLoader from 'react-spinners/BarLoader'
|
|
|
|
import { GoInfo } from 'react-icons/go'
|
|
|
|
import { Tooltip } from 'react-tippy'
|
|
|
|
import { ExternalLink } from './Links'
|
|
|
|
import 'react-tippy/dist/tippy.css'
|
|
|
|
|
|
|
|
export function DedicatedAvailability({
|
|
|
|
name,
|
|
|
|
service,
|
|
|
|
link,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) {
|
|
|
|
const response = useFetch(`/availability/${service}/${name}`)
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(`${service}: ${response.error}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AvailabilityResult
|
|
|
|
availability={response.availability}
|
|
|
|
name={name}
|
|
|
|
link={link}
|
|
|
|
prefix={prefix}
|
|
|
|
suffix={suffix}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ExistentialAvailability({
|
|
|
|
name,
|
|
|
|
target,
|
|
|
|
link,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) {
|
|
|
|
const response = useFetch(target, null, { metadata: true })
|
|
|
|
|
|
|
|
if (response.status !== 404 && response.status !== 200) {
|
|
|
|
throw new Error(`${name}: ${response.status}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const availability = response.status === 404
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AvailabilityResult
|
|
|
|
name={name}
|
|
|
|
availability={availability}
|
|
|
|
link={link}
|
|
|
|
prefix={prefix}
|
|
|
|
suffix={suffix}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function CustomSearchCard({
|
|
|
|
title,
|
|
|
|
query,
|
|
|
|
link,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
children,
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<CardContainer>
|
|
|
|
<CardTitle>{title}</CardTitle>
|
|
|
|
<CardList>
|
|
|
|
<ErrorHandler key={query}>{children(query)}</ErrorHandler>
|
|
|
|
</CardList>
|
|
|
|
</CardContainer>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:27:05 +09:00
|
|
|
export function Card({ title, nameList = [], alternativeList = [], children }) {
|
2019-07-31 18:41:44 +09:00
|
|
|
const [revealAlternatives, setRevealAlternatives] = useState(false)
|
2019-07-31 13:11:00 +09:00
|
|
|
|
|
|
|
function onClick() {
|
2019-07-31 18:41:44 +09:00
|
|
|
setRevealAlternatives(true)
|
2019-07-31 13:11:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-08-05 22:59:39 +09:00
|
|
|
<CardContainer>
|
2019-07-31 13:11:00 +09:00
|
|
|
<CardTitle>{title}</CardTitle>
|
2019-08-01 00:58:44 +09:00
|
|
|
<CardList>
|
2019-07-31 13:11:00 +09:00
|
|
|
{nameList.map((name) => (
|
2019-08-05 22:59:39 +09:00
|
|
|
<ErrorHandler key={name}>{children(name)}</ErrorHandler>
|
2019-07-31 13:11:00 +09:00
|
|
|
))}
|
2019-07-31 18:41:44 +09:00
|
|
|
{revealAlternatives &&
|
|
|
|
alternativeList.map((name) => (
|
2019-08-05 22:59:39 +09:00
|
|
|
<ErrorHandler key={name}>{children(name)}</ErrorHandler>
|
2019-07-31 18:41:44 +09:00
|
|
|
))}
|
2019-08-01 00:58:44 +09:00
|
|
|
{alternativeList.length > 0 && !revealAlternatives ? (
|
2019-08-05 22:59:39 +09:00
|
|
|
<Button onClick={onClick}>show more</Button>
|
2019-08-01 00:58:44 +09:00
|
|
|
) : null}
|
|
|
|
</CardList>
|
2019-08-05 22:59:39 +09:00
|
|
|
</CardContainer>
|
2019-07-31 13:11:00 +09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-05 22:59:39 +09:00
|
|
|
class ErrorBoundary extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = { hasError: false }
|
|
|
|
}
|
2019-08-01 13:21:23 +09:00
|
|
|
|
2019-08-05 22:59:39 +09:00
|
|
|
static getDerivedStateFromError(error) {
|
|
|
|
return { hasError: true, message: error.message }
|
|
|
|
}
|
2019-08-01 13:21:23 +09:00
|
|
|
|
2019-08-05 22:59:39 +09:00
|
|
|
render() {
|
|
|
|
if (this.state.hasError) {
|
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
title={this.state.message}
|
|
|
|
position="bottom"
|
|
|
|
arrow={true}
|
|
|
|
animation="shift"
|
|
|
|
duration="200">
|
|
|
|
<ResultContainer>
|
|
|
|
<ResultItem>
|
|
|
|
<GoInfo />
|
|
|
|
<ResultName>Error</ResultName>
|
|
|
|
</ResultItem>
|
|
|
|
</ResultContainer>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return this.props.children
|
2019-08-01 13:21:23 +09:00
|
|
|
}
|
2019-08-05 22:59:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const ErrorHandler = ({ children }) => (
|
|
|
|
<ErrorBoundary>
|
|
|
|
<Suspense
|
|
|
|
fallback={
|
|
|
|
<ResultContainer>
|
|
|
|
<BarLoader />
|
|
|
|
</ResultContainer>
|
|
|
|
}>
|
|
|
|
{children}
|
|
|
|
</Suspense>
|
|
|
|
</ErrorBoundary>
|
|
|
|
)
|
|
|
|
|
|
|
|
const AvailabilityResult = ({
|
|
|
|
name,
|
|
|
|
availability,
|
|
|
|
link,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) => (
|
|
|
|
<ResultContainer>
|
|
|
|
<ResultItem color={availability ? 'green' : 'red'}>
|
|
|
|
{icon}
|
|
|
|
<ResultName>
|
|
|
|
<ExternalLink href={link}>
|
|
|
|
{prefix}
|
|
|
|
{name}
|
|
|
|
{suffix}
|
|
|
|
</ExternalLink>
|
|
|
|
</ResultName>
|
|
|
|
</ResultItem>
|
|
|
|
</ResultContainer>
|
|
|
|
)
|
2019-08-01 13:21:23 +09:00
|
|
|
|
2019-08-05 22:59:39 +09:00
|
|
|
const CardContainer = styled.div`
|
2019-07-31 13:11:00 +09:00
|
|
|
padding: 40px;
|
|
|
|
|
|
|
|
${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-07-31 18:41:44 +09:00
|
|
|
const CardTitle = styled.div`
|
2019-08-01 01:48:55 +09:00
|
|
|
margin-bottom: 15px;
|
2019-08-02 04:23:21 +09:00
|
|
|
font-size: 1rem;
|
2019-07-31 18:41:44 +09:00
|
|
|
font-weight: bold;
|
2019-08-01 00:58:44 +09:00
|
|
|
|
|
|
|
${mobile} {
|
|
|
|
padding-left: 20px;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const CardList = styled.div`
|
|
|
|
border-radius: 2px;
|
|
|
|
|
|
|
|
${mobile} {
|
|
|
|
padding: 20px;
|
|
|
|
box-shadow: 0px 2px 10px 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;
|
|
|
|
}
|
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-07-31 13:11:00 +09:00
|
|
|
`
|
2019-08-05 22:59:39 +09:00
|
|
|
|
|
|
|
const ResultContainer = styled.div`
|
|
|
|
min-height: 1em;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
margin-top: 8px;
|
|
|
|
`
|
|
|
|
|
|
|
|
const ResultItem = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
align-items: flex-start;
|
|
|
|
word-break: break-all;
|
|
|
|
color: ${({ color }) => color};
|
|
|
|
`
|
|
|
|
|
|
|
|
const ResultName = styled.div`
|
|
|
|
margin-left: 6px;
|
|
|
|
font-family: monospace;
|
|
|
|
font-size: 1rem;
|
|
|
|
|
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
color: inherit;
|
|
|
|
}
|
|
|
|
`
|