import { ColumnSubscription , constants , getItemsFromSubscriptions , } from '@devhub/core' ; import _ from 'lodash' ; import { createSelector } from 'reselect' ; import { EMPTY_ARRAY , EMPTY_OBJ } from '../../utils/constants' ; import { RootState } from '../types' ; import { currentUserPlanSelector } from './auth' ; import { columnsArrSelector } from './columns' ; import { betterMemoize , createShallowEqualSelector } from './helpers' ; const s = ( state ) => state . subscriptions || EMPTY_OBJ ; export const subscriptionIdsSelector = ( state ) => s ( state ) . allIds || EMPTY_ARRAY ; export const subscriptionsByIdSelector = ( state ) => s ( state ) . byId || EMPTY_OBJ ; export const subscriptionSelector = ( state , id ) => subscriptionsByIdSelector ( state ) [ id ] || undefined ; export const allSubscriptionsArrSelector = createShallowEqualSelector ( ( state ) => s ( state ) . byId , ( state ) => subscriptionIdsSelector ( state ) , ( byId , subscriptionIds ) : ColumnSubscription [ ] => { if ( ! ( byId && subscriptionIds && subscriptionIds . length ) ) return EMPTY_ARRAY ; return subscriptionIds . map ( ( id ) => byId [ id ] ) . filter ( Boolean ) as ColumnSubscription [ ] ; } ) ; export const userSubscriptionsArrSelector = createShallowEqualSelector ( ( state ) => currentUserPlanSelector ( state ) , ( state ) => columnsArrSelector ( state ) , ( state ) => subscriptionsByIdSelector ( state ) , ( plan , columns , byId ) : ColumnSubscription [ ] => { if ( ! ( plan && columns && columns . length && byId ) ) return EMPTY_ARRAY ; const limit = ( plan && plan . featureFlags . columnsLimit ) || constants . COLUMNS_LIMIT ; const validColumns = columns . filter ( ( c ) => c . subscriptionIds && c . subscriptionIds . length ) . slice ( 0 , limit ) ; const ids = _ . uniq ( validColumns . reduce < string [ ] > ( ( result , column ) => { return result . concat ( column . subscriptionIds ) ; } , [ ] ) ) ; return ids . map ( ( id ) => byId [ id ] ) . filter ( Boolean ) as ColumnSubscription [ ] ; } ) ; export const createSubscriptionsSelector = ( ) => createShallowEqualSelector ( ( _state , subscriptionIds ) => subscriptionIds || EMPTY_ARRAY , ( state , _subscriptionIds ) => subscriptionsByIdSelector ( state ) , ( subscriptionIds , byId ) => { return subscriptionIds . map ( ( id ) => byId [ id ] ) . filter ( Boolean ) as ColumnSubscription [ ] ; } ) ; export const createSubscriptionsDataSelector = ( ) => { const subscriptionsSelector = createSubscriptionsSelector ( ) ; const memoizedGetItemsFromSubscriptions = betterMemoize ( getItemsFromSubscriptions ) ; return createShallowEqualSelector ( ( state , subscriptionIds ) => subscriptionsSelector ( state , subscriptionIds ) , ( state , _subscriptionIds ) => state . data . byId , ( state , _subscriptionIds ) => state . data . idsBySubscriptionId , ( state , _subscriptionIds ) => state . data . savedIds , ( subscriptions , dataByNodeIdOrId , idsBySubscriptionId , savedIds ) => { const getItemByNodeIdOrId = ( nodeIdOrId ) => dataByNodeIdOrId ?. [ nodeIdOrId ] ?. item ; const savedIdsToInclude = Object . entries ( idsBySubscriptionId ) . reduce < string [ ] > ( ( result , [ subscriptionId , ids ] ) => { if ( ! subscriptions . map ( ( s ) => s . id ) . includes ( subscriptionId ) ) return result ; return result . concat ( ids . filter ( ( id ) => savedIds . includes ( id ) ) ) ; } , [ ] ) ; const result = memoizedGetItemsFromSubscriptions ( subscriptions , getItemByNodeIdOrId , savedIdsToInclude ) ; if ( ! ( result && result . length ) ) return EMPTY_ARRAY ; return result ; } ) ; } ; export const subscriptionLastFetchedAtSelector = createSelector ( ( state , subscriptionId ) => subscriptionSelector ( state , subscriptionId ) , ( subscription ) => { if ( ! ( subscription && subscription . data ) ) return ; return _ . max ( [ subscription . data . lastFetchRequestAt , subscription . data . lastFetchFailureAt , subscription . data . lastFetchSuccessAt , ] ) ; } ) ;