cloudflare/vinext
Publicmirrored fromhttps://github.com/cloudflare/vinextAvailable
examples/hackernews/components/comment.jsx
39lines · modecode
| 1 | 'use client'; |
| 2 | |
| 3 | import { useState } from 'react'; |
| 4 | |
| 5 | import timeAgo from '../lib/time-ago'; |
| 6 | |
| 7 | import styles from './comment.module.css'; |
| 8 | |
| 9 | export default function Comment({ user, text, date, comments, commentsCount }) { |
| 10 | const [toggled, setToggled] = useState(false); |
| 11 | |
| 12 | const toggle = () => setToggled(!toggled); |
| 13 | |
| 14 | return ( |
| 15 | <div className={styles.comment}> |
| 16 | <div className={styles.meta} suppressHydrationWarning> |
| 17 | {user} {timeAgo(new Date(date))} ago{' '} |
| 18 | <span onClick={toggle} className={styles.toggle}> |
| 19 | {toggled ? `[+${(commentsCount || 0) + 1}]` : '[-]'} |
| 20 | </span> |
| 21 | </div> |
| 22 | |
| 23 | {toggled |
| 24 | ? null |
| 25 | : [ |
| 26 | <div |
| 27 | key="text" |
| 28 | className={styles.text} |
| 29 | dangerouslySetInnerHTML={{ __html: text }} |
| 30 | />, |
| 31 | <div key="children" className={styles.children}> |
| 32 | {comments.map((comment) => ( |
| 33 | <Comment key={comment.id} {...comment} /> |
| 34 | ))} |
| 35 | </div>, |
| 36 | ]} |
| 37 | </div> |
| 38 | ); |
| 39 | } |
| 40 | |