import React, { useState, useEffect, useContext } from 'react'; import { Link, useNavigate, useParams, useLocation } 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'; import getDate from '../utils/getDate'; import renderThreadComments from '../utils/renderThreadComments'; import { useCookies } from 'react-cookie'; const Thread = ({ setBodyStyle }) => { const [defaultSubplebbits, setDefaultSubplebbits] = useState([]); const { selectedTitle, setSelectedTitle, selectedAddress, setSelectedAddress, selectedThread, setSelectedThread, selectedStyle, setSelectedStyle } = useContext(BoardContext); const [showPostFormLink, setShowPostFormLink] = useState(true); const [showPostForm, setShowPostForm] = useState(false); const navigate = useNavigate(); const location = useLocation(); const [prevScrollPos, setPrevScrollPos] = useState(0); const [visible, setVisible] = useState(true); const comment = useComment(`${selectedThread}`); const { subplebbitAddress, threadCid } = useParams(); const [cookies, setCookie] = useCookies(['selectedStyle']); // temporary title from JSON, gets subplebbitAddress and threadCid from URL useEffect(() => { setSelectedAddress(subplebbitAddress); setSelectedThread(threadCid); const selectedSubplebbit = defaultSubplebbits.find((subplebbit) => subplebbit.address === subplebbitAddress); if (selectedSubplebbit) { setSelectedTitle(selectedSubplebbit.title); } }, [subplebbitAddress, setSelectedAddress, setSelectedTitle, defaultSubplebbits]); // fetches default subplebbits from JSON 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; }; }, []); // mobile navbar scroll effect 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]); // post route handling useEffect(() => { const path = location.pathname; if (path.endsWith('/post')) { setShowPostFormLink(false); setShowPostForm(true); } else { setShowPostFormLink(true); setShowPostForm(false); } }, [location.pathname]); const handleVoidClick = () => {} // desktop navbar board select functionality const handleClickTitle = (title, address) => { setSelectedTitle(title); setSelectedAddress(address); }; // mobile navbar board select functionality 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 = () => { setShowPostFormLink(false); setShowPostForm(true); navigate(`/${selectedAddress}/thread/${selectedThread}/post`); }; const handleClickTop = () => { window.scrollTo(0, 0); } const handleClickBottom = () => { window.scrollTo(0, document.body.scrollHeight); } // scroll to post when quote is clicked function handleQuoteClick(reply, event) { event.preventDefault(); const cid = reply.cid.slice(0, 8); const targetElement = [...document.querySelectorAll('.post-reply')] .find(el => el.innerHTML.includes(cid)); if (targetElement) { targetElement.scrollIntoView({ behavior: "instant" }); } } 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"); setCookie("selectedStyle", "Yotsuba", { path: "/" }); 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"); setCookie("selectedStyle", "Yotsuba-B", { path: "/", sameSite: 'none', secure: true }); break; case "Futaba": setBodyStyle({ background: "#ffe", color: "maroon", fontFamily: "times new roman, serif" }); setSelectedStyle("Futaba"); setCookie("selectedStyle", "Futaba", { path: "/", sameSite: 'none', secure: true }); break; case "Burichan": setBodyStyle({ background: "#eef2ff", color: "#000", fontFamily: "times new roman, serif" }); setSelectedStyle("Burichan"); setCookie("selectedStyle", "Burichan", { path: "/", sameSite: 'none', secure: true }); break; case "Tomorrow": setBodyStyle({ background: "#1d1f21 none", color: "#c5c8c6", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Tomorrow"); setCookie("selectedStyle", "Tomorrow", { path: "/", sameSite: 'none', secure: true }); break; case "Photon": setBodyStyle({ background: "#eee none", color: "#333", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Photon"); setCookie("selectedStyle", "Photon", { path: "/", sameSite: 'none', secure: true }); break; default: setBodyStyle({ background: "#ffe url(/assets/fade.png) top repeat-x", color: "maroon", fontFamily: "Arial, Helvetica, sans-serif" }); setSelectedStyle("Yotsuba"); setCookie("selectedStyle", "Yotsuba", { path: "/", sameSite: 'none', secure: true }); } } function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(";").shift(); } useEffect(() => { const style = getCookie("selectedStyle"); if (style) { handleStyleChange({ target: { value: style } }); } }, []); 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}     (u/ {comment.author.address.length > 20 ? <> {comment.author.address.slice(0, 20) + "..."} : {comment.author.address} })   {getDate(comment.timestamp)}   c/ {comment.cid.slice(0, 8)}
{comment.content}
{comment.replyCount > 0 && Object.keys(comment.replies.pages.topAll.comments).map(() => { const renderedComments = renderThreadComments(comment.replies.pages.topAll.comments); return renderedComments.map(reply => { return (
{'>>'}
  {reply.author.displayName ? reply.author.displayName.length > 20 ? <> {reply.author.displayName.slice(0, 20) + " (...)"} : {reply.author.displayName} : Anonymous}   (u/ {reply.author.address.length > 20 ? <> {reply.author.address.slice(0, 20) + "..."} : {reply.author.address} })   {getDate(reply.timestamp)}   c/ {reply.cid.slice(0, 8)}
handleQuoteClick(reply, event)}> {`c/${reply.parentCid.slice(0, 8)}`}{
} {reply.content}
) }) })}

... {comment.author.displayName ? comment.author.displayName.length > 15 ? <> {comment.author.displayName.slice(0, 15) + " (...)"} : {comment.author.displayName} : Anonymous}   (u/ {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}
{getDate(comment.timestamp)}   c/ {comment.cid.slice(0, 8)}
58 KB JPG
{comment.content ? ( <> {comment.content} ) : null}
{comment.replyCount > 0 && Object.keys(comment.replies.pages.topAll.comments).map(() => { const renderedComments = renderThreadComments(comment.replies.pages.topAll.comments); return renderedComments.map(reply => { return (
... {reply.author.displayName ? reply.author.displayName.length > 12 ? <> {reply.author.displayName.slice(0, 12) + " (...)"} : {reply.author.displayName} : Anonymous}   (u/ {reply.author.address.length > 12 ? <> {reply.author.address.slice(0, 12) + "..."} : {reply.author.address} } )
{getDate(reply.timestamp)}  c/ {reply.cid.slice(0, 8)}
handleQuoteClick(reply, event)}> {`c/${reply.parentCid.slice(0, 8)}`}{
} {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;