fix(home): fix rerender with useEffect dep

This commit is contained in:
plebeius.eth
2023-10-05 10:43:18 +02:00
parent fabec543bf
commit c63ade854b
+40 -35
View File
@@ -64,51 +64,56 @@ const Home = () => {
const sfwAddresses = defaultSubplebbits.map((subplebbit) => subplebbit.address); const sfwAddresses = defaultSubplebbits.map((subplebbit) => subplebbit.address);
const sfwSubs = useSubplebbits({ subplebbitAddresses: sfwAddresses }); const sfwSubs = useSubplebbits({ subplebbitAddresses: sfwAddresses });
const sfwList = sfwSubs.subplebbits.map((subplebbit) => subplebbit?.address); const sfwList = sfwSubs.subplebbits.map((subplebbit) => subplebbit?.address);
const subplebbitToCid = {}; const [sfwListCids, setSfwListCids] = useState([]);
sfwSubs.subplebbits.forEach((s) => { useEffect(() => {
let maxTimestamp = -Infinity; let subplebbitToCid = {};
let mostRecentCid = null;
if (s && s.posts && s.posts.pages && s.posts.pages.hot && s.posts.pages.hot.comments) { sfwSubs.subplebbits.forEach((s) => {
for (const comment of Object.values(s.posts.pages.hot.comments)) { let maxTimestamp = -Infinity;
const commentMediaInfo = getCommentMediaInfo(comment); let mostRecentCid = null;
const isMediaShowed =
comment.link &&
commentMediaInfo &&
(commentMediaInfo.type === 'image' ||
commentMediaInfo.type === 'video' ||
(commentMediaInfo.type === 'webpage' && commentMediaInfo.thumbnail) ||
(commentMediaInfo.type === 'iframe' && commentMediaInfo.thumbnail));
if ( if (s && s.posts && s.posts.pages && s.posts.pages.hot && s.posts.pages.hot.comments) {
isMediaShowed && for (const comment of Object.values(s.posts.pages.hot.comments)) {
comment.replyCount > 2 && const commentMediaInfo = getCommentMediaInfo(comment);
!comment.removed && const isMediaShowed =
!comment.locked && comment.link &&
!comment.pinned && commentMediaInfo &&
comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * 30 && (commentMediaInfo.type === 'image' ||
comment.timestamp > maxTimestamp commentMediaInfo.type === 'video' ||
) { (commentMediaInfo.type === 'webpage' && commentMediaInfo.thumbnail) ||
maxTimestamp = comment.timestamp; (commentMediaInfo.type === 'iframe' && commentMediaInfo.thumbnail));
mostRecentCid = comment.cid;
if (
isMediaShowed &&
comment.replyCount > 2 &&
!comment.removed &&
!comment.locked &&
!comment.pinned &&
comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * 30 &&
comment.timestamp > maxTimestamp
) {
maxTimestamp = comment.timestamp;
mostRecentCid = comment.cid;
}
}
if (mostRecentCid) {
subplebbitToCid[s.address] = { cid: mostRecentCid, timestamp: maxTimestamp };
} }
} }
});
if (mostRecentCid) { const newSfwListCids = Object.values(subplebbitToCid)
subplebbitToCid[s.address] = { cid: mostRecentCid, timestamp: maxTimestamp }; .sort((a, b) => b.timestamp - a.timestamp)
} .map((item) => item.cid)
} .slice(0, 8);
});
const sfwListCids = Object.values(subplebbitToCid) setSfwListCids(newSfwListCids);
.sort((a, b) => b.timestamp - a.timestamp) }, [sfwSubs.subplebbits]);
.map((item) => item.cid)
.slice(0, 8);
const account = useAccount(); const account = useAccount();
const navigate = useNavigate(); const navigate = useNavigate();
const isElectron = window.electron && window.electron.isElectron;
const inputRef = useRef(null); const inputRef = useRef(null);
const prevStyle = useRef(selectedStyle); const prevStyle = useRef(selectedStyle);