- {subplebbits
+ {defaultSubplebbits
.filter((subplebbit: Subplebbit | undefined): subplebbit is Subplebbit => subplebbit !== undefined)
.map((subplebbit: Subplebbit) => {
const address = subplebbit.address;
@@ -105,15 +108,94 @@ const Boards = ({ subplebbits }: { subplebbits: (Subplebbit | undefined)[] }) =>
);
};
-const PopularThreads = () => {
+interface PopularThreadProps {
+ post: Comment;
+ boardTitle: string | undefined;
+ boardShortAddress: string;
+}
+
+const PopularThreadCard = ({ post, boardTitle, boardShortAddress }: PopularThreadProps) => {
+ const { cid, content, subplebbitAddress, title } = post || {};
+ const commentMediaInfo = getCommentMediaInfo(post);
+
+ return (
+
+
{boardTitle || boardShortAddress}
+
+
+
+
+
+
+ {title && {title}}
+ {content && (content.length > 99 ? `: ${content.substring(0, 99)}...` : `: ${content}`)}
+
+
+ );
+};
+
+const PopularThreads = ({ subplebbits }: { subplebbits: any }) => {
const { t } = useTranslation();
+ const [popularPosts, setPopularPosts] = useState([]);
+
+ useEffect(() => {
+ let subplebbitToPost: any = {};
+
+ subplebbits.forEach((subplebbit: any) => {
+ let mostRecentPost = null;
+
+ if (subplebbit?.posts?.pages?.hot?.comments) {
+ for (const comment of Object.values(subplebbit.posts.pages.hot.comments) as Comment[]) {
+ const { deleted, locked, pinned, removed, replyCount, timestamp } = comment;
+
+ const commentMediaInfo = getCommentMediaInfo(comment);
+ const isMediaShowed = getHasThumbnail(commentMediaInfo, comment.link);
+
+ if (
+ isMediaShowed &&
+ replyCount >= 2 &&
+ !removed &&
+ !deleted &&
+ !locked &&
+ !pinned && // criteria
+ timestamp > Date.now() / 1000 - 60 * 60 * 24 * 30 // 30 days
+ ) {
+ if (!mostRecentPost || comment.timestamp > mostRecentPost.timestamp) {
+ mostRecentPost = comment;
+ }
+ }
+
+ if (mostRecentPost) {
+ subplebbitToPost[subplebbit.address] = mostRecentPost;
+ }
+ }
+ }
+ });
+
+ const newPopularPosts: any = Object.values(subplebbitToPost)
+ .sort((a: any, b: any) => b.timestamp - a.timestamp)
+ .slice(0, 8);
+
+ setPopularPosts(newPopularPosts);
+ }, [subplebbits]);
+
return (
{t('popular_threads')}
{t('options')} ▼
-
+
+ {popularPosts.length < 8 ? (
+
+
+
+ ) : (
+ popularPosts.map((post: any) => (
+
+ ))
+ )}
+
);
};
@@ -219,6 +301,7 @@ const Footer = () => {
};
const Home = () => {
+ const defaultSubplebbits = useDefaultSubplebbits();
const subplebbitAddresses = useDefaultSubplebbitAddresses();
const { subplebbits } = useSubplebbits({ subplebbitAddresses });
@@ -231,8 +314,8 @@ const Home = () => {
-
-
+
+