From f0fa5e6945f7567ba38e85de650f1225393d2acf Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 8 Jul 2021 15:30:18 -0500 Subject: [PATCH] Timelines: truncate on dequeue if scrolled to the top --- app/soapbox/reducers/timelines.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/soapbox/reducers/timelines.js b/app/soapbox/reducers/timelines.js index 444940ba1..8ca050646 100644 --- a/app/soapbox/reducers/timelines.js +++ b/app/soapbox/reducers/timelines.js @@ -25,6 +25,9 @@ import { } from 'immutable'; import { GROUP_REMOVE_STATUS_SUCCESS } from '../actions/groups'; +const TRUNCATE_LIMIT = 40; +const TRUNCATE_SIZE = 20; + const initialState = ImmutableMap(); const initialTimeline = ImmutableMap({ @@ -55,6 +58,8 @@ const truncate = (items, truncateLimit, newSize) => ( items.size > truncateLimit ? items.take(newSize) : items ); +const truncateIds = items => truncate(items, TRUNCATE_LIMIT, TRUNCATE_SIZE); + const setLoading = (state, timelineId, loading) => { return state.update(timelineId, initialTimeline, timeline => timeline.set('isLoading', loading)); }; @@ -98,7 +103,7 @@ const updateTimeline = (state, timelineId, statusId) => { return state.update(timelineId, initialTimeline, timeline => timeline.withMutations(timeline => { if (top) { // For performance, truncate items if user is scrolled to the top - timeline.set('items', truncate(newIds, 40, 20)); + timeline.set('items', truncateIds(newIds)); } else { timeline.set('unread', unread + 1); timeline.set('items', newIds); @@ -184,9 +189,16 @@ const removeStatusFromGroup = (state, groupId, statusId) => { }; const timelineDequeue = (state, timelineId) => { + const top = state.getIn([timelineId, 'top']); + return state.update(timelineId, initialTimeline, timeline => timeline.withMutations(timeline => { const queuedIds = timeline.get('queuedItems'); - timeline.update('items', ids => mergeStatusIds(ids, queuedIds)); + + timeline.update('items', ids => { + const newIds = mergeStatusIds(ids, queuedIds); + return top ? truncateIds(newIds) : newIds; + }); + timeline.set('queuedItems', ImmutableOrderedSet()); timeline.set('totalQueuedItemsCount', 0); }));