import { gql, useApolloClient, useLazyQuery, useQuery } from '@apollo/client'
import { useEffect, useState } from 'react'
import Comment, { CommentSkeleton } from './comment'
import styles from './header.module.css'
import { Nav, Navbar } from 'react-bootstrap'
import { COMMENTS_QUERY } from '../fragments/items'
import { COMMENTS } from '../fragments/comments'
export function CommentsHeader ({ handleSort }) {
const [sort, setSort] = useState('hot')
const getHandleClick = sort => {
return () => {
setSort(sort)
handleSort(sort)
}
}
return (
)
}
export default function Comments ({ parentId, comments, ...props }) {
const client = useApolloClient()
useEffect(() => {
const hash = window.location.hash
if (hash) {
document.querySelector(hash).scrollIntoView({ behavior: 'smooth' })
}
}, [])
const [getComments, { loading }] = useLazyQuery(COMMENTS_QUERY, {
fetchPolicy: 'network-only',
onCompleted: data => {
client.writeFragment({
id: `Item:${parentId}`,
fragment: gql`
${COMMENTS}
fragment Comments on Item {
comments {
...CommentsRecursive
}
}
`,
fragmentName: 'Comments',
data: {
comments: data.comments
}
})
}
})
return (
<>
{comments.length ? getComments({ variables: { id: parentId, sort } })} /> : null}
{loading
?
: comments.map(item => (
))}
>
)
}
export function CommentsSkeleton () {
return
}
export function CommentsQuery ({ query, ...props }) {
const { error, data } = useQuery(query)
if (error) return Failed to load!
if (!data) {
return
}
return
}