2019-07-31 01:12:51 +09:00
|
|
|
import React, { Suspense, useState } from 'react'
|
2019-07-30 23:27:28 +09:00
|
|
|
import styled from 'styled-components'
|
2019-07-31 01:12:51 +09:00
|
|
|
import useFetch from 'fetch-suspense'
|
2019-07-30 23:27:28 +09:00
|
|
|
import { BarLoader } from 'react-spinners'
|
2019-07-31 04:42:54 +09:00
|
|
|
import { mobile } from '../util/css'
|
2019-07-30 23:27:28 +09:00
|
|
|
|
2019-07-31 00:18:58 +09:00
|
|
|
export function Card({ children }) {
|
|
|
|
return (
|
|
|
|
<CardWrapper>
|
|
|
|
<ErrorBoundary>
|
|
|
|
<Suspense fallback={<BarLoader />}>{children}</Suspense>
|
|
|
|
</ErrorBoundary>
|
|
|
|
</CardWrapper>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CardTitle = styled.div`
|
|
|
|
font-size: 0.8rem;
|
|
|
|
font-weight: bold;
|
|
|
|
margin-bottom: 15px;
|
|
|
|
`
|
|
|
|
|
|
|
|
export function AvailabilityCell({
|
|
|
|
name,
|
|
|
|
availability,
|
|
|
|
url,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<ItemContainer>
|
|
|
|
{icon}
|
|
|
|
<Item>
|
|
|
|
<a href={url} target="_blank" rel="noopener noreferrer">
|
|
|
|
{prefix}
|
|
|
|
{availability ? (
|
|
|
|
<span style={{ color: 'green' }}>{name}</span>
|
|
|
|
) : (
|
|
|
|
<span style={{ color: 'red' }}>{name}</span>
|
|
|
|
)}
|
|
|
|
{suffix}
|
|
|
|
</a>
|
|
|
|
</Item>
|
|
|
|
</ItemContainer>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-31 01:12:51 +09:00
|
|
|
export function DedicatedAvailability({
|
|
|
|
name,
|
|
|
|
provider,
|
|
|
|
url,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) {
|
|
|
|
const response = useFetch(`/availability/${provider}/${name}`)
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(`${provider}: ${response.error}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AvailabilityCell
|
|
|
|
availability={response.availability}
|
|
|
|
name={name}
|
|
|
|
url={url}
|
|
|
|
prefix={prefix}
|
|
|
|
suffix={suffix}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:22:31 +09:00
|
|
|
export function ExistentialAvailability({
|
2019-07-31 01:12:51 +09:00
|
|
|
name,
|
|
|
|
target,
|
|
|
|
prefix = '',
|
|
|
|
suffix = '',
|
|
|
|
icon,
|
|
|
|
}) {
|
|
|
|
const response = useFetch(target, null, { metadata: true })
|
|
|
|
|
|
|
|
if (response.status !== 404 && response.status !== 200) {
|
2019-07-31 01:43:12 +09:00
|
|
|
throw new Error(`${name}: ${response.status}`)
|
2019-07-31 01:12:51 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const availability = response.status === 404
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AvailabilityCell
|
|
|
|
name={name}
|
|
|
|
availability={availability}
|
|
|
|
url={`https://formulae.brew.sh/formula/${name}`}
|
|
|
|
prefix={prefix}
|
|
|
|
suffix={suffix}
|
|
|
|
icon={icon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Alternatives({ nameList, children }) {
|
|
|
|
const [show, setShow] = useState(false)
|
|
|
|
|
|
|
|
function onClick() {
|
|
|
|
setShow(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{show ? (
|
|
|
|
nameList.map((name) => (
|
|
|
|
<ErrorBoundary>
|
2019-07-31 04:25:24 +09:00
|
|
|
<Suspense
|
|
|
|
fallback={
|
|
|
|
<ItemContainer>
|
|
|
|
<BarLoader />
|
|
|
|
</ItemContainer>
|
|
|
|
}>
|
|
|
|
{children(name)}
|
|
|
|
</Suspense>
|
2019-07-31 01:12:51 +09:00
|
|
|
</ErrorBoundary>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<ShowAlternativesButton onClick={onClick}>
|
2019-07-31 10:36:54 +09:00
|
|
|
show alternatives
|
2019-07-31 01:12:51 +09:00
|
|
|
</ShowAlternativesButton>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const ShowAlternativesButton = styled.div`
|
|
|
|
display: inline-block;
|
|
|
|
margin-top: 10px;
|
|
|
|
padding: 5px 0;
|
|
|
|
border: none;
|
|
|
|
border-bottom: 1px dashed black;
|
|
|
|
cursor: pointer;
|
|
|
|
font-family: monospace;
|
|
|
|
font-size: 1rem;
|
|
|
|
`
|
|
|
|
|
2019-07-30 23:27:28 +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) {
|
|
|
|
return <h4>{this.state.message}</h4>
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:18:58 +09:00
|
|
|
const CardWrapper = styled.div`
|
2019-07-31 00:26:49 +09:00
|
|
|
margin-bottom: 20px;
|
2019-07-31 04:42:54 +09:00
|
|
|
padding: 40px;
|
2019-07-30 23:27:28 +09:00
|
|
|
border-radius: 2px;
|
2019-07-31 04:42:54 +09:00
|
|
|
|
|
|
|
${mobile} {
|
|
|
|
padding: 20px;
|
|
|
|
box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.1);
|
|
|
|
border-radius: 0;
|
|
|
|
}
|
2019-07-30 23:27:28 +09:00
|
|
|
`
|
|
|
|
|
2019-07-31 01:12:51 +09:00
|
|
|
const ItemContainer = styled.div`
|
2019-07-30 23:27:28 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
`
|