import React from 'react'; import { Link } from 'react-router-dom'; import { HashLink } from 'react-router-hash-link'; import { Helmet } from 'react-helmet'; import moment from 'moment'; import localForage from 'localforage'; import { infoLine, otherDiscussions, ToggleDot } from '../utils.js'; class Comments extends React.Component { constructor(props) { super(props); const id = this.props.match.params.id; const cache = this.props.cache; if (id in cache) console.log('cache hit'); this.state = { story: cache[id] || false, related: [], error: false, collapsed: [], expanded: [], }; } componentDidMount() { const id = this.props.match.params.id; localForage.getItem(id).then((value) => this.setState({ story: value })); localForage.getItem(`related-${id}`).then((value) => value ? this.setState({ related: value }) : null); fetch('/api/' + id) .then(res => res.json()) .then( (result) => { this.setState({ story: result.story, related: result.related }, () => { const hash = window.location.hash.substring(1); if (hash) { document.getElementById(hash).scrollIntoView(); } }); localForage.setItem(id, result.story); localForage.setItem(`related-${id}`, result.related); }, (error) => { this.setState({ error: true }); } ); } collapseComment(cid) { this.setState(prevState => ({ ...prevState, collapsed: [...prevState.collapsed, cid], expanded: prevState.expanded.filter(x => x !== cid), })); } expandComment(cid) { this.setState(prevState => ({ ...prevState, collapsed: prevState.collapsed.filter(x => x !== cid), expanded: [...prevState.expanded, cid], })); } countComments(c) { return c.comments.reduce((sum, x) => sum + this.countComments(x), 1); } displayComment(story, c, level) { const cid = c.author + c.date; const collapsed = this.state.collapsed.includes(cid); const expanded = this.state.expanded.includes(cid); const hidden = collapsed || (level == 4 && !expanded); const hasChildren = c.comments.length !== 0; return (
{c.author === story.author ? '[OP]' : ''} {c.author || '[Deleted]'}
{' '} |
Connection error?
} {story ?loading...
}