fix popular posts

This commit is contained in:
Tom (plebeius.eth)
2024-10-12 17:09:23 +02:00
parent ba9496ca42
commit 088ec8428b
2 changed files with 53 additions and 33 deletions
+23 -3
View File
@@ -4,8 +4,14 @@ import { getCommentMediaInfo, getHasThumbnail } from '../lib/utils/media-utils';
const usePopularPosts = (subplebbits: Subplebbit[]) => { const usePopularPosts = (subplebbits: Subplebbit[]) => {
const [popularPosts, setPopularPosts] = useState<Comment[]>([]); const [popularPosts, setPopularPosts] = useState<Comment[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => { useEffect(() => {
const fetchPopularPosts = () => {
try {
setIsLoading(true);
setError(null);
const uniqueLinks: Set<string> = new Set(); const uniqueLinks: Set<string> = new Set();
const allPosts: Comment[] = []; const allPosts: Comment[] = [];
@@ -17,11 +23,13 @@ const usePopularPosts = (subplebbits: Subplebbit[]) => {
if (subplebbit?.posts?.pages?.hot?.comments) { if (subplebbit?.posts?.pages?.hot?.comments) {
for (const post of Object.values(subplebbit.posts.pages.hot.comments as Comment)) { for (const post of Object.values(subplebbit.posts.pages.hot.comments as Comment)) {
const { deleted, link, locked, pinned, removed, replyCount, timestamp } = post; const { deleted, link, locked, pinned, removed, replyCount, timestamp } = post;
try {
const commentMediaInfo = getCommentMediaInfo(post); const commentMediaInfo = getCommentMediaInfo(post);
const isMediaShowed = getHasThumbnail(commentMediaInfo, link); const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
if ( if (
isMediaShowed && hasThumbnail &&
(replyCount > 0 || postsPerSub > 1) && (replyCount > 0 || postsPerSub > 1) &&
!deleted && !deleted &&
!removed && !removed &&
@@ -33,6 +41,9 @@ const usePopularPosts = (subplebbits: Subplebbit[]) => {
subplebbitPosts.push(post); subplebbitPosts.push(post);
uniqueLinks.add(link); uniqueLinks.add(link);
} }
} catch (err) {
console.error('Error processing post:', err);
}
} }
subplebbitPosts.sort((a: any, b: any) => b.timestamp - a.timestamp); subplebbitPosts.sort((a: any, b: any) => b.timestamp - a.timestamp);
@@ -43,9 +54,18 @@ const usePopularPosts = (subplebbits: Subplebbit[]) => {
const sortedPosts = allPosts.sort((a: any, b: any) => b.timestamp - a.timestamp).slice(0, 8); const sortedPosts = allPosts.sort((a: any, b: any) => b.timestamp - a.timestamp).slice(0, 8);
setPopularPosts(sortedPosts); setPopularPosts(sortedPosts);
} catch (err) {
console.error('Error in usePopularPosts:', err);
setError('Failed to fetch popular posts');
} finally {
setIsLoading(false);
}
};
fetchPopularPosts();
}, [subplebbits]); }, [subplebbits]);
return popularPosts; return { popularPosts, isLoading, error };
}; };
export default usePopularPosts; export default usePopularPosts;
@@ -71,7 +71,7 @@ const PopularThreadsBox = ({ multisub, subplebbits }: { multisub: Subplebbit[];
}; };
const filteredSubplebbits = useMemo(getFilteredSubplebbits, [subplebbits, showWorksafeContentOnly, showNsfwContentOnly, multisub]); const filteredSubplebbits = useMemo(getFilteredSubplebbits, [subplebbits, showWorksafeContentOnly, showNsfwContentOnly, multisub]);
const popularPosts = usePopularPosts(filteredSubplebbits); const { popularPosts } = usePopularPosts(filteredSubplebbits);
return ( return (
<div className={styles.box}> <div className={styles.box}>
@@ -79,8 +79,8 @@ const PopularThreadsBox = ({ multisub, subplebbits }: { multisub: Subplebbit[];
<h2 className='capitalize'>{t('popular_threads')}</h2> <h2 className='capitalize'>{t('popular_threads')}</h2>
<BoxModal isBoardsBoxModal={false} /> <BoxModal isBoardsBoxModal={false} />
</div> </div>
<div className={`${styles.boxContent} ${popularPosts.length === 8 ? styles.popularThreads : ''}`}> <div className={`${styles.boxContent} ${popularPosts.length !== 0 ? styles.popularThreads : ''}`}>
{popularPosts.length < 8 ? ( {popularPosts.length === 0 ? (
<LoadingEllipsis string={t('loading')} /> <LoadingEllipsis string={t('loading')} />
) : ( ) : (
popularPosts.map((post: any) => ( popularPosts.map((post: any) => (