stacker.news/components/item-full.js

91 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-09-24 21:28:21 +00:00
import Item from './item'
import Reply from './reply'
2021-09-23 17:42:00 +00:00
import Comment from './comment'
import Text from './text'
2021-09-24 21:28:21 +00:00
import Comments from './comments'
2021-09-23 17:42:00 +00:00
import { COMMENTS } from '../fragments/comments'
import { ITEM_FIELDS } from '../fragments/items'
import { gql, useQuery } from '@apollo/client'
import styles from '../styles/item.module.css'
import { NOFOLLOW_LIMIT } from '../lib/constants'
import { useRouter } from 'next/router'
2021-09-23 22:18:48 +00:00
import { useMe } from './me'
2021-09-24 21:28:21 +00:00
import { Button } from 'react-bootstrap'
2021-09-23 17:42:00 +00:00
2021-09-24 21:28:21 +00:00
function BioItem ({ item, handleClick }) {
2021-09-23 22:18:48 +00:00
const me = useMe()
2021-09-23 20:09:07 +00:00
if (!item.text) {
return null
}
return (
<>
<ItemText item={item} />
2021-09-23 22:18:48 +00:00
{me?.name === item.user.name &&
2021-09-24 21:28:21 +00:00
<Button
onClick={handleClick}
size='md' variant='link'
className='text-right'
>edit bio
</Button>}
2021-09-23 20:09:07 +00:00
<Reply parentId={item.id} />
</>
)
}
function TopLevelItem ({ item }) {
return (
<Item item={item}>
{item.text && <ItemText item={item} />}
<Reply parentId={item.id} replyOpen />
</Item>
)
}
function ItemText ({ item }) {
return <Text nofollow={item.sats + item.boost < NOFOLLOW_LIMIT}>{item.text}</Text>
}
2021-09-24 21:28:21 +00:00
export default function ItemFull ({ item, bio, ...props }) {
2021-09-23 17:42:00 +00:00
const query = gql`
${ITEM_FIELDS}
${COMMENTS}
{
2021-09-24 21:28:21 +00:00
item(id: ${item.id}) {
2021-09-23 17:42:00 +00:00
...ItemFields
text
comments {
...CommentsRecursive
}
}
}`
const router = useRouter()
const { error, data } = useQuery(query, {
fetchPolicy: router.query.cache ? 'cache-first' : undefined
})
2021-09-24 21:28:21 +00:00
if (error) {
return <div>Failed to load!</div>
2021-09-23 17:42:00 +00:00
}
2021-09-24 21:28:21 +00:00
// XXX replace item with cache version
if (data) {
({ item } = data)
}
2021-09-23 17:42:00 +00:00
return (
<>
{item.parentId
2021-09-24 21:28:21 +00:00
? <Comment item={item} replyOpen includeParent noComments {...props} />
2021-09-23 20:09:07 +00:00
: (bio
2021-09-24 21:28:21 +00:00
? <BioItem item={item} {...props} />
: <TopLevelItem item={item} {...props} />
2021-09-23 17:42:00 +00:00
)}
2021-09-24 21:28:21 +00:00
{item.comments &&
<div className={styles.comments}>
<Comments comments={item.comments} />
</div>}
2021-09-23 17:42:00 +00:00
</>
)
}