stacker.news/components/item-full.js

88 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-09-23 17:42:00 +00:00
import Item, { ItemSkeleton } from './item'
import Reply, { ReplySkeleton } from './reply'
import Comment from './comment'
import Text from './text'
import Comments, { CommentsSkeleton } from './comments'
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 20:09:07 +00:00
function BioItem ({ item }) {
if (!item.text) {
return null
}
return (
<>
<ItemText item={item} />
<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>
}
export default function ItemFull ({ item: qItem, bio }) {
2021-09-23 17:42:00 +00:00
const query = gql`
${ITEM_FIELDS}
${COMMENTS}
{
item(id: ${qItem.id}) {
...ItemFields
text
comments {
...CommentsRecursive
}
}
}`
const router = useRouter()
const { error, data } = useQuery(query, {
fetchPolicy: router.query.cache ? 'cache-first' : undefined
})
if (error) return <div>Failed to load!</div>
if (!data) {
return (
<div>
<ItemSkeleton>
<ReplySkeleton />
</ItemSkeleton>
<div className={styles.comments}>
<CommentsSkeleton />
</div>
</div>
)
}
const { item } = data
return (
<>
{item.parentId
? <Comment item={item} replyOpen includeParent noComments />
2021-09-23 20:09:07 +00:00
: (bio
? <BioItem item={item} />
: <TopLevelItem item={item} />
2021-09-23 17:42:00 +00:00
)}
<div className={styles.comments}>
<Comments comments={item.comments} />
</div>
</>
)
}