| import treeSelect from '@automattic/tree-select'; |
| import { filter, groupBy, keyBy, map, mapValues, partition } from 'lodash'; |
| import { getPostCommentItems } from 'calypso/state/comments/selectors/get-post-comment-items'; |
|
|
| import 'calypso/state/comments/init'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export const getPostCommentsTree = treeSelect( |
| ( state, siteId, postId ) => [ getPostCommentItems( state, siteId, postId ) ], |
| ( [ allItems ], siteId, postId, status = 'approved', authorId ) => { |
| const items = filter( allItems, ( item ) => { |
| |
| const commentAuthorId = item?.author?.ID; |
| if ( |
| authorId && |
| commentAuthorId && |
| item.status === 'unapproved' && |
| commentAuthorId !== authorId |
| ) { |
| return false; |
| } |
| if ( status !== 'all' ) { |
| return item.isPlaceholder || item.status === status; |
| } |
| return true; |
| } ); |
|
|
| |
| const [ roots, children ] = partition( items, ( item ) => item.parent === false ); |
|
|
| |
| const childrenGroupedByParent = groupBy( children, 'parent.ID' ); |
|
|
| |
| |
| const parentToChildIdMap = mapValues( childrenGroupedByParent, ( _children ) => |
| map( _children, 'ID' ).reverse() |
| ); |
|
|
| |
| const transformItemToNode = ( item ) => ( { |
| data: item, |
| children: parentToChildIdMap[ item.ID ] || [], |
| } ); |
|
|
| const commentsByIdMap = keyBy( map( items, transformItemToNode ), 'data.ID' ); |
|
|
| return { |
| ...commentsByIdMap, |
| children: map( roots, ( root ) => root.ID ).reverse(), |
| }; |
| } |
| ); |
|
|