stacker.news/components/item.js

192 lines
6.9 KiB
JavaScript
Raw Normal View History

2021-04-14 23:56:29 +00:00
import Link from 'next/link'
2021-04-14 00:57:32 +00:00
import styles from './item.module.css'
2021-04-18 18:50:04 +00:00
import { timeSince } from '../lib/time'
2021-04-22 22:14:32 +00:00
import UpVote from './upvote'
2021-09-15 23:42:44 +00:00
import { useEffect, useRef, useState } from 'react'
2021-08-11 20:34:10 +00:00
import Countdown from './countdown'
import { NOFOLLOW_LIMIT } from '../lib/constants'
2022-01-10 18:53:26 +00:00
import Pin from '../svgs/pushpin-fill.svg'
2022-02-03 22:01:42 +00:00
import reactStringReplace from 'react-string-replace'
2022-07-18 21:24:28 +00:00
import Toc from './table-of-contents'
2022-07-30 13:25:46 +00:00
import PollIcon from '../svgs/bar-chart-horizontal-fill.svg'
import { Badge } from 'react-bootstrap'
import { newComments } from '../lib/new-comments'
2022-09-21 19:57:36 +00:00
import { useMe } from './me'
import DontLikeThis from './dont-link-this'
import Flag from '../svgs/flag-fill.svg'
2022-12-19 22:27:52 +00:00
import Share from './share'
2022-10-25 21:35:32 +00:00
import { abbrNum } from '../lib/format'
2022-02-03 22:01:42 +00:00
2022-07-21 22:55:05 +00:00
export function SearchTitle ({ title }) {
2022-02-03 22:29:48 +00:00
return reactStringReplace(title, /:high\[([^\]]+)\]/g, (match, i) => {
2022-02-03 22:01:42 +00:00
return <mark key={`mark-${match}`}>{match}</mark>
})
}
2021-04-14 00:57:32 +00:00
2022-04-19 18:32:39 +00:00
function FwdUser ({ user }) {
return (
<div className={styles.other}>
100% of tips are forwarded to{' '}
<Link href={`/${user.name}`} passHref>
<a>@{user.name}</a>
</Link>
</div>
)
}
2022-07-18 21:24:28 +00:00
export default function Item ({ item, rank, showFwdUser, toc, children }) {
2021-11-27 18:01:02 +00:00
const mine = item.mine
2021-08-11 20:13:10 +00:00
const editThreshold = new Date(item.createdAt).getTime() + 10 * 60000
const [canEdit, setCanEdit] =
useState(mine && (Date.now() < editThreshold))
2021-09-15 23:42:44 +00:00
const [wrap, setWrap] = useState(false)
const titleRef = useRef()
2022-09-21 19:57:36 +00:00
const me = useMe()
const [hasNewComments, setHasNewComments] = useState(false)
2021-09-15 23:42:44 +00:00
useEffect(() => {
setWrap(
Math.ceil(parseFloat(window.getComputedStyle(titleRef.current).lineHeight)) <
2021-09-15 23:42:44 +00:00
titleRef.current.clientHeight)
}, [])
useEffect(() => {
// if we are showing toc, then this is a full item
setHasNewComments(!toc && newComments(item))
}, [item])
2021-04-14 00:57:32 +00:00
return (
2021-04-14 23:56:29 +00:00
<>
2021-04-22 22:14:32 +00:00
{rank
? (
<div className={styles.rank}>
{rank}
</div>)
: <div />}
2021-04-14 23:56:29 +00:00
<div className={styles.item}>
2022-09-21 19:57:36 +00:00
{item.position
? <Pin width={24} height={24} className={styles.pin} />
: item.meDontLike ? <Flag width={24} height={24} className={`${styles.dontLike}`} /> : <UpVote item={item} className={styles.upvote} />}
2021-04-14 23:56:29 +00:00
<div className={styles.hunk}>
2021-09-15 23:42:44 +00:00
<div className={`${styles.main} flex-wrap ${wrap ? 'd-inline' : ''}`}>
2021-04-14 23:56:29 +00:00
<Link href={`/items/${item.id}`} passHref>
2022-02-03 22:01:42 +00:00
<a ref={titleRef} className={`${styles.title} text-reset mr-2`}>
{item.searchTitle ? <SearchTitle title={item.searchTitle} /> : item.title}
2022-07-30 13:25:46 +00:00
{item.pollCost && <span> <PollIcon className='fill-grey vertical-align-baseline' height={14} width={14} /></span>}
2022-02-03 22:01:42 +00:00
</a>
2021-04-14 23:56:29 +00:00
</Link>
{item.url &&
2021-12-06 21:27:14 +00:00
<>
{/* eslint-disable-next-line */}
<a
className={`${styles.link} ${wrap ? styles.linkSmall : ''}`} target='_blank' href={item.url}
rel={item.sats + item.boost >= NOFOLLOW_LIMIT ? null : 'nofollow'}
>
{item.url.replace(/(^https?:|^)\/\//, '')}
</a>
</>}
2021-04-14 23:56:29 +00:00
</div>
2021-04-28 16:30:02 +00:00
<div className={`${styles.other}`}>
2022-01-07 16:32:31 +00:00
{!item.position &&
<>
2022-10-25 21:35:32 +00:00
<span title={`from ${item.upvotes} users ${item.mine ? `\\ ${item.meSats} sats to post` : `(${item.meSats} sats from me)`} `}>{abbrNum(item.sats)} sats</span>
2022-01-07 16:32:31 +00:00
<span> \ </span>
</>}
2021-09-10 18:55:36 +00:00
{item.boost > 0 &&
<>
2022-10-25 21:35:32 +00:00
<span>{abbrNum(item.boost)} boost</span>
2021-09-10 18:55:36 +00:00
<span> \ </span>
</>}
2021-04-15 19:41:02 +00:00
<Link href={`/items/${item.id}`} passHref>
<a title={`${item.commentSats} sats`} className='text-reset'>
{item.ncomments} comments
{hasNewComments && <>{' '}<Badge className={styles.newComment} variant={null}>new</Badge></>}
</a>
2021-04-15 19:41:02 +00:00
</Link>
2021-04-14 23:56:29 +00:00
<span> \ </span>
2021-04-28 16:30:02 +00:00
<span>
<Link href={`/${item.user.name}`} passHref>
<a>@{item.user.name}</a>
</Link>
<span> </span>
<Link href={`/items/${item.id}`} passHref>
<a title={item.createdAt} className='text-reset'>{timeSince(new Date(item.createdAt))}</a>
</Link>
2022-09-27 21:19:15 +00:00
{me && !item.meSats && !item.position && !item.meDontLike && !item.mine && <DontLikeThis id={item.id} />}
{(item.outlawed && <Link href='/outlawed'><a>{' '}<Badge className={styles.newComment} variant={null}>OUTLAWED</Badge></a></Link>) ||
(item.freebie && !item.mine && (me?.greeterMode) && <Link href='/freebie'><a>{' '}<Badge className={styles.newComment} variant={null}>FREEBIE</Badge></a></Link>)}
{item.prior &&
<>
<span> \ </span>
<Link href={`/items/${item.prior}`} passHref>
<a className='text-reset'>yesterday</a>
</Link>
</>}
2021-04-28 16:30:02 +00:00
</span>
2021-08-11 20:13:10 +00:00
{canEdit &&
<>
<span> \ </span>
<Link href={`/items/${item.id}/edit`} passHref>
<a className='text-reset'>
edit
<Countdown
date={editThreshold}
2021-08-11 20:34:10 +00:00
className=' '
2021-08-11 20:13:10 +00:00
onComplete={() => {
setCanEdit(false)
}}
/>
</a>
</Link>
</>}
2021-04-14 23:56:29 +00:00
</div>
2022-04-19 18:32:39 +00:00
{showFwdUser && item.fwdUser && <FwdUser user={item.fwdUser} />}
2021-04-14 00:57:32 +00:00
</div>
2022-12-19 22:27:52 +00:00
{toc &&
<>
<Share item={item} />
<Toc text={item.text} />
</>}
2021-04-14 00:57:32 +00:00
</div>
2021-04-14 23:56:29 +00:00
{children && (
<div className={styles.children}>
{children}
</div>
)}
</>
2021-04-14 00:57:32 +00:00
)
}
2021-04-22 22:14:32 +00:00
2021-04-27 00:55:48 +00:00
export function ItemSkeleton ({ rank, children }) {
2021-04-22 22:14:32 +00:00
return (
<>
2022-01-27 19:18:48 +00:00
{rank
? (
<div className={styles.rank}>
{rank}
</div>)
: <div />}
2021-04-22 22:14:32 +00:00
<div className={`${styles.item} ${styles.skeleton}`}>
2021-04-28 19:30:14 +00:00
<UpVote className={styles.upvote} />
2021-04-22 22:14:32 +00:00
<div className={styles.hunk}>
<div className={`${styles.main} flex-wrap flex-md-nowrap`}>
<span className={`${styles.title} clouds text-reset flex-md-fill flex-md-shrink-0 mr-2`} />
<span className={`${styles.link} clouds`} />
</div>
<div className={styles.other}>
2021-04-28 22:52:03 +00:00
<span className={`${styles.otherItem} clouds`} />
2021-04-22 22:14:32 +00:00
<span className={`${styles.otherItem} clouds`} />
<span className={`${styles.otherItem} ${styles.otherItemLonger} clouds`} />
<span className={`${styles.otherItem} ${styles.otherItemLonger} clouds`} />
</div>
</div>
</div>
2021-04-27 00:55:48 +00:00
{children && (
<div className={styles.children}>
{children}
</div>
)}
2021-04-22 22:14:32 +00:00
</>
)
}