import { createSelector } from 'reselect' ; import { IDesk } from 'superdesk-api' ; import { IApplicationState } from '../index' ; import { CONTENT_PANEL_STATE , ISetItem , IStorageDestinationItem , SET_STATE , } from '../../interfaces' ; import { getStorageDestinationsById } from '../storageDestinations/selectors' ; import { getCurrentDeskId , getDesksAllowedSets } from '../workspace/selectors' ; import { assertNever } from '../../utils/typescript' ; type ISetArrays = { draft : Array < ISetItem > ; usable : Array < ISetItem > ; disabled : Array < ISetItem > ; } ; export function getSets ( state ) { return state . sets . sets ; } export function getActiveSets ( state ) { return state . sets . sets . filter ( ( set ) => set . state === SET_STATE . USABLE ) ; } export function getDisabledSets ( state ) { return state . sets . sets . filter ( ( set ) => set . state === SET_STATE . DISABLED ) ; } export function getDisabledSetIds ( state ) { const disabledSets = getDisabledSets ( state ) ; let disabledSetIds = [ ] ; disabledSets . forEach ( ( set ) => { disabledSetIds . push ( set . _id ) ; } ) ; return disabledSetIds ; } export const getSetsById = createSelector < IApplicationState , Array < ISetItem > , Dictionary < string , ISetItem > > ( [ getSets ] , ( sets ) => { return sets . reduce < Dictionary < string , ISetItem > > ( ( items , set ) => { items [ set . _id ] = set ; return items ; } , { } ) ; } ) ; export function getSetContentPanelState ( state ) { return state . sets . contentPanelState ?? CONTENT_PANEL_STATE . CLOSED ; } export function getSelectedSetId ( state ) { return state . sets . selectedSetId ; } export const getSelectedSet = createSelector < IApplicationState , Dictionary < string , ISetItem > , string | undefined , ISetItem | undefined > ( [ getSetsById , getSelectedSetId ] , ( sets , setId ? ) => ( setId != null ? sets ?. [ setId ] : undefined ) ) ; export const getSelectedSetStorageDestination = createSelector < IApplicationState , ISetItem | undefined , Dictionary < string , IStorageDestinationItem > , IStorageDestinationItem | undefined > ( [ getSelectedSet , getStorageDestinationsById ] , ( set , destinations ) => ( set ?. destination_name != null ? destinations ?. [ set ?. destination_name ] : undefined ) ) ; export const getSetsGroupedByState = createSelector < IApplicationState , Array < ISetItem > , ISetArrays > ( [ getSets ] , ( sets ) => { const groupedSets = { draft : [ ] , usable : [ ] , disabled : [ ] , } ; sets . forEach ( ( set ) => { switch ( set . state ) { case SET_STATE . USABLE : groupedSets . usable . push ( set ) ; break ; case SET_STATE . DISABLED : groupedSets . disabled . push ( set ) ; break ; case SET_STATE . DRAFT : groupedSets . draft . push ( set ) ; break ; default : assertNever ( set . state ) ; } } ) ; return groupedSets ; } ) ; export function getAssetsCountForSets ( state ) { return state . sets . counts ; } export const getSelectedSetCount = createSelector < IApplicationState , Dictionary < string , number > , string | undefined , number | undefined > ( [ getAssetsCountForSets , getSelectedSetId ] , ( counts , setId ? ) => ( setId != null ? counts ?. [ setId ] : undefined ) ) ; export const getAvailableSetsForDesk = createSelector < IApplicationState , IDesk [ '_id' ] | null , Dictionary < ISetItem [ '_id' ] , ISetItem > , Dictionary < ISetItem [ '_id' ] , Array < IDesk [ '_id' ] > > , Array < ISetItem [ '_id' ] > > ( [ getCurrentDeskId , getSetsById , getDesksAllowedSets ] , ( currentDeskId , sets , deskRestrictions ) => { return Object . keys ( sets ) . filter ( ( setId ) => ( currentDeskId == null || deskRestrictions [ setId ] == null || deskRestrictions [ setId ] . includes ( currentDeskId ) ) ) ; } ) ;