Files
5chan/src/components/Board.jsx
T

461 lines
17 KiB
React
Raw Normal View History

2023-02-09 19:08:15 +01:00
import React, { useState, useEffect, useContext } from 'react';
2023-02-11 22:00:13 +01:00
import { Link, useNavigate } from 'react-router-dom';
2023-02-09 19:08:15 +01:00
import { BoardContext } from '../App';
2023-02-14 11:29:53 +01:00
import { Container, NavBar, Header, Break, PostFormLink, PostFormTable, PostForm, TopBar, BoardForm } from './styles/Board.styled';
import ImageBanner from './ImageBanner';
2023-02-15 16:48:21 +01:00
import { useFeed, useAccountsActions } from '@plebbit/plebbit-react-hooks';
2023-02-16 21:01:21 +01:00
import InfiniteScroll from 'react-infinite-scroller';
2023-02-14 21:13:15 +01:00
2023-02-05 18:44:16 +01:00
const Board = ({ setBodyStyle }) => {
2023-02-04 22:10:54 +01:00
const [defaultSubplebbits, setDefaultSubplebbits] = useState([]);
const { selectedTitle, setSelectedTitle, selectedAddress, setSelectedAddress, selectedThread, setSelectedThread, selectedStyle, setSelectedStyle } = useContext(BoardContext);
2023-02-11 22:00:13 +01:00
const [showPostFormLink, setShowPostFormLink] = useState(true);
const [showPostForm, setShowPostForm] = useState(false);
2023-02-15 16:48:21 +01:00
const [name, setName] = useState('');
const [subject, setSubject] = useState('');
const [comment, setComment] = useState('');
2023-02-11 22:00:13 +01:00
const navigate = useNavigate();
2023-02-14 11:29:53 +01:00
const { feed, hasMore, loadMore } = useFeed([`${selectedAddress}`], 'new');
2023-02-16 21:01:21 +01:00
2023-02-19 16:48:46 +01:00
// console.log(feed);
2023-02-17 18:03:05 +01:00
2023-02-16 21:01:21 +01:00
const tryLoadMore = async () => {
try {loadMore()}
catch (e)
{await new Promise(resolve => setTimeout(resolve, 1000))}
}
2023-02-15 16:48:21 +01:00
const { publishComment } = useAccountsActions();
2023-02-04 22:10:54 +01:00
2023-02-15 16:48:21 +01:00
const onChallengeVerification = (challengeVerification) => {
if (challengeVerification.challengeSuccess === true) {
console.log('challenge success', {publishedCid: challengeVerification.publication.cid})
}
else if (challengeVerification.challengeSuccess === false) {
console.error('challenge failed', {reason: challengeVerification.reason, errors: challengeVerification.errors});
alert("Error: You seem to have mistyped the CAPTCHA. Please try again.");
}
}
const onChallenge = async (challenges, comment) => {
let challengeAnswers = [];
try {
challengeAnswers = await getChallengeAnswersFromUser(challenges)
}
catch (error) {
console.log(error);
}
if (challengeAnswers) {
await comment.publishChallengeAnswers(challengeAnswers)
}
}
const onError = (error) => console.error(error)
const getChallengeAnswersFromUser = async (challenges) => {
return new Promise((resolve, reject) => {
const imageString = challenges?.challenges[0].challenge;
const imageSource = `data:image/png;base64,${imageString}`;
const challengeImg = new Image();
challengeImg.src = imageSource;
challengeImg.onload = () => {
const inputEl = document.getElementById('t-resp');
const cntEl = document.getElementById('t-cnt');
cntEl.appendChild(challengeImg);
inputEl.focus();
const handleKeyDown = (event) => {
if (event.key === 'Enter') {
const challengeResponse = inputEl.value;
inputEl.value = '';
if (cntEl.contains(challengeImg)) {
cntEl.removeChild(challengeImg);
}
document.removeEventListener('keydown', handleKeyDown);
resolve(challengeResponse);
}
};
document.addEventListener('keydown', handleKeyDown);
};
challengeImg.onerror = () => {
reject(new Error('Could not load challenge image'));
};
});
};
2023-02-12 21:20:08 +01:00
2023-02-04 22:10:54 +01:00
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;
};
}, []);
2023-02-12 21:20:08 +01:00
const handleVoidClick = () => {}
2023-02-07 22:55:00 +01:00
const handleClick = (title, address) => {
setSelectedTitle(title);
setSelectedAddress(address);
};
2023-02-11 22:00:13 +01:00
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.");
2023-02-11 22:00:13 +01:00
};
const handleClickForm = () => {
setShowPostFormLink(false);
setShowPostForm(true);
2023-02-19 22:08:18 +01:00
navigate(`/${selectedAddress}/post`);
2023-02-11 22:00:13 +01:00
};
const handleClickThread = (thread) => {
setSelectedThread(thread);
}
2023-02-15 16:48:21 +01:00
const handlePublishComment = async () => {
// Event.preventDefault();
try {
const pendingComment = await publishComment({
content: comment,
title: subject,
subplebbitAddress: selectedAddress,
onChallengeVerification,
onChallenge,
onError,
});
console.log(`Comment pending with index: ${pendingComment.index}`);
setName('');
setSubject('');
setComment('');
} catch (error) {
console.error(error);
}
};
2023-02-11 22:00:13 +01:00
2023-02-08 21:49:36 +01:00
const handleStyleChange = (event) => {
switch (event.target.value) {
case "Yotsuba":
setBodyStyle({
2023-02-16 14:05:48 +01:00
background: "#ffe url(/assets/fade.png) top repeat-x",
2023-02-08 21:49:36 +01:00
color: "maroon",
fontFamily: "Arial, Helvetica, sans-serif"
});
setSelectedStyle("Yotsuba");
break;
case "Yotsuba B":
setBodyStyle({
2023-02-16 14:05:48 +01:00
background: "#eef2ff url(/assets/fade-blue.png) top center repeat-x",
2023-02-08 21:49:36 +01:00
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({
2023-02-16 14:05:48 +01:00
background: "#ffe url(/assets/fade.png) top repeat-x",
2023-02-08 21:49:36 +01:00
color: "maroon",
fontFamily: "Arial, Helvetica, sans-serif"
});
setSelectedStyle("Yotsuba");
}
}
2023-02-18 16:05:03 +01:00
function renderComments(comments) {
return comments.map(comment => {
const { replyCount, replies: { pages: { topAll: { comments: nestedComments } } } } = comment;
if (replyCount > 0 && nestedComments.length > 0) {
2023-02-19 16:48:46 +01:00
const renderedNestedComments = renderComments(nestedComments);
2023-02-18 16:05:03 +01:00
return [comment, ...renderedNestedComments];
}
return [comment];
}).flat();
}
2023-02-02 21:58:02 +01:00
return (
2023-02-04 22:10:54 +01:00
<Container>
2023-02-08 21:49:36 +01:00
<NavBar selectedStyle={selectedStyle}>
2023-02-04 22:10:54 +01:00
<>
{defaultSubplebbits.map(subplebbit => (
2023-02-11 22:00:13 +01:00
<span className="boardList" key={`span-${subplebbit.address}`}>
2023-02-07 22:55:00 +01:00
[
2023-02-19 22:08:18 +01:00
<Link to={`/${subplebbit.address}`} key={`a-${subplebbit.address}`} onClick={() => handleClick(subplebbit.title, subplebbit.address)}
>{subplebbit.title}</Link>
2023-02-04 22:10:54 +01:00
]&nbsp;
</span>
))}
2023-02-23 16:45:29 +01:00
<span className="nav">
[
<a href={handleVoidClick}>Settings</a>
]
[
<Link to="/" onClick={() => handleStyleChange({target: {value: "Yotsuba"}}
)}>Home</Link>
]
2023-02-04 22:10:54 +01:00
</span>
2023-02-23 16:45:29 +01:00
<div id="board-nav-mobile">
<div className="board-select">
<strong>Board</strong>
&nbsp;
<select id="board-select-mobile">
{defaultSubplebbits.map(subplebbit => (
<option key={`option-${subplebbit.address}`} value={subplebbit.address} onClick={() => handleClick(subplebbit.title, subplebbit.address)}>{subplebbit.title}</option>))}
</select>
</div>
<div className="page-jump">
<a href={handleVoidClick}>Settings</a>
&nbsp;
<Link to="/" onClick={() => handleStyleChange({target: {value: "Yotsuba"}}
)}>Home</Link>
</div>
</div>
<div id="separator-mobile">&nbsp;</div>
<div id="separator-mobile">&nbsp;</div>
2023-02-04 22:10:54 +01:00
</>
</NavBar>
2023-02-08 21:49:36 +01:00
<Header selectedStyle={selectedStyle}>
2023-02-04 22:10:54 +01:00
<>
<div className="banner">
2023-02-05 18:44:16 +01:00
<ImageBanner />
2023-02-04 22:10:54 +01:00
</div>
2023-02-07 13:23:14 +01:00
<>
2023-02-07 22:55:00 +01:00
<div className="board-title">{selectedTitle}</div>
<div className="board-address">p/{selectedAddress}</div>
2023-02-07 13:23:14 +01:00
</>
2023-02-04 22:10:54 +01:00
</>
</Header>
2023-02-08 21:49:36 +01:00
<Break selectedStyle={selectedStyle} />
2023-02-15 16:48:21 +01:00
<PostForm selectedStyle={selectedStyle}>
2023-02-11 22:00:13 +01:00
<PostFormLink id="post-form-link" showPostFormLink={showPostFormLink} >
2023-02-04 22:10:54 +01:00
[
2023-02-21 14:49:00 +01:00
<a onClick={handleClickForm} onMouseOver={(event) => event.target.style.cursor='pointer'}>Start a New Thread</a>
2023-02-04 22:10:54 +01:00
]
2023-02-11 22:00:13 +01:00
</PostFormLink>
<PostFormTable id="post-form" showPostForm={showPostForm} selectedStyle={selectedStyle} className="post-form">
<tbody>
<tr data-type="Name">
<td id="td-name">Name</td>
<td>
2023-02-15 16:48:21 +01:00
<input name="name" type="text" tabIndex={1} placeholder="Anonymous" value={name} onChange={(event) => setName(event.target.value)} />
2023-02-11 22:00:13 +01:00
</td>
</tr>
<tr data-type="Subject">
<td>Subject</td>
<td>
2023-02-15 16:48:21 +01:00
<input name="sub" type="text" tabIndex={3} value={subject} onChange={(event) => setSubject(event.target.value)} />
<input id="post-button" type="submit" value="Post" tabIndex={6} onClick={handlePublishComment} />
2023-02-11 22:00:13 +01:00
</td>
</tr>
<tr data-type="Comment">
<td>Comment</td>
<td>
2023-02-15 16:48:21 +01:00
<textarea name="com" cols="48" rows="4" tabIndex={4} wrap="soft" value={comment} onChange={(event) => setComment(event.target.value)}></textarea>
2023-02-11 22:00:13 +01:00
</td>
</tr>
<tr id="captchaFormPart">
<td>Verification</td>
<td colSpan={2}>
<div id="t-root">
2023-02-15 16:48:21 +01:00
<input id="t-resp" name="t-response" placeholder="Type the CAPTCHA here and hit return" autoComplete='off' type="text" />
2023-02-11 22:00:13 +01:00
<button id="t-help" type="button" onClick={handleClickHelp} data-tip="Help" tabIndex={-1}>?</button>
<div id="t-cnt">
</div>
</div>
</td>
</tr>
<tr data-type="File">
<td>Embed File</td>
<td>
<input name="embed" type="text" tabIndex={7} placeholder="Paste link" />
</td>
</tr>
<tr></tr>
</tbody>
</PostFormTable>
2023-02-04 22:10:54 +01:00
</PostForm>
2023-02-08 21:49:36 +01:00
<TopBar selectedStyle={selectedStyle}>
2023-02-05 18:44:16 +01:00
<hr />
2023-02-07 13:23:14 +01:00
<span className="style-changer">
Style:
 
2023-02-13 12:05:10 +01:00
<select id="style-selector" onChange={handleStyleChange} value={selectedStyle}>
2023-02-08 21:49:36 +01:00
<option value="Yotsuba">Yotsuba</option>
2023-02-07 13:23:14 +01:00
<option value="Yotsuba B">Yotsuba B</option>
2023-02-08 21:49:36 +01:00
<option value="Futaba">Futaba</option>
2023-02-07 13:23:14 +01:00
<option value="Burichan">Burichan</option>
<option value="Tomorrow">Tomorrow</option>
<option value="Photon">Photon</option>
</select>
</span>
2023-02-19 16:48:46 +01:00
[
2023-02-19 22:08:18 +01:00
<Link to={`/${selectedAddress}/catalog`}>Catalog</Link>
2023-02-19 16:48:46 +01:00
]
2023-02-05 18:44:16 +01:00
</TopBar>
2023-02-15 16:48:21 +01:00
<BoardForm selectedStyle={selectedStyle}>
<div className="board">
2023-02-16 21:01:21 +01:00
<InfiniteScroll
2023-02-14 11:29:53 +01:00
pageStart={0}
2023-02-16 21:01:21 +01:00
loadMore={tryLoadMore}
2023-02-14 11:29:53 +01:00
hasMore={hasMore}
loader={<div>Loading...</div>}
2023-02-16 21:01:21 +01:00
>
{feed.map(thread => {
let counter = 1;
const { replies: { pages: { topAll: { comments } } } } = thread;
2023-02-19 16:48:46 +01:00
const renderedComments = renderComments(comments);
return (
<>
<div key={`t-${thread.cid}`} className="thread">
<div key={`c-${thread.cid}`} className="post-container op-container">
<div key={`po-${thread.cid}`} className="post op">
2023-02-22 16:31:45 +01:00
<hr key={`hr-${thread.cid}`} />
<div key={`pi-${thread.cid}`} className="post-info">
2023-02-22 16:31:45 +01:00
<div key={`f-${thread.cid}`} className="file">
<div key={`ft-${thread.cid}`} className="file-text">
File:&nbsp;
<a key={`fa-${thread.cid}`} href={`${thread.link}`} target="_blank">filename.something</a>&nbsp;(metadata)
</div>
2023-02-23 16:45:29 +01:00
<a key={`fta-${thread.cid}`} href={handleVoidClick} target="_blank" className="file-thumb">
2023-02-22 16:31:45 +01:00
<img key={`fti-${thread.cid}`} src="/assets/plebchan-psycho.png" alt="filename.something" />
</a>
</div>
<span key={`nb-${thread.cid}`} className="name-block">
2023-02-22 16:31:45 +01:00
<span key={`q-${thread.cid}`} className="title">
{thread.title ? `${thread.title}` : null}&nbsp;
</span>
2023-02-17 18:03:05 +01:00
<span key={`n-${thread.cid}`} className="name">{thread.author.displayName || "Anonymous"}</span>
&nbsp;
<span key={`pa-${thread.cid}`} className="poster-address">
(User: {thread.author.address})
</span>
2023-02-22 16:31:45 +01:00
&nbsp;
<span key={`dt-${thread.cid}`} className="date-time" data-utc="data">2 weeks ago</span>
&nbsp;
<span key={`pn-${thread.cid}`} className="post-number">
<a key={`pl1-${thread.cid}`} href={handleVoidClick} title="Link to this post">No.</a>
<a key={`pl2-${thread.cid}`} href={handleVoidClick} title="Reply to this post">00000001</a>
&nbsp;
<span key={`rl1-${thread.cid}`}>
[
<Link key={`rl2-${thread.cid}`} to={`/${selectedAddress}/thread/${thread.cid}`} onClick={() => handleClickThread(thread.cid)} className="reply-link" >Reply</Link>
]
</span>
2023-02-07 22:55:00 +01:00
</span>
2023-02-22 16:31:45 +01:00
<a key={`pmb-${thread.cid}`} className="post-menu-button" href={handleVoidClick} title="Post menu" data-cmd="post-menu"></a>
<div key={`bi-${thread.cid}`} id="backlink-id" className="backlink">
<span key={`ql1-${thread.cid}`}>
<a key={`ql2-${thread.cid}`} className="quote-link" href={handleVoidClick}>{'>>'}00000002</a>
</span>
</div>
</span>
<blockquote key={`bq-${thread.cid}`}>
2023-02-17 18:03:05 +01:00
{thread.content ? (
<>
{thread.content}
</>
) : null}
2023-02-07 22:55:00 +01:00
</blockquote>
2023-02-05 22:15:38 +01:00
</div>
2023-02-07 22:55:00 +01:00
</div>
</div>
2023-02-18 16:05:03 +01:00
{renderedComments.map(reply => {
counter++;
2023-02-18 16:05:03 +01:00
const counterString = counter.toString().padStart(8, '0');
return (
<div key={`pc-${reply.cid}`} className="post-container reply-container">
<div key={`sa-${reply.cid}`} className="side-arrows">{'>>'}</div>
<div key={`pr-${reply.cid}`} className="post-reply">
<div key={`pi-${reply.cid}`} className="post-info">
<span key={`nb-${reply.cid}`} className="nameblock">
2023-02-17 18:03:05 +01:00
<span key={`n-${reply.cid}`} className="name">{reply.author.displayName || "Anonymous"}</span>
&nbsp;
<span key={`pa-${reply.cid}`} className="poster-address">
(User: {reply.author.address})
</span>
</span>
&nbsp;
<span key={`dt-${reply.cid}`} className="date-time" data-utc="data">2 weeks ago</span>
&nbsp;
<span key={`pn-${reply.cid}`} className="post-number">
2023-02-12 21:20:08 +01:00
<a key={`pl1-${reply.cid}`} href={handleVoidClick} title="Link to this post">No.</a>
2023-02-18 16:05:03 +01:00
<a key={`pl2-${reply.cid}`} href={handleVoidClick} title="Reply to this post">{counterString}</a>
</span>
2023-02-12 21:20:08 +01:00
<a key={`pmb-${reply.cid}`} className="post-menu-button" href={handleVoidClick} title="Post menu" data-cmd="post-menu"></a>
</div>
<blockquote key={`pm-${reply.cid}`} className="post-message">
2023-02-19 16:48:46 +01:00
<a className="quotelink" href={handleVoidClick}>
{`>>${counterString}`}{<br />}
2023-02-18 16:05:03 +01:00
</a>
{reply.content}
</blockquote>
</div>
</div>
2023-02-10 22:45:50 +01:00
)})}
</div>
</>
)})}
2023-02-16 21:01:21 +01:00
</InfiniteScroll>
</div>
2023-02-05 22:15:38 +01:00
</BoardForm>
2023-02-04 22:10:54 +01:00
</Container>
2023-02-07 22:55:00 +01:00
);
2023-01-31 23:08:57 +01:00
}
export default Board;