From ff87caf526cb146a53f845e3b1d52368cee0bfa8 Mon Sep 17 00:00:00 2001
From: Josh Perez <60019601+josh-signal@users.noreply.github.com>
Date: Tue, 3 May 2022 12:02:43 -0400
Subject: [PATCH] Fixes going to oldest unread story when viewing
---
ts/components/StoryViewer.stories.tsx | 19 ---------------
ts/components/StoryViewer.tsx | 33 +++++++++++++++++++--------
ts/state/smart/StoryViewer.tsx | 3 ---
ts/util/lint/exceptions.json | 7 ++++++
4 files changed, 30 insertions(+), 32 deletions(-)
diff --git a/ts/components/StoryViewer.stories.tsx b/ts/components/StoryViewer.stories.tsx
index 1f98d2df27ee..4811fd02a3a7 100644
--- a/ts/components/StoryViewer.stories.tsx
+++ b/ts/components/StoryViewer.stories.tsx
@@ -39,7 +39,6 @@ function getDefaultProps(): PropsType {
preferredReactionEmoji: ['❤️', '👍', '👎', '😂', '😮', '😢'],
queueStoryDownload: action('queueStoryDownload'),
renderEmojiPicker: () =>
,
- selectedStoryIndex: 0,
stories: [
{
attachment: fakeAttachment({
@@ -109,24 +108,6 @@ story.add('Multi story', () => {
);
});
-story.add('So many stories (start on story 4)', () => {
- const sender = getDefaultConversation();
- return (
-
- );
-});
-
story.add('Caption', () => (
;
renderEmojiPicker: (props: RenderEmojiPickerProps) => JSX.Element;
replyState?: ReplyStateType;
- selectedStoryIndex: number;
skinTone?: number;
stories: Array;
views?: Array;
@@ -94,13 +99,11 @@ export const StoryViewer = ({
recentEmojis,
renderEmojiPicker,
replyState,
- selectedStoryIndex,
skinTone,
stories,
views,
}: PropsType): JSX.Element => {
- const [currentStoryIndex, setCurrentStoryIndex] =
- useState(selectedStoryIndex);
+ const [currentStoryIndex, setCurrentStoryIndex] = useState(0);
const [storyDuration, setStoryDuration] = useState();
const [isShowingContextMenu, setIsShowingContextMenu] = useState(false);
const [hasConfirmHideStory, setHasConfirmHideStory] = useState(false);
@@ -155,12 +158,22 @@ export const StoryViewer = ({
setHasExpandedCaption(false);
}, [messageId]);
- // In case we want to change the story we're viewing from 0 -> N
+ // These exist to change currentStoryIndex to the oldest unread story a user
+ // has, or set to 0 whenever conversationId changes.
+ // We use a ref so that we only depend on conversationId changing, since
+ // the stories Array will change once we mark as story as viewed.
+ const storiesRef = useRef(stories);
+
useEffect(() => {
- if (selectedStoryIndex) {
- setCurrentStoryIndex(selectedStoryIndex);
- }
- }, [selectedStoryIndex]);
+ const unreadStoryIndex = storiesRef.current.findIndex(
+ story => story.isUnread
+ );
+ setCurrentStoryIndex(unreadStoryIndex < 0 ? 0 : unreadStoryIndex);
+ }, [conversationId]);
+
+ useEffect(() => {
+ storiesRef.current = stories;
+ }, [stories]);
// Either we show the next story in the current user's stories or we ask
// for the next user's stories.
diff --git a/ts/state/smart/StoryViewer.tsx b/ts/state/smart/StoryViewer.tsx
index ffcc220233ee..e1188b00b961 100644
--- a/ts/state/smart/StoryViewer.tsx
+++ b/ts/state/smart/StoryViewer.tsx
@@ -55,8 +55,6 @@ export function SmartStoryViewer({
>(getStoriesSelector);
const { group, stories } = getStoriesByConversationId(conversationId);
- const unreadStoryIndex = stories.findIndex(story => story.isUnread);
- const selectedStoryIndex = unreadStoryIndex > 0 ? unreadStoryIndex : 0;
const recentEmojis = useRecentEmojis();
const skinTone = useSelector(getEmojiSkinTone);
@@ -96,7 +94,6 @@ export function SmartStoryViewer({
recentEmojis={recentEmojis}
renderEmojiPicker={renderEmojiPicker}
replyState={replyState}
- selectedStoryIndex={selectedStoryIndex}
stories={stories}
skinTone={skinTone}
{...storiesActions}
diff --git a/ts/util/lint/exceptions.json b/ts/util/lint/exceptions.json
index c2305a25e5bf..5baec742c6ca 100644
--- a/ts/util/lint/exceptions.json
+++ b/ts/util/lint/exceptions.json
@@ -7706,6 +7706,13 @@
"reasonCategory": "usageTrusted",
"updated": "2022-04-29T23:54:21.656Z"
},
+ {
+ "rule": "React-useRef",
+ "path": "ts/components/StoryViewer.tsx",
+ "line": " const storiesRef = useRef(stories);",
+ "reasonCategory": "usageTrusted",
+ "updated": "2022-04-30T00:44:47.213Z"
+ },
{
"rule": "React-useRef",
"path": "ts/components/StoryViewsNRepliesModal.tsx",