2023-02-16 22:23:59 +00:00
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useEffect , useState } from 'react'
import { Badge , Dropdown } from 'react-bootstrap'
import Countdown from './countdown'
import { abbrNum } from '../lib/format'
import { newComments } from '../lib/new-comments'
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 16:15:46 +00:00
< span title = { ` from ${ item . upvotes } users ${ 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 >
< / > }
< Link href = { ` /items/ ${ item . id } ` } passHref >
< a title = { ` ${ item . commentSats } sats ` } className = 'text-reset' >
{ item . ncomments } { commentsText || 'comments' }
{ hasNewComments && < > { ' ' } < Badge className = { styles . newComment } variant = { null } > new < /Badge></ > }
< / a >
< / L i n k >
< span > \ < / s p a n >
< span >
< Link href = { ` / ${ item . user . name } ` } passHref >
< a className = 'd-inline-flex align-items-center' >
2023-05-01 21:49:47 +00:00
@ { item . user . name } < CowboyHat className = 'ml-1 fill-grey' user = { item . user } height = { 12 } width = { 12 } / >
2023-02-16 22:23:59 +00:00
{ embellishUser }
< / a >
< / L i n k >
< span > < / s p a n >
< Link href = { ` /items/ ${ item . id } ` } passHref >
< a title = { item . createdAt } className = 'text-reset' > { timeSince ( new Date ( item . createdAt ) ) } < / a >
< / L i n k >
{ item . prior &&
< >
< span > \ < / s p a n >
< Link href = { ` /items/ ${ item . prior } ` } passHref >
< a className = 'text-reset' > yesterday < / a >
< / L i n k >
< / > }
< / s p a n >
2023-05-05 17:38:56 +00:00
{ item . subName &&
< Link href = { ` /~ ${ item . subName } ` } >
< a > { ' ' } < Badge className = { styles . newComment } variant = { null } > { item . subName } < / B a d g e > < / a >
2023-05-02 16:55:10 +00:00
< / L i n k > }
2023-02-16 22:23:59 +00:00
{ ( item . outlawed && ! item . mine &&
< Link href = '/outlawed' >
2023-05-02 16:55:10 +00:00
< a > { ' ' } < Badge className = { styles . newComment } variant = { null } > outlawed < / B a d g e > < / a >
< / L i n k > ) | |
( item . freebie && ! item . mine &&
2023-02-16 22:23:59 +00:00
< Link href = '/freebie' >
2023-05-02 16:55:10 +00:00
< a > { ' ' } < Badge className = { styles . newComment } variant = { null } > freebie < / B a d g e > < / a >
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 &&
< Dropdown . Item >
< Link passHref href = { ` /items/ ${ item . id } /ots ` } >
< a className = 'text-reset' > ots timestamp < / a >
< / L i n k >
< / D r o p d o w n . I t e m > }
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 (
< Dropdown className = 'pointer' as = 'span' >
< Dropdown . Toggle variant = 'success' id = 'dropdown-basic' as = 'a' >
< MoreIcon className = 'fill-grey ml-1' height = { 16 } width = { 16 } / >
< / 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 >
)
}