import { createSelector } from 'reselect' ; import { SearchResult , SearchResults , HitBlockMap , RequesterBlockMap , SortingOption , AttributeWeights , RequesterId } from '../types' ; import { filterBelowTopticonThreshold , attributeWeightsSelector } from './turkopticon' ; import { sortBy } from '../utils/sorting' ; import { searchResultsSelector , hitBlocklistSelector , requesterBlocklistSelector , sortOptionSelector , markedAsReadGroupIdsSelector } from './index' ; import { noTurkopticon } from '../utils/turkopticon' ; import { List } from 'immutable' ; export const selectGroupId = ( hit ) => hit . groupId ; export const resultsLengthSelector = createSelector ( [ searchResultsSelector ] , ( searchResults ) => searchResults . size ) ; const hideBlockedHits = createSelector ( [ searchResultsSelector , hitBlocklistSelector ] , ( hits , blockedHits ) => hits . filter ( ( hit ) => ! blockedHits . has ( hit . groupId ) ) as SearchResults ) ; export const hideBlockedRequesters = createSelector ( [ searchResultsSelector , requesterBlocklistSelector ] , ( hits , blockedRequesters ) => hits . filter ( ( hit ) => ! blockedRequesters . has ( hit . requester . id ) ) as SearchResults ) ; const hideBlockedRequestersAndHits = createSelector ( [ hideBlockedHits , requesterBlocklistSelector ] , ( resultsFilteredByBlockedIds , blockedRequesters ) => resultsFilteredByBlockedIds . filter ( ( result ) => ! blockedRequesters . has ( result . requester . id ) ) as SearchResults ) ; const searchResultsFilteredByAboveThreshold = createSelector ( [ hideBlockedRequestersAndHits , filterBelowTopticonThreshold ] , ( hits , aboveThreshold ) => hits . filter ( ( hit ) => aboveThreshold . has ( hit . groupId ) ) ) ; const filteredAndSortedResults = createSelector ( [ searchResultsFilteredByAboveThreshold , sortOptionSelector , attributeWeightsSelector ] , ( filteredHits , sortingOption , weights ) => { const sortingFn = sortBy ( sortingOption , weights ) ; return filteredHits . sort ( sortingFn ) ; } ) ; export const newResults = createSelector ( [ filteredAndSortedResults , markedAsReadGroupIdsSelector ] , ( hits , seenHITs ) => hits . filterNot ( ( hit ) => seenHITs . has ( hit . groupId ) ) ) ; export const newResultsGroupIdsList = createSelector ( [ newResults ] , ( hits ) => hits . map ( selectGroupId ) . toList ( ) ) ; const markedAsReadResults = createSelector ( [ filteredAndSortedResults , markedAsReadGroupIdsSelector ] , ( hits , seenHITs ) => hits . filter ( ( hit ) => seenHITs . has ( hit . groupId ) ) ) ; const groupNewHitsBeforeOldHits = createSelector ( [ newResults , markedAsReadResults ] , ( hits , readHits ) => hits . concat ( readHits ) ) ; export const filteredResultsGroupIdSet = createSelector ( [ groupNewHitsBeforeOldHits ] , ( hits ) => { return hits . map ( selectGroupId ) . toSet ( ) ; } ) ; export const filteredResultsGroupIdList = createSelector ( [ groupNewHitsBeforeOldHits ] , ( hits ) => { return hits . map ( selectGroupId ) . toList ( ) ; } ) ; export const getSearchResultRequesterIds = createSelector ( [ searchResultsSelector ] , searchResults => searchResults . filter ( noTurkopticon ) . reduce ( ( acc , cur ) => acc . push ( cur . requester . id ) , List < RequesterId > ( [ ] ) ) ) ;