2019-08-01 00:58:44 +09:00
|
|
|
import React, { Suspense } from 'react'
|
2019-07-31 13:11:00 +09:00
|
|
|
import styled from 'styled-components'
|
|
|
|
import useFetch from 'fetch-suspense'
|
|
|
|
import { BarLoader } from 'react-spinners'
|
2019-08-01 13:21:34 +09:00
|
|
|
import { GoInfo } from 'react-icons/go'
|
|
|
|
import { Tooltip } from 'react-tippy'
|
|
|
|
import 'react-tippy/dist/tippy.css'
|
2019-07-31 13:11:00 +09:00
|
|
|
|
2019-07-31 18:54:28 +09:00
|
|
|
import { ExternalLink } from './Links'
|
|
|
|
|
2019-08-01 00:58:44 +09:00
|
|
|
class ErrorBoundary extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = { hasError: false }
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDerivedStateFromError(error) {
|
|
|
|
return { hasError: true, message: error.message }
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch(error, info) {}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.hasError) {
|
2019-08-01 13:21:34 +09:00
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
title={this.state.message}
|
|
|
|
position="bottom"
|
|
|
|
arrow={true}
|
|
|
|
animation="shift"
|
|
|
|
duration="200">
|
|
|
|
<Container>
|
|
|
|
<Cell>
|
|
|
|
<GoInfo />
|
|
|
|
<Name>Error</Name>
|
|
|
|
</Cell>
|
|
|
|
</Container>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
2019-08-01 00:58:44 +09:00
|
|
|
}
|
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Result = ({
|
2019-07-31 13:11:00 +09:00
|
|
|
name,
|
|
|
|
availability,
|
2019-07-31 13:40:16 +09:00
|
|
|
link,
|
2019-07-31 13:11:00 +09:00
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
2019-08-01 00:58:44 +09:00
|
|
|
}) => (
|
|
|
|
<Container>
|
|
|
|
<Cell availability={availability}>
|
2019-07-31 13:11:00 +09:00
|
|
|
{icon}
|
2019-08-01 00:58:44 +09:00
|
|
|
<Name>
|
2019-07-31 18:54:28 +09:00
|
|
|
<ExternalLink href={link}>
|
2019-07-31 13:11:00 +09:00
|
|
|
{prefix}
|
2019-08-01 00:58:44 +09:00
|
|
|
{name}
|
2019-07-31 13:11:00 +09:00
|
|
|
{suffix}
|
2019-07-31 18:54:28 +09:00
|
|
|
</ExternalLink>
|
2019-08-01 00:58:44 +09:00
|
|
|
</Name>
|
|
|
|
</Cell>
|
|
|
|
</Container>
|
|
|
|
)
|
2019-07-31 13:11:00 +09:00
|
|
|
|
2019-08-01 00:58:44 +09:00
|
|
|
const Fallback = () => (
|
|
|
|
<Container>
|
2019-07-31 13:11:00 +09:00
|
|
|
<BarLoader />
|
2019-08-01 00:58:44 +09:00
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
|
|
|
|
export const AvailabilityContainer = ({ children }) => (
|
|
|
|
<ErrorBoundary>
|
|
|
|
<Suspense fallback={<Fallback />}>{children}</Suspense>
|
|
|
|
</ErrorBoundary>
|
2019-07-31 13:11:00 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
export function DedicatedAvailability({
|
|
|
|
name,
|
2019-07-31 13:40:16 +09:00
|
|
|
service,
|
|
|
|
link,
|
2019-07-31 13:11:00 +09:00
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) {
|
2019-07-31 13:40:16 +09:00
|
|
|
const response = useFetch(`/availability/${service}/${name}`)
|
2019-08-01 00:58:44 +09:00
|
|
|
|
2019-07-31 13:11:00 +09:00
|
|
|
if (response.error) {
|
2019-07-31 13:40:16 +09:00
|
|
|
throw new Error(`${service}: ${response.error}`)
|
2019-07-31 13:11:00 +09:00
|
|
|
}
|
2019-08-01 00:58:44 +09:00
|
|
|
|
2019-07-31 13:11:00 +09:00
|
|
|
return (
|
2019-08-01 00:58:44 +09:00
|
|
|
<Result
|
2019-07-31 13:11:00 +09:00
|
|
|
availability={response.availability}
|
|
|
|
name={name}
|
2019-07-31 13:40:16 +09:00
|
|
|
link={link}
|
2019-07-31 13:11:00 +09:00
|
|
|
prefix={prefix}
|
|
|
|
suffix={suffix}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ExistentialAvailability({
|
|
|
|
name,
|
|
|
|
target,
|
2019-07-31 13:57:37 +09:00
|
|
|
link,
|
2019-07-31 13:11:00 +09:00
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) {
|
|
|
|
const response = useFetch(target, null, { metadata: true })
|
2019-08-01 00:58:44 +09:00
|
|
|
|
2019-07-31 13:11:00 +09:00
|
|
|
if (response.status !== 404 && response.status !== 200) {
|
|
|
|
throw new Error(`${name}: ${response.status}`)
|
|
|
|
}
|
2019-08-01 00:58:44 +09:00
|
|
|
|
2019-07-31 13:11:00 +09:00
|
|
|
const availability = response.status === 404
|
2019-08-01 00:58:44 +09:00
|
|
|
|
2019-07-31 13:11:00 +09:00
|
|
|
return (
|
2019-08-01 00:58:44 +09:00
|
|
|
<Result
|
2019-07-31 13:11:00 +09:00
|
|
|
name={name}
|
|
|
|
availability={availability}
|
2019-07-31 13:57:37 +09:00
|
|
|
link={link}
|
2019-07-31 13:11:00 +09:00
|
|
|
prefix={prefix}
|
|
|
|
suffix={suffix}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:27:05 +09:00
|
|
|
export function CustomAvailability({
|
|
|
|
name,
|
|
|
|
target,
|
|
|
|
link,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
children,
|
|
|
|
}) {
|
|
|
|
const response = useFetch(target, null, { metadata: true })
|
|
|
|
const availability = children(response)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Result
|
|
|
|
name={name}
|
|
|
|
availability={availability}
|
|
|
|
link={link}
|
|
|
|
prefix={prefix}
|
|
|
|
suffix={suffix}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-01 00:58:44 +09:00
|
|
|
const Container = styled.div`
|
|
|
|
min-height: 1em;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
margin-top: 8px;
|
|
|
|
`
|
|
|
|
|
|
|
|
const Cell = styled.div`
|
2019-07-31 13:11:00 +09:00
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
align-items: flex-start;
|
|
|
|
word-break: break-all;
|
2019-08-01 00:58:44 +09:00
|
|
|
color: ${({ availability }) => (availability ? 'green' : 'red')};
|
2019-07-31 13:11:00 +09:00
|
|
|
`
|
|
|
|
|
2019-08-01 01:56:42 +09:00
|
|
|
const Name = styled.div`
|
2019-07-31 13:11:00 +09:00
|
|
|
margin-left: 6px;
|
|
|
|
font-family: monospace;
|
|
|
|
font-size: 1rem;
|
|
|
|
|
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
color: inherit;
|
|
|
|
}
|
|
|
|
`
|