diff --git a/lib/selectors/calendar-selectors.js b/lib/selectors/calendar-selectors.js index 029625914..38e37a92d 100644 --- a/lib/selectors/calendar-selectors.js +++ b/lib/selectors/calendar-selectors.js @@ -1,72 +1,75 @@ // @flow import * as React from 'react'; import { rawEntryInfoWithinActiveRange } from '../shared/entry-utils'; import SearchIndex from '../shared/search-index'; import { threadInFilterList } from '../shared/thread-utils'; import type { FilterThreadInfo } from '../types/filter-types'; import { values } from '../utils/objects'; import { useSelector } from '../utils/redux-utils'; import { currentCalendarQuery } from './nav-selectors'; import { threadInfoSelector } from './thread-selectors'; function useFilterThreadInfos( calendarActive: boolean, ): $ReadOnlyArray { const threadInfos = useSelector(threadInfoSelector); const rawEntryInfos = useSelector(state => state.entryStore.entryInfos); const calendarQueryFunc = useSelector(currentCalendarQuery); const calendarQuery = calendarQueryFunc(calendarActive); return React.useMemo(() => { const result: { [threadID: string]: FilterThreadInfo } = {}; for (const entryID in rawEntryInfos) { const rawEntryInfo = rawEntryInfos[entryID]; if (!rawEntryInfoWithinActiveRange(rawEntryInfo, calendarQuery)) { continue; } const threadID = rawEntryInfo.threadID; const threadInfo = threadInfos[rawEntryInfo.threadID]; if (!threadInFilterList(threadInfo)) { continue; } if (result[threadID]) { - result[threadID].numVisibleEntries++; + result[threadID] = { + ...result[threadID], + numVisibleEntries: result[threadID].numVisibleEntries + 1, + }; } else { result[threadID] = { threadInfo, numVisibleEntries: 1, }; } } for (const threadID in threadInfos) { const threadInfo = threadInfos[threadID]; if (!result[threadID] && threadInFilterList(threadInfo)) { result[threadID] = { threadInfo, numVisibleEntries: 0, }; } } return values(result).sort( (first: FilterThreadInfo, second: FilterThreadInfo) => second.numVisibleEntries - first.numVisibleEntries, ); }, [threadInfos, rawEntryInfos, calendarQuery]); } function useFilterThreadSearchIndex(calendarActive: boolean): SearchIndex { const threadInfos = useFilterThreadInfos(calendarActive); return React.useMemo(() => { const searchIndex = new SearchIndex(); for (const filterThreadInfo of threadInfos) { const { threadInfo } = filterThreadInfo; searchIndex.addEntry(threadInfo.id, threadInfo.uiName); } return searchIndex; }, [threadInfos]); } export { useFilterThreadInfos, useFilterThreadSearchIndex }; diff --git a/lib/types/filter-types.js b/lib/types/filter-types.js index 3af90fe64..c9b5306fa 100644 --- a/lib/types/filter-types.js +++ b/lib/types/filter-types.js @@ -1,34 +1,34 @@ // @flow import { type ThreadInfo } from './thread-types'; export const calendarThreadFilterTypes = Object.freeze({ THREAD_LIST: 'threads', NOT_DELETED: 'not_deleted', }); export type CalendarThreadFilterType = $Values< typeof calendarThreadFilterTypes, >; export type CalendarThreadFilter = { +type: 'threads', +threadIDs: $ReadOnlyArray, }; export type CalendarFilter = { +type: 'not_deleted' } | CalendarThreadFilter; export const defaultCalendarFilters: $ReadOnlyArray = [ { type: calendarThreadFilterTypes.NOT_DELETED }, ]; export const updateCalendarThreadFilter = 'UPDATE_CALENDAR_THREAD_FILTER'; export const clearCalendarThreadFilter = 'CLEAR_CALENDAR_THREAD_FILTER'; export const setCalendarDeletedFilter = 'SET_CALENDAR_DELETED_FILTER'; export type SetCalendarDeletedFilterPayload = { +includeDeleted: boolean, }; export type FilterThreadInfo = { - threadInfo: ThreadInfo, - numVisibleEntries: number, + +threadInfo: ThreadInfo, + +numVisibleEntries: number, };