1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-19 13:30:32 +09:00
namae/web/src/components/Availability.js

105 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-07-31 13:11:00 +09:00
import React from 'react'
import styled from 'styled-components'
import useFetch from 'fetch-suspense'
import { BarLoader } from 'react-spinners'
function AvailabilityCell({
name,
availability,
2019-07-31 13:40:16 +09:00
link,
2019-07-31 13:11:00 +09:00
prefix = '',
suffix = '',
icon,
}) {
return (
<ItemContainer>
{icon}
<Item>
2019-07-31 13:40:16 +09:00
<a href={link} target="_blank" rel="noopener noreferrer">
2019-07-31 13:11:00 +09:00
{prefix}
{availability ? (
<span style={{ color: 'green' }}>{name}</span>
) : (
<span style={{ color: 'red' }}>{name}</span>
)}
{suffix}
</a>
</Item>
</ItemContainer>
)
}
export const Fallback = () => (
<ItemContainer>
<BarLoader />
</ItemContainer>
)
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-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
}
return (
<AvailabilityCell
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 })
if (response.status !== 404 && response.status !== 200) {
throw new Error(`${name}: ${response.status}`)
}
const availability = response.status === 404
return (
<AvailabilityCell
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}
/>
)
}
const ItemContainer = styled.div`
display: flex;
flex-direction: row;
align-items: flex-start;
margin-top: 8px;
word-break: break-all;
`
const Item = styled.span`
margin-left: 6px;
font-family: monospace;
font-size: 1rem;
a {
text-decoration: none;
color: inherit;
}
`