import { Installation } from '@devhub/core' ; import _ from 'lodash' ; import { createSelector } from 'reselect' ; import { EMPTY_ARRAY , EMPTY_OBJ } from '../../../utils/constants' ; import { RootState } from '../../types' ; const s = ( state ) => ( state . github && state . github . installations ) || EMPTY_OBJ ; export const installationIdsSelector = ( state ) => s ( state ) . allIds || EMPTY_ARRAY ; export const installationOwnerNamesSelector = ( state ) => s ( state ) . allOwnerNames || EMPTY_ARRAY ; export const installationsLastFetchedAtSelector = ( state ) => _ . max ( [ s ( state ) . lastFetchRequestAt , s ( state ) . lastFetchFailureAt , s ( state ) . lastFetchSuccessAt , ] ) ; export const installationsLoadStateSelector = ( state ) => s ( state ) . loadState ; export const installationSelector = ( state , id ) => s ( state ) . byId && s ( state ) . byId [ id ] ; export const installationsArrSelector = createSelector ( ( state ) => installationIdsSelector ( state ) , ( state ) => s ( state ) . byId , ( ids , byId ) => ( byId && ids ? ids . map ( ( id ) => byId [ id ] ) . filter ( Boolean ) : EMPTY_ARRAY ) as Installation [ ] ) ; export const installationByOwnerSelector = createSelector ( ( state ) => s ( state ) . byId , ( state ) => s ( state ) . byOwnerName , ( _state , ownerName ) => ownerName , ( byId , byOwnerName , _ownerName ) => { const ownerName = `${ _ownerName || '' }` . trim ( ) . toLowerCase ( ) ; const installationId = ownerName && byOwnerName [ ownerName ] ; return ( installationId && byId [ installationId ] ) || undefined ; } ) ; export const installationTokenByOwnerSelector = ( state , ownerName ) => { const installation = installationByOwnerSelector ( state , ownerName ) ; const tokenDetails = installation && installation . tokenDetails ; return ( tokenDetails && tokenDetails . token ) || undefined ; } ; export const installationTokenByRepoSelector = ( state , ownerName , _repoName ) => { return installationTokenByOwnerSelector ( state , ownerName ) ; } ;