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.
28 lines
615 B
JavaScript
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;
|