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-22 22:14:32 +00:00
|
|
|
import UpVote from './upvote'
|
2023-12-27 02:27:52 +00:00
|
|
|
import { useRef } from 'react'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { AD_USER_ID, UNKNOWN_LINK_REL } from '@/lib/constants'
|
|
|
|
import Pin from '@/svgs/pushpin-fill.svg'
|
2022-02-03 22:01:42 +00:00
|
|
|
import reactStringReplace from 'react-string-replace'
|
2024-03-20 00:37:31 +00:00
|
|
|
import PollIcon from '@/svgs/bar-chart-horizontal-fill.svg'
|
|
|
|
import BountyIcon from '@/svgs/bounty-bag.svg'
|
2023-01-26 16:11:55 +00:00
|
|
|
import ActionTooltip from './action-tooltip'
|
2024-03-20 00:37:31 +00:00
|
|
|
import ImageIcon from '@/svgs/image-fill.svg'
|
|
|
|
import { numWithUnits } from '@/lib/format'
|
2023-02-16 22:23:59 +00:00
|
|
|
import ItemInfo from './item-info'
|
2024-03-20 00:37:31 +00:00
|
|
|
import Prism from '@/svgs/prism.svg'
|
|
|
|
import { commentsViewedAt } from '@/lib/new-comments'
|
2023-08-06 19:18:40 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2023-08-16 19:03:37 +00:00
|
|
|
import { Badge } from 'react-bootstrap'
|
2024-03-20 00:37:31 +00:00
|
|
|
import AdIcon from '@/svgs/advertisement-fill.svg'
|
2023-12-20 01:55:19 +00:00
|
|
|
import { DownZap } from './dont-link-this'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { timeLeft } from '@/lib/time'
|
2022-02-03 22:01:42 +00:00
|
|
|
|
2022-07-21 22:55:05 +00:00
|
|
|
export function SearchTitle ({ title }) {
|
2023-10-03 00:07:05 +00:00
|
|
|
return reactStringReplace(title, /\*\*\*([^*]+)\*\*\*/g, (match, i) => {
|
2023-10-15 21:13:54 +00:00
|
|
|
return <mark key={`strong-${match}-${i}`}>{match}</mark>
|
2022-02-03 22:01:42 +00:00
|
|
|
})
|
|
|
|
}
|
2021-04-14 00:57:32 +00:00
|
|
|
|
2024-02-03 21:26:21 +00:00
|
|
|
export default function Item ({ item, rank, belowTitle, right, full, children, siblingComments, onQuoteReply, pinnable }) {
|
2021-09-15 23:42:44 +00:00
|
|
|
const titleRef = useRef()
|
2023-08-06 19:18:40 +00:00
|
|
|
const router = useRouter()
|
2021-09-15 23:42:44 +00:00
|
|
|
|
2023-07-13 20:18:04 +00:00
|
|
|
const image = item.url && item.url.startsWith(process.env.NEXT_PUBLIC_IMGPROXY_URL)
|
|
|
|
|
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 />}
|
2023-10-26 17:52:06 +00:00
|
|
|
<div className={`${styles.item} ${siblingComments ? 'pt-3' : ''}`}>
|
2024-02-03 21:26:21 +00:00
|
|
|
{item.position && (pinnable || !item.subName)
|
2022-09-21 19:57:36 +00:00
|
|
|
? <Pin width={24} height={24} className={styles.pin} />
|
2023-12-20 01:55:19 +00:00
|
|
|
: item.meDontLikeSats > item.meSats
|
|
|
|
? <DownZap width={24} height={24} className={styles.dontLike} id={item.id} meDontLikeSats={item.meDontLikeSats} />
|
2023-08-16 22:53:51 +00:00
|
|
|
: Number(item.user?.id) === AD_USER_ID
|
|
|
|
? <AdIcon width={24} height={24} className={styles.ad} />
|
2023-12-27 02:27:52 +00:00
|
|
|
: <UpVote item={item} className={styles.upvote} />}
|
2021-04-14 23:56:29 +00:00
|
|
|
<div className={styles.hunk}>
|
2023-05-01 20:58:30 +00:00
|
|
|
<div className={`${styles.main} flex-wrap`}>
|
2023-08-06 19:18:40 +00:00
|
|
|
<Link
|
|
|
|
href={`/items/${item.id}`}
|
|
|
|
onClick={(e) => {
|
|
|
|
const viewedAt = commentsViewedAt(item)
|
|
|
|
if (viewedAt) {
|
|
|
|
e.preventDefault()
|
2023-10-26 17:30:13 +00:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
|
|
|
window.open(
|
2023-10-26 19:36:20 +00:00
|
|
|
`/items/${item.id}`,
|
2023-10-26 17:30:13 +00:00
|
|
|
'_blank',
|
|
|
|
'noopener,noreferrer'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
router.push(
|
2023-08-06 19:18:40 +00:00
|
|
|
`/items/${item.id}?commentsViewedAt=${viewedAt}`,
|
|
|
|
`/items/${item.id}`)
|
2023-10-26 17:30:13 +00:00
|
|
|
}
|
2023-08-06 19:18:40 +00:00
|
|
|
}
|
|
|
|
}} ref={titleRef} className={`${styles.title} text-reset me-2`}
|
|
|
|
>
|
2023-07-23 15:08:43 +00:00
|
|
|
{item.searchTitle ? <SearchTitle title={item.searchTitle} /> : item.title}
|
2024-03-06 20:02:48 +00:00
|
|
|
{item.pollCost && <PollIndicator item={item} />}
|
2023-07-23 15:08:43 +00:00
|
|
|
{item.bounty > 0 &&
|
|
|
|
<span className={styles.icon}>
|
2023-08-08 21:04:06 +00:00
|
|
|
<ActionTooltip notForm overlayText={`${numWithUnits(item.bounty)} ${item.bountyPaidTo?.length ? ' paid' : ' bounty'}`}>
|
2023-07-23 15:08:43 +00:00
|
|
|
<BountyIcon className={`${styles.bountyIcon} ${item.bountyPaidTo?.length ? 'fill-success' : 'fill-grey'}`} height={16} width={16} />
|
|
|
|
</ActionTooltip>
|
|
|
|
</span>}
|
2023-09-26 21:44:57 +00:00
|
|
|
{item.forwards?.length > 0 && <span className={styles.icon}><Prism className='fill-grey ms-1' height={14} width={14} /></span>}
|
2023-07-24 18:35:05 +00:00
|
|
|
{image && <span className={styles.icon}><ImageIcon className='fill-grey ms-2' height={16} width={16} /></span>}
|
2021-04-14 23:56:29 +00:00
|
|
|
</Link>
|
2023-07-13 20:18:04 +00:00
|
|
|
{item.url && !image &&
|
2024-03-05 01:20:14 +00:00
|
|
|
// eslint-disable-next-line
|
|
|
|
<a
|
|
|
|
className={styles.link} target='_blank' href={item.url}
|
|
|
|
rel={item.rel ?? UNKNOWN_LINK_REL}
|
|
|
|
>
|
|
|
|
{item.url.replace(/(^https?:|^)\/\//, '')}
|
|
|
|
</a>}
|
2021-04-14 23:56:29 +00:00
|
|
|
</div>
|
2023-08-16 19:03:37 +00:00
|
|
|
<ItemInfo
|
2023-12-27 02:27:52 +00:00
|
|
|
full={full} item={item}
|
2023-11-21 03:37:57 +00:00
|
|
|
onQuoteReply={onQuoteReply}
|
2024-02-03 21:26:21 +00:00
|
|
|
pinnable={pinnable}
|
2023-11-20 14:33:59 +00:00
|
|
|
extraBadges={Number(item?.user?.id) === AD_USER_ID && <Badge className={styles.newComment} bg={null}>AD</Badge>}
|
2023-08-16 19:03:37 +00:00
|
|
|
/>
|
2023-02-16 22:23:59 +00:00
|
|
|
{belowTitle}
|
2021-04-14 00:57:32 +00:00
|
|
|
</div>
|
2023-02-16 22:23:59 +00:00
|
|
|
{right}
|
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`}>
|
2023-07-24 18:35:05 +00:00
|
|
|
<span className={`${styles.title} clouds text-reset flex-md-fill flex-md-shrink-0 me-2`} />
|
2021-04-22 22:14:32 +00:00
|
|
|
<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
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2024-03-06 20:02:48 +00:00
|
|
|
|
|
|
|
function PollIndicator ({ item }) {
|
|
|
|
const hasExpiration = !!item.pollExpiresAt
|
|
|
|
const timeRemaining = timeLeft(new Date(item.pollExpiresAt))
|
|
|
|
const isActive = !hasExpiration || !!timeRemaining
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className={styles.icon} title={isActive ? 'active' : 'results in'}>
|
|
|
|
<PollIcon
|
|
|
|
className={`${
|
|
|
|
isActive
|
|
|
|
? 'fill-success'
|
|
|
|
: 'fill-grey'
|
|
|
|
} ms-1`} height={14} width={14}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|