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

285 lines
5.7 KiB
JavaScript
Raw Normal View History

2019-08-07 13:38:37 +09:00
import React, { useState, useEffect, Suspense } from 'react'
2019-07-31 13:11:00 +09:00
import styled from 'styled-components'
2019-08-05 22:59:39 +09:00
import useFetch from 'fetch-suspense'
2019-08-07 13:38:37 +09:00
import { Tooltip } from 'react-tippy'
import 'react-tippy/dist/tippy.css'
2019-08-05 22:59:39 +09:00
import BarLoader from 'react-spinners/BarLoader'
import { GoInfo } from 'react-icons/go'
2019-08-07 13:38:37 +09:00
2019-08-05 22:59:39 +09:00
import { ExternalLink } from './Links'
2019-08-07 13:38:37 +09:00
import { mobile } from '../util/css'
const COLORS = {
available: '#6e00ff',
unavailable: 'darkgrey',
error: '#ff388b',
}
2019-08-05 22:59:39 +09:00
2019-08-06 00:45:18 +09:00
export function Card({ title, children }) {
return (
<CardContainer>
<CardTitle>{title}</CardTitle>
<CardContent>
<ErrorBoundary>
<Suspense
fallback={
<ResultContainer>
<BarLoader />
</ResultContainer>
}>
{children}
</Suspense>
</ErrorBoundary>
</CardContent>
</CardContainer>
)
}
export function Repeater({ items = [], moreItems = [], children }) {
const [revealAlternatives, setRevealAlternatives] = useState(false)
function onClick() {
setRevealAlternatives(true)
}
2019-08-07 13:38:37 +09:00
useEffect(() => {
setRevealAlternatives(false)
}, [items, moreItems])
2019-08-06 00:45:18 +09:00
return (
<>
{items.map((name) => (
<CellError key={name}>{children(name)}</CellError>
))}
{revealAlternatives
? moreItems.map((name) => (
<CellError key={name}>{children(name)}</CellError>
))
: null}
{moreItems.length > 0 && !revealAlternatives ? (
<Button onClick={onClick}>show more</Button>
) : null}
</>
)
}
2019-08-05 22:59:39 +09:00
export function DedicatedAvailability({
name,
2019-08-06 00:45:18 +09:00
message = '',
2019-08-07 22:25:51 +09:00
messageIfTaken = undefined,
2019-08-05 22:59:39 +09:00
service,
link,
2019-08-07 22:25:51 +09:00
linkIfTaken = undefined,
2019-08-05 22:59:39 +09:00
prefix = '',
suffix = '',
icon,
}) {
const response = useFetch(`/availability/${service}/${name}`)
if (response.error) {
throw new Error(`${service}: ${response.error}`)
}
return (
2019-08-06 00:45:18 +09:00
<Result
title={name}
2019-08-07 22:25:51 +09:00
message={response.availability ? message : messageIfTaken || message}
link={response.availability ? link : linkIfTaken || link}
2019-08-06 00:45:18 +09:00
icon={icon}
2019-08-07 13:38:37 +09:00
color={response.availability ? COLORS.available : COLORS.unavailable}
2019-08-05 22:59:39 +09:00
prefix={prefix}
suffix={suffix}
/>
)
}
export function ExistentialAvailability({
name,
2019-08-06 00:45:18 +09:00
message = '',
2019-08-07 22:25:51 +09:00
messageIfTaken = undefined,
2019-08-05 22:59:39 +09:00
target,
link,
2019-08-07 22:25:51 +09:00
linkIfTaken = undefined,
2019-08-05 22:59:39 +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 (
2019-08-06 00:45:18 +09:00
<Result
title={name}
2019-08-07 22:25:51 +09:00
message={availability ? message : messageIfTaken || message}
link={availability ? link : linkIfTaken || link}
2019-08-06 00:45:18 +09:00
icon={icon}
2019-08-07 13:38:37 +09:00
color={availability ? COLORS.available : COLORS.unavailable}
2019-08-05 22:59:39 +09:00
prefix={prefix}
suffix={suffix}
/>
)
}
2019-08-06 00:45:18 +09:00
export const Result = ({
2019-08-05 22:59:39 +09:00
title,
2019-08-06 00:45:18 +09:00
message = '',
2019-08-05 22:59:39 +09:00
link,
2019-08-06 00:45:18 +09:00
icon,
color = 'inherit',
2019-08-05 22:59:39 +09:00
prefix = '',
suffix = '',
2019-08-06 00:45:18 +09:00
}) => {
const content = (
<>
{prefix}
{title}
{suffix}
</>
2019-08-05 22:59:39 +09:00
)
2019-07-31 13:11:00 +09:00
return (
2019-08-06 00:45:18 +09:00
<ResultContainer>
<Tooltip
title={message}
position="bottom"
arrow={true}
animation="shift"
duration="200">
<ResultItem color={color}>
2019-08-07 13:38:37 +09:00
<ResultIcon>{icon}</ResultIcon>
2019-08-06 00:45:18 +09:00
<ResultName>
{link ? (
<ExternalLink href={link}>{content}</ExternalLink>
) : (
content
)}
</ResultName>
</ResultItem>
</Tooltip>
</ResultContainer>
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>
2019-08-07 13:38:37 +09:00
<ResultItem color={COLORS.error}>
<ResultIcon>
<GoInfo />
</ResultIcon>
2019-08-05 22:59:39 +09:00
<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
}
2019-08-06 00:45:18 +09:00
const CellError = ({ children }) => (
2019-08-05 22:59:39 +09:00
<ErrorBoundary>
<Suspense
fallback={
<ResultContainer>
<BarLoader />
</ResultContainer>
}>
{children}
</Suspense>
</ErrorBoundary>
)
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;
}
`
2019-08-06 00:45:18 +09:00
const CardContent = styled.div`
2019-08-01 00:58:44 +09:00
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`
display: flex;
align-items: center;
margin-top: 8px;
`
2019-08-07 13:38:37 +09:00
const ResultIcon = styled.div`
width: 1em;
`
2019-08-05 22:59:39 +09:00
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;
2019-08-07 13:38:37 +09:00
line-height: 1em;
2019-08-05 22:59:39 +09:00
font-family: monospace;
font-size: 1rem;
a {
text-decoration: none;
color: inherit;
}
`