2023-02-16 22:23:59 +00:00
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useEffect , useState } from 'react'
2023-07-24 18:35:05 +00:00
import Badge from 'react-bootstrap/Badge'
import Dropdown from 'react-bootstrap/Dropdown'
2023-02-16 22:23:59 +00:00
import Countdown from './countdown'
import { abbrNum } from '../lib/format'
2023-08-06 19:18:40 +00:00
import { newComments , commentsViewedAt } from '../lib/new-comments'
2023-02-16 22:23:59 +00:00
import { timeSince } from '../lib/time'
import CowboyHat from './cowboy-hat'
import { DeleteDropdownItem } from './delete'
import styles from './item.module.css'
import { useMe } from './me'
import MoreIcon from '../svgs/more-fill.svg'
import DontLikeThisDropdownItem from './dont-link-this'
import BookmarkDropdownItem from './bookmark'
2023-06-01 00:44:06 +00:00
import SubscribeDropdownItem from './subscribe'
2023-02-16 22:23:59 +00:00
import { CopyLinkDropdownItem } from './share'
2023-07-09 16:15:46 +00:00
export default function ItemInfo ( { item , pendingSats , full , commentsText , className , embellishUser , extraInfo , onEdit , editText } ) {
2023-02-16 22:23:59 +00:00
const editThreshold = new Date ( item . createdAt ) . getTime ( ) + 10 * 60000
const me = useMe ( )
const router = useRouter ( )
const [ canEdit , setCanEdit ] =
useState ( item . mine && ( Date . now ( ) < editThreshold ) )
const [ hasNewComments , setHasNewComments ] = useState ( false )
useEffect ( ( ) => {
2023-05-11 19:34:42 +00:00
if ( ! full ) {
setHasNewComments ( newComments ( item ) )
}
2023-02-16 22:23:59 +00:00
} , [ item ] )
return (
< div className = { className || ` ${ styles . other } ` } >
{ ! item . position &&
< >
2023-07-09 17:37:12 +00:00
< span title = { ` from ${ item . upvotes } stackers ${ item . mine ? ` \\ ${ item . meSats } sats to post ` : ` ( ${ item . meSats + pendingSats } sats from me) ` } ` } > { abbrNum ( item . sats + pendingSats ) } sats < / s p a n >
2023-02-16 22:23:59 +00:00
< span > \ < / s p a n >
< / > }
{ item . boost > 0 &&
< >
< span > { abbrNum ( item . boost ) } boost < / s p a n >
< span > \ < / s p a n >
< / > }
2023-08-06 19:18:40 +00:00
< Link
href = { ` /items/ ${ item . id } ` } onClick = { ( e ) => {
const viewedAt = commentsViewedAt ( item )
if ( viewedAt ) {
e . preventDefault ( )
router . push (
` /items/ ${ item . id } ?commentsViewedAt= ${ viewedAt } ` ,
` /items/ ${ item . id } ` )
}
} } title = { ` ${ item . commentSats } sats ` } className = 'text-reset position-relative'
>
2023-07-23 15:08:43 +00:00
{ item . ncomments } { commentsText || 'comments' }
2023-08-06 18:21:38 +00:00
{ hasNewComments &&
< span className = { styles . notification } >
< span className = 'invisible' > { ' ' } < / s p a n >
< / s p a n > }
2023-02-16 22:23:59 +00:00
< / L i n k >
< span > \ < / s p a n >
< span >
2023-07-24 18:35:05 +00:00
< Link href = { ` / ${ item . user . name } ` } >
@ { item . user . name } < CowboyHat className = 'ms-1 fill-grey' user = { item . user } height = { 12 } width = { 12 } / >
2023-07-23 15:08:43 +00:00
{ embellishUser }
2023-02-16 22:23:59 +00:00
< / L i n k >
< span > < / s p a n >
2023-07-23 15:08:43 +00:00
< Link href = { ` /items/ ${ item . id } ` } title = { item . createdAt } className = 'text-reset' suppressHydrationWarning >
{ timeSince ( new Date ( item . createdAt ) ) }
2023-02-16 22:23:59 +00:00
< / L i n k >
{ item . prior &&
< >
< span > \ < / s p a n >
2023-07-23 15:08:43 +00:00
< Link href = { ` /items/ ${ item . prior } ` } className = 'text-reset' >
yesterday
2023-02-16 22:23:59 +00:00
< / L i n k >
< / > }
< / s p a n >
2023-05-05 17:38:56 +00:00
{ item . subName &&
< Link href = { ` /~ ${ item . subName } ` } >
2023-07-24 18:35:05 +00:00
{ ' ' } < Badge className = { styles . newComment } bg = { null } > { item . subName } < / B a d g e >
2023-05-02 16:55:10 +00:00
< / L i n k > }
2023-02-16 22:23:59 +00:00
{ ( item . outlawed && ! item . mine &&
2023-07-23 15:08:43 +00:00
< Link href = '/recent/outlawed' >
2023-07-24 18:35:05 +00:00
{ ' ' } < Badge className = { styles . newComment } bg = { null } > outlawed < / B a d g e >
2023-05-02 16:55:10 +00:00
< / L i n k > ) | |
2023-07-23 15:08:43 +00:00
( item . freebie &&
< Link href = '/recent/freebies' >
2023-07-24 18:35:05 +00:00
{ ' ' } < Badge className = { styles . newComment } bg = { null } > freebie < / B a d g e >
2023-02-16 22:23:59 +00:00
< / L i n k >
2023-05-02 16:55:10 +00:00
) }
2023-02-16 22:23:59 +00:00
{ canEdit && ! item . deletedAt &&
< >
< span > \ < / s p a n >
< span
className = 'text-reset pointer'
onClick = { ( ) => onEdit ? onEdit ( ) : router . push ( ` /items/ ${ item . id } /edit ` ) }
>
{ editText || 'edit' }
< Countdown
date = { editThreshold }
onComplete = { ( ) => {
setCanEdit ( false )
} }
/ >
< / s p a n >
< / > }
< ItemDropdown >
< CopyLinkDropdownItem item = { item } / >
2023-02-16 23:07:20 +00:00
{ me && < BookmarkDropdownItem item = { item } / > }
2023-06-01 00:44:06 +00:00
{ me && item . user . id !== me . id && < SubscribeDropdownItem item = { item } / > }
2023-02-16 23:07:20 +00:00
{ item . otsHash &&
2023-07-23 15:08:43 +00:00
< Link href = { ` /items/ ${ item . id } /ots ` } className = 'text-reset dropdown-item' >
ots timestamp
< / L i n k > }
2023-02-16 22:23:59 +00:00
{ me && ! item . meSats && ! item . position && ! item . meDontLike &&
! item . mine && ! item . deletedAt && < DontLikeThisDropdownItem id = { item . id } / > }
{ item . mine && ! item . position && ! item . deletedAt &&
< DeleteDropdownItem itemId = { item . id } / > }
< / I t e m D r o p d o w n >
2023-02-16 23:14:14 +00:00
{ extraInfo }
2023-02-16 22:23:59 +00:00
< / d i v >
)
}
export function ItemDropdown ( { children } ) {
return (
2023-07-24 18:35:05 +00:00
< Dropdown className = { ` pointer ${ styles . dropdown } ` } as = 'span' >
< Dropdown . Toggle variant = 'success' as = 'a' >
< MoreIcon className = 'fill-grey ms-1' height = { 16 } width = { 16 } / >
2023-02-16 22:23:59 +00:00
< / D r o p d o w n . T o g g l e >
< Dropdown . Menu >
{ children }
< / D r o p d o w n . M e n u >
< / D r o p d o w n >
)
}