stacker.news/components/hat.js

60 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-07-24 18:35:05 +00:00
import Badge from 'react-bootstrap/Badge'
import OverlayTrigger from 'react-bootstrap/OverlayTrigger'
import Tooltip from 'react-bootstrap/Tooltip'
2023-02-01 14:44:35 +00:00
import CowboyHatIcon from '../svgs/cowboy.svg'
2023-08-11 19:12:18 +00:00
import AnonIcon from '../svgs/spy-fill.svg'
import { numWithUnits } from '../lib/format'
2023-08-11 19:12:18 +00:00
import { ANON_USER_ID } from '../lib/constants'
2023-02-01 14:44:35 +00:00
2023-08-11 19:12:18 +00:00
export default function Hat ({ user, badge, className = 'ms-1', height = 16, width = 16 }) {
if (!user) return null
if (Number(user.id) === ANON_USER_ID) {
return (
<HatTooltip overlayText={badge ? 'anonymous' : 'posted anonymously'}>
{badge
? (
<Badge bg='grey-medium' className='ms-2 d-inline-flex align-items-center'>
<AnonIcon className={`${className} align-middle`} height={height} width={width} />
</Badge>)
: <span><AnonIcon className={`${className} align-middle`} height={height} width={width} /></span>}
</HatTooltip>
)
}
if (user.streak === null || user.hideCowboyHat) {
2023-02-01 14:44:35 +00:00
return null
}
2023-05-01 21:49:47 +00:00
const streak = user.streak
2023-02-01 14:44:35 +00:00
return (
<HatTooltip overlayText={streak
? `${numWithUnits(streak, { abbreviate: false, unitSingular: 'day', unitPlural: 'days' })}`
: 'new'}
>
2023-02-01 14:44:35 +00:00
{badge
? (
2023-07-24 18:35:05 +00:00
<Badge bg='grey-medium' className='ms-2 d-inline-flex align-items-center'>
2023-02-01 14:44:35 +00:00
<CowboyHatIcon className={className} height={height} width={width} />
2023-07-24 18:35:05 +00:00
<span className='ms-1 text-dark'>{streak || 'new'}</span>
2023-02-01 14:44:35 +00:00
</Badge>)
2023-07-24 18:35:05 +00:00
: <span><CowboyHatIcon className={className} height={height} width={width} /></span>}
2023-02-01 14:44:35 +00:00
</HatTooltip>
)
}
2023-08-10 22:33:47 +00:00
export function HatTooltip ({ children, overlayText, placement }) {
2023-02-01 14:44:35 +00:00
return (
<OverlayTrigger
placement={placement || 'bottom'}
overlay={
<Tooltip>
2023-02-02 19:47:09 +00:00
{overlayText}
2023-02-01 14:44:35 +00:00
</Tooltip>
}
trigger={['hover', 'focus']}
>
{children}
</OverlayTrigger>
)
}