Files
frontend-app-discussions/src/discussions/posts/data/hooks.js
Zameel Hassan f04429f6f7 fix: add null check for post objects in usePostList hook (#752)
Adds defensive null checks when accessing post properties in the posts
forEach loop to prevent potential errors in the MFE discussion sidebar.
This addresses the issue reported in #751.
2025-05-12 18:47:17 +05:00

28 lines
615 B
JavaScript

import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { selectThreadsByIds } from './selectors';
const usePostList = (ids) => {
const posts = useSelector(selectThreadsByIds(ids));
const pinnedPostsIds = [];
const unpinnedPostsIds = [];
const sortedIds = useMemo(() => {
posts.forEach((post) => {
if (post && post.pinned) {
pinnedPostsIds.push(post.id);
} else if (post) {
unpinnedPostsIds.push(post.id);
}
});
return [...pinnedPostsIds, ...unpinnedPostsIds];
}, [posts]);
return sortedIds;
};
export default usePostList;