import React, { useState, useEffect, useContext } from 'react'; import { Link, useNavigate, useParams } from 'react-router-dom'; import { Container, NavBar, Header, Break, PostForm, BoardForm } from './styles/Board.styled'; import { ReplyFormTable, ReplyFormLink, TopBar, BottomBar } from './styles/Thread.styled'; import { BoardContext } from '../App'; import { useComment } from '@plebbit/plebbit-react-hooks'; import ImageBanner from './ImageBanner'; import { Tooltip } from 'react-tooltip'; const Thread = ({ setBodyStyle }) => { const [defaultSubplebbits, setDefaultSubplebbits] = useState([]); const { selectedTitle, setSelectedTitle, selectedAddress, setSelectedAddress, selectedThread, setSelectedThread, selectedStyle, setSelectedStyle } = useContext(BoardContext); const [showReplyFormLink, setShowReplyFormLink] = useState(true); const [showReplyForm, setShowReplyForm] = useState(false); const navigate = useNavigate(); const [prevScrollPos, setPrevScrollPos] = useState(0); const [visible, setVisible] = useState(true); const comment = useComment(`${selectedThread}`); const { subplebbitAddress, threadCid } = useParams(); useEffect(() => { setSelectedAddress(subplebbitAddress); setSelectedThread(threadCid); const selectedSubplebbit = defaultSubplebbits.find((subplebbit) => subplebbit.address === subplebbitAddress); if (selectedSubplebbit) { setSelectedTitle(selectedSubplebbit.title); } }, [subplebbitAddress, setSelectedAddress, setSelectedTitle, defaultSubplebbits]); useEffect(() => { let didCancel = false; fetch( "https://raw.githubusercontent.com/plebbit/temporary-default-subplebbits/master/subplebbits.json", { cache: "no-cache" } ) .then((res) => res.json()) .then(res => { if (!didCancel) { setDefaultSubplebbits(res); } }); return () => { didCancel = true; }; }, []); useEffect(() => { const handleScroll = () => { const currentScrollPos = window.pageYOffset; setVisible(prevScrollPos > currentScrollPos || currentScrollPos < 10); setPrevScrollPos(currentScrollPos); }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, [prevScrollPos, visible]); const handleVoidClick = () => {} const handleClickTitle = (title, address) => { setSelectedTitle(title); setSelectedAddress(address); }; const handleSelectChange = (event) => { const selected = event.target.value; const selectedTitle = defaultSubplebbits.find((subplebbit) => subplebbit.address === selected).title; setSelectedTitle(selectedTitle); setSelectedAddress(selected); navigate(`/${selected}`); } const handleClickHelp = () => { alert("- The CAPTCHA loads after you click \"Post\" \n- The CAPTCHA is case-sensitive. \n- Make sure to not block any cookies set by plebchan."); }; const handleClickForm = () => { setShowReplyFormLink(false); setShowReplyForm(true); navigate(`/${selectedAddress}/thread/${selectedThread}/post`); }; const handleClickTop = () => { window.scrollTo(0, 0); } const handleClickBottom = () => { window.scrollTo(0, document.body.scrollHeight); } const handleStyleChange = (event) => { switch (event.target.value) { case "Yotsuba": setBodyStyle({ background: "#ffe url(/assets/fade.png) top repeat-x", color: "maroon", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Yotsuba"); break; case "Yotsuba B": setBodyStyle({ background: "#eef2ff url(/assets/fade-blue.png) top center repeat-x", color: "#000", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Yotsuba B"); break; case "Futaba": setBodyStyle({ background: "#ffe", color: "maroon", fontFamily: "times new roman, serif" }); setSelectedStyle("Futaba"); break; case "Burichan": setBodyStyle({ background: "#eef2ff", color: "#000", fontFamily: "times new roman, serif" }); setSelectedStyle("Burichan"); break; case "Tomorrow": setBodyStyle({ background: "#1d1f21 none", color: "#c5c8c6", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Tomorrow"); break; case "Photon": setBodyStyle({ background: "#eee none", color: "#333", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Photon"); break; default: setBodyStyle({ background: "#ffe url(/assets/fade.png) top repeat-x", color: "maroon", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Yotsuba"); } } function renderComments(comments) { const commentKeys = Object.keys(comments); const renderedComments = commentKeys.map(key => { const comment = comments[key]; const { replies: { pages: { topAll: { comments: nestedComments } } } } = comment; if (comment.replyCount > 0 && nestedComments) { const renderedNestedComments = renderComments(nestedComments); return [comment, ...renderedNestedComments]; } return [comment]; }).flat(); return renderedComments; } return ( <> {defaultSubplebbits.map(subplebbit => ( [ handleClickTitle(subplebbit.title, subplebbit.address)} >{subplebbit.title} ]  ))} [ Settings ] [ handleStyleChange({target: {value: "Yotsuba"}} )}>Home ]
Board  
Settings   handleStyleChange({target: {value: "Yotsuba"}} )}>Home
 
 
<>
<>
{selectedTitle}
p/{selectedAddress}
Return
Catalog
[ event.target.style.cursor='pointer'}>Post a Reply ]
event.target.style.cursor='pointer'}>Post a Reply
Name Comment Verification
Embed File

Style:   [ Return ] [ Catalog ] [ event.target.style.cursor='pointer'} onTouchStart={handleClickBottom}>Bottom ] {comment ? ( comment.replyCount > 0 ? ( {comment.replyCount} replies ) : ( No replies yet )) : ( null )}
{comment ? ( <>
File:  filename.something (metadata)
filename.something
{comment.title ? ( comment.title.length > 75 ? <> {comment.title.slice(0, 75) + " (...)"} : {comment.title} ) : null}   {comment.author.displayName ? comment.author.displayName.length > 20 ? <> {comment.author.displayName.slice(0, 20) + " (...)"} : {comment.author.displayName} : Anonymous}     (User:  {comment.author.address.length > 20 ? <> {comment.author.address.slice(0, 20) + "..."} : {comment.author.address} })   2 weeks ago   No. 00000001
{comment.content}
{comment.replyCount > 0 && Object.keys(comment.replies.pages.topAll.comments).map(() => { const renderedComments = renderComments(comment.replies.pages.topAll.comments); return renderedComments.map(reply => { let counter = 1; counter++; const counterString = counter.toString().padStart(8, '0'); return (
{'>>'}
  {reply.author.displayName ? reply.author.displayName.length > 20 ? <> {reply.author.displayName.slice(0, 20) + " (...)"} : {reply.author.displayName} : Anonymous}   (User:  {reply.author.address.length > 20 ? <> {reply.author.address.slice(0, 20) + "..."} : {reply.author.address} })   2 weeks ago   No. {counterString}
{`>>${counterString}`}{
}
{reply.content}
) }) })}

... {comment.author.displayName ? comment.author.displayName.length > 15 ? <> {comment.author.displayName.slice(0, 15) + " (...)"} : {comment.author.displayName} : Anonymous}   (User:  {comment.author.address.length > 15 ? <> {comment.author.address.slice(0, 15) + "..."} : {comment.author.address} } ) 
{comment.title ? ( comment.title.length > 30 ? <> {comment.title.slice(0, 30) + " (...)"} : {comment.title} ) : null}
2 weeks ago   No. 00000001
{comment.content ? ( <> {comment.content} ) : null}
{comment.replyCount > 0 && Object.keys(comment.replies.pages.topAll.comments).map(() => { const renderedComments = renderComments(comment.replies.pages.topAll.comments); return renderedComments.map(reply => { let counter = 1; counter++; const counterString = counter.toString().padStart(8, '0'); return (
... {reply.author.displayName ? reply.author.displayName.length > 12 ? <> {reply.author.displayName.slice(0, 12) + " (...)"} : {reply.author.displayName} : Anonymous}   (User:  {reply.author.address.length > 12 ? <> {reply.author.address.slice(0, 12) + "..."} : {reply.author.address} } )
2 weeks ago  No. {counterString}
{`>>${counterString}`}{
}
{reply.content}
) }) })}

[ Return ] [ Catalog ] [ event.target.style.cursor='pointer'} onTouchStart={handleClickTop}>Top ] [ event.target.style.cursor='pointer'}>Post a Reply ] {comment.replyCount > 0 ? ( {comment.replyCount} replies ) : ( No replies yet )}

Style:   {comment ? ( comment.replyCount > 0 ? ( {comment.replyCount} replies ) : ( No replies yet )) : ( null )}
event.target.style.cursor='pointer'}>Post a Reply
) : (
Loading...
)}
); } export default Thread;