/** * External dependencies */ import { map, values } from 'lodash'; import { EMPTY_ARRAY } from '@nelio-content/constants'; import { createIndexedSelector, isDefined } from '@nelio-content/utils'; import type { Maybe, PostId, SocialMessage, Uuid } from '@nelio-content/types'; /** * Internal dependencies */ import { filterOutInvalidAutoMessages } from './utils'; import type { State } from '../../config'; export function getSocialMessage( state: State, id?: Uuid ): Maybe< SocialMessage > { return id ? state.entities.messages.byId[ id ] : undefined; } export const getSocialMessagesRelatedToPost = createIndexedSelector( ( state: State, postId?: PostId ): ReadonlyArray< SocialMessage > => { const summaries = !! postId && state.entities.messages.byRelatedPost[ postId ]; if ( ! summaries ) { return EMPTY_ARRAY; } const validMessages = filterOutInvalidAutoMessages( state, summaries ).filter( ( i ) => i.type === 'social' ); return map( validMessages, ( { id } ) => getSocialMessage( state, id ) ).filter( isDefined ); }, ( state: State, postId?: PostId ) => ( { key: postId || 0, dependants: [ !! postId && state.entities.messages.byRelatedPost[ postId ], state.entities.messages.byId, state.social.profiles.byId, ], } ) ); export const getSocialMessageIdsRelatedToPost = createIndexedSelector( ( state: State, postId?: PostId ): ReadonlyArray< Uuid > => { return map( getSocialMessagesRelatedToPost( state, postId ), 'id' ); }, ( state: State, postId?: PostId ) => ( { key: postId || 0, dependants: [ getSocialMessagesRelatedToPost( state, postId ) ], } ) ); export const getRecurringMessages = createIndexedSelector( ( state: State, recurrenceGroup?: Uuid ): ReadonlyArray< SocialMessage > => { return recurrenceGroup ? values( state.entities.messages.byId ).filter( ( m ) => m.recurrenceGroup === recurrenceGroup ) : []; }, ( state: State, recurrenceGroup?: Uuid ) => ( { key: recurrenceGroup || 0, dependants: [ state.entities.messages.byId ], } ) );