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

189 lines
3.6 KiB
JavaScript
Raw Normal View History

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 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 const CardHolder = styled.div`
display: flex;
flex-direction: column;
`
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}
/>
)
}
export function ExistenceAvailability({
name,
target,
prefix = '',
suffix = '',
icon,
}) {
const response = useFetch(target, null, { metadata: true })
if (response.status !== 404 && response.status !== 200) {
throw new Error(`Homebrew: ${response.statusText}`)
}
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)
console.log(children)
function onClick() {
setShow(true)
}
return (
<>
{show ? (
nameList.map((name) => (
<ErrorBoundary>
<Suspense fallback={<BarLoader />}>{children(name)}</Suspense>
</ErrorBoundary>
))
) : (
<ShowAlternativesButton onClick={onClick}>
Show Alternatives
</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-30 23:27:28 +09:00
padding: 20px;
2019-07-31 00:26:49 +09:00
box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.1);
2019-07-30 23:27:28 +09:00
border-radius: 2px;
`
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;
}
`