From 0a0fabd2ca66b6a2d023a30daea7698ed36b61fd Mon Sep 17 00:00:00 2001 From: Josh Perez <60019601+josh-signal@users.noreply.github.com> Date: Fri, 8 Apr 2022 11:40:15 -0400 Subject: [PATCH] Changes story sort order --- ts/state/ducks/stories.ts | 4 ++++ ts/state/selectors/stories.ts | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/ts/state/ducks/stories.ts b/ts/state/ducks/stories.ts index 58f705fbc172..5668dd4c5ef2 100644 --- a/ts/state/ducks/stories.ts +++ b/ts/state/ducks/stories.ts @@ -105,6 +105,10 @@ function markStoryRead( return; } + if (!isDownloaded(matchingStory.attachment)) { + return; + } + if (matchingStory.readStatus !== ReadStatus.Unread) { return; } diff --git a/ts/state/selectors/stories.ts b/ts/state/selectors/stories.ts index 7a6660aa883a..c167aad2d7d4 100644 --- a/ts/state/selectors/stories.ts +++ b/ts/state/selectors/stories.ts @@ -21,6 +21,32 @@ export const shouldShowStoriesView = createSelector( ({ isShowingStoriesView }): boolean => isShowingStoriesView ); +function getNewestStory(x: ConversationStoryType): StoryViewType { + return x.stories[x.stories.length - 1]; +} + +function sortByRecencyAndUnread( + a: ConversationStoryType, + b: ConversationStoryType +): number { + const storyA = getNewestStory(a); + const storyB = getNewestStory(b); + + if (storyA.isUnread && storyB.isUnread) { + return storyA.timestamp > storyB.timestamp ? -1 : 1; + } + + if (storyB.isUnread) { + return 1; + } + + if (storyA.isUnread) { + return -1; + } + + return storyA.timestamp > storyB.timestamp ? -1 : 1; +} + export const getStories = createSelector( getConversationSelector, getStoriesState, @@ -99,10 +125,11 @@ export const getStories = createSelector( }); }); - // Reversing so that the story list is in DESC order return { - hiddenStories: Array.from(hiddenStoriesById.values()).reverse(), - stories: Array.from(storiesById.values()).reverse(), + hiddenStories: Array.from(hiddenStoriesById.values()).sort( + sortByRecencyAndUnread + ), + stories: Array.from(storiesById.values()).sort(sortByRecencyAndUnread), }; } );