stacker.news/components/comment.js

139 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-04-14 23:56:29 +00:00
import itemStyles from './item.module.css'
import styles from './comment.module.css'
import Text from './text'
import Link from 'next/link'
import Reply from './reply'
import { useState } from 'react'
2021-04-15 19:41:02 +00:00
import { gql, useQuery } from '@apollo/client'
2021-04-18 18:50:04 +00:00
import { timeSince } from '../lib/time'
2021-04-22 22:14:32 +00:00
import UpVote from './upvote'
2021-04-30 21:42:51 +00:00
import Eye from '../svgs/eye-fill.svg'
import EyeClose from '../svgs/eye-close-line.svg'
2021-04-14 23:56:29 +00:00
2021-04-15 19:41:02 +00:00
function Parent ({ item }) {
const { data } = useQuery(
gql`{
root(id: ${item.id}) {
id
title
}
}`
)
const ParentFrag = () => (
<>
<span> \ </span>
<Link href={`/items/${item.parentId}`} passHref>
<a className='text-reset'>parent</a>
</Link>
</>
)
if (!data) {
return <ParentFrag />
}
return (
<>
{data.root.id !== item.parentId && <ParentFrag />}
<span> \ </span>
<Link href={`/items/${data.root.id}`} passHref>
<a className='text-reset'>{data.root.title}</a>
</Link>
</>
)
}
2021-04-27 00:55:48 +00:00
export default function Comment ({ item, children, replyOpen, includeParent, cacheId, noComments, noReply }) {
2021-04-15 19:41:02 +00:00
const [reply, setReply] = useState(replyOpen)
2021-04-30 21:42:51 +00:00
const [collapse, setCollapse] = useState(false)
2021-04-14 23:56:29 +00:00
return (
2021-04-30 21:42:51 +00:00
<div className={includeParent ? '' : `${styles.comment} ${collapse ? styles.collapsed : ''}`}>
2021-04-28 19:30:14 +00:00
<div className={`${itemStyles.item} ${styles.item}`}>
<UpVote itemId={item.id} meSats={item.meSats} className={styles.upvote} />
2021-04-30 21:42:51 +00:00
<div className={`${itemStyles.hunk} ${styles.hunk}`}>
<div className='d-flex align-items-center'>
<div className={`${itemStyles.other} ${styles.other}`}>
<span>{item.sats} sats</span>
<span> \ </span>
<span>{item.boost} boost</span>
<span> \ </span>
<Link href={`/items/${item.id}`} passHref>
<a className='text-reset'>{item.ncomments} replies</a>
</Link>
<span> \ </span>
<Link href={`/${item.user.name}`} passHref>
<a>@{item.user.name}</a>
</Link>
<span> </span>
<span>{timeSince(new Date(item.createdAt))}</span>
{includeParent && <Parent item={item} />}
</div>
{!includeParent && (collapse
? <Eye className={styles.collapser} height={10} width={10} onClick={() => setCollapse(false)} />
: <EyeClose className={styles.collapser} height={10} width={10} onClick={() => setCollapse(true)} />)}
2021-04-14 23:56:29 +00:00
</div>
2021-04-28 19:30:14 +00:00
<div className={styles.text}>
<Text>{item.text}</Text>
2021-04-14 23:56:29 +00:00
</div>
</div>
</div>
2021-04-28 19:30:14 +00:00
<div className={`${itemStyles.children} ${styles.children}`}>
{!noReply &&
<div
className={`${itemStyles.other} ${styles.reply}`}
onClick={() => setReply(!reply)}
>
{reply ? 'cancel' : 'reply'}
</div>}
{reply &&
2021-04-29 18:43:17 +00:00
<div className={styles.replyWrapper}>
2021-05-20 01:09:32 +00:00
<Reply
parentId={item.id} autoFocus={!replyOpen}
onSuccess={() => setReply(replyOpen || false)} cacheId={cacheId}
/>
2021-04-28 19:30:14 +00:00
</div>}
{children}
<div className={`${styles.comments} ml-sm-1 ml-md-3`}>
{item.comments && !noComments
? item.comments.map((item) => (
<Comment key={item.id} item={item} />
))
: null}
</div>
</div>
</div>
2021-04-22 22:14:32 +00:00
)
}
export function CommentSkeleton ({ skeletonChildren }) {
return (
2021-04-28 22:52:03 +00:00
<div className={styles.comment}>
2021-04-22 22:14:32 +00:00
<div className={`${itemStyles.item} ${itemStyles.skeleton} ${styles.item} ${styles.skeleton}`}>
<UpVote className={styles.upvote} />
<div className={`${itemStyles.hunk} ${styles.hunk}`}>
<div className={itemStyles.other}>
2021-04-28 22:52:03 +00:00
<span className={`${itemStyles.otherItem} clouds`} />
<span className={`${itemStyles.otherItem} clouds`} />
2021-04-22 22:14:32 +00:00
<span className={`${itemStyles.otherItem} clouds`} />
<span className={`${itemStyles.otherItem} ${itemStyles.otherItemLonger} clouds`} />
</div>
<div className={`${styles.text} clouds`} />
2021-04-14 23:56:29 +00:00
</div>
2021-04-22 22:14:32 +00:00
</div>
2021-04-28 22:52:03 +00:00
<div className={`${itemStyles.children} ${styles.children} ${styles.skeleton}`}>
<div className={styles.replyPadder}>
<div className={`${itemStyles.other} ${styles.reply} clouds`} />
</div>
<div className={`${styles.comments} ml-sm-1 ml-md-3`}>
2021-05-05 18:13:14 +00:00
{skeletonChildren
? <CommentSkeleton skeletonChildren={skeletonChildren - 1} />
2021-04-17 18:15:18 +00:00
: null}
</div>
2021-04-14 23:56:29 +00:00
</div>
2021-04-28 22:52:03 +00:00
</div>
2021-04-14 23:56:29 +00:00
)
}