stacker.news/components/comments.js

37 lines
956 B
JavaScript
Raw Normal View History

2021-04-22 22:14:32 +00:00
import { useQuery } from '@apollo/client'
import { useRouter } from 'next/router'
2021-06-24 23:56:01 +00:00
import { useEffect } from 'react'
2021-04-22 22:14:32 +00:00
import Comment, { CommentSkeleton } from './comment'
2021-04-15 19:41:02 +00:00
2021-04-27 00:55:48 +00:00
export default function Comments ({ comments, ...props }) {
2021-06-24 23:56:01 +00:00
useEffect(() => {
// Your code here
const hash = window.location.hash
if (hash) {
document.querySelector(hash).scrollIntoView({ behavior: 'smooth' })
}
}, [])
2021-04-27 00:55:48 +00:00
return comments.map(item => (
2021-04-28 19:30:14 +00:00
<Comment key={item.id} item={item} {...props} />
2021-04-27 00:55:48 +00:00
))
}
export function CommentsSkeleton () {
2021-05-05 18:13:14 +00:00
return <CommentSkeleton skeletonChildren={7} />
2021-04-27 00:55:48 +00:00
}
export function CommentsQuery ({ query, ...props }) {
const router = useRouter()
const { error, data } = useQuery(query, {
fetchPolicy: router.query.cache ? 'cache-first' : undefined
})
2021-04-17 18:15:18 +00:00
2021-04-22 22:14:32 +00:00
if (error) return <div>Failed to load!</div>
if (!data) {
2021-04-27 00:55:48 +00:00
return <CommentsSkeleton />
2021-04-22 22:14:32 +00:00
}
2021-04-15 19:41:02 +00:00
2021-04-27 00:55:48 +00:00
return <Comments comments={data.comments} {...props} />
2021-04-15 19:41:02 +00:00
}