Skip to content

Commit

Permalink
View next unread story improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-signal committed Apr 28, 2022
1 parent 9d3498d commit 84411fe
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
17 changes: 16 additions & 1 deletion ts/components/Stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,23 @@ export const Stories = ({
});

const onNextUserStories = useCallback(() => {
// First find the next unread story if there are any
const nextUnreadIndex = stories.findIndex(conversationStory =>
conversationStory.stories.some(story => story.isUnread)
);

if (nextUnreadIndex >= 0) {
const nextStory = stories[nextUnreadIndex];
setConversationIdToView(nextStory.conversationId);
return;
}

// If not then play the next available story
const storyIndex = stories.findIndex(
x => x.conversationId === conversationIdToView
);

// If we've reached the end, close the viewer
if (storyIndex >= stories.length - 1 || storyIndex === -1) {
setConversationIdToView(undefined);
return;
Expand All @@ -59,7 +73,8 @@ export const Stories = ({
x => x.conversationId === conversationIdToView
);
if (storyIndex <= 0) {
setConversationIdToView(undefined);
// Restart playback on the story if it's the oldest
setConversationIdToView(conversationIdToView);
return;
}
const prevStory = stories[storyIndex - 1];
Expand Down
4 changes: 3 additions & 1 deletion ts/components/StoryViewer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function getDefaultProps(): PropsType {
preferredReactionEmoji: ['❤️', '👍', '👎', '😂', '😮', '😢'],
queueStoryDownload: action('queueStoryDownload'),
renderEmojiPicker: () => <div />,
selectedStoryIndex: 0,
stories: [
{
attachment: fakeAttachment({
Expand Down Expand Up @@ -106,11 +107,12 @@ story.add('Multi story', () => {
);
});

story.add('So many stories', () => {
story.add('So many stories (start on story 4)', () => {
const sender = getDefaultConversation();
return (
<StoryViewer
{...getDefaultProps()}
selectedStoryIndex={5}
stories={Array(20).fill({
attachment: fakeAttachment({
url: '/fixtures/snow.jpg',
Expand Down
12 changes: 11 additions & 1 deletion ts/components/StoryViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type PropsType = {
recentEmojis?: Array<string>;
renderEmojiPicker: (props: RenderEmojiPickerProps) => JSX.Element;
replyState?: ReplyStateType;
selectedStoryIndex: number;
skinTone?: number;
stories: Array<StoryViewType>;
views?: Array<string>;
Expand Down Expand Up @@ -87,11 +88,13 @@ export const StoryViewer = ({
recentEmojis,
renderEmojiPicker,
replyState,
selectedStoryIndex,
skinTone,
stories,
views,
}: PropsType): JSX.Element => {
const [currentStoryIndex, setCurrentStoryIndex] = useState(0);
const [currentStoryIndex, setCurrentStoryIndex] =
useState(selectedStoryIndex);
const [storyDuration, setStoryDuration] = useState<number | undefined>();

const visibleStory = stories[currentStoryIndex];
Expand Down Expand Up @@ -140,6 +143,13 @@ export const StoryViewer = ({
setHasExpandedCaption(false);
}, [messageId]);

// In case we want to change the story we're viewing from 0 -> N
useEffect(() => {
if (selectedStoryIndex) {
setCurrentStoryIndex(selectedStoryIndex);
}
}, [selectedStoryIndex]);

// Either we show the next story in the current user's stories or we ask
// for the next user's stories.
const showNextStory = useCallback(() => {
Expand Down
4 changes: 3 additions & 1 deletion ts/models/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2437,9 +2437,10 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {

if (isStory(message.attributes)) {
attributes.hasPostedStory = true;
} else {
attributes.active_at = now;
}

attributes.active_at = now;
conversation.set(attributes);

if (
Expand Down Expand Up @@ -2542,6 +2543,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
const isGroupStoryReply =
isGroup(conversation.attributes) && message.get('storyId');
if (
!isStory(message.attributes) &&
!isGroupStoryReply &&
(!conversationTimestamp ||
message.get('sent_at') > conversationTimestamp)
Expand Down
3 changes: 3 additions & 0 deletions ts/state/smart/StoryViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ 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<StateType, number>(getEmojiSkinTone);
Expand Down Expand Up @@ -86,6 +88,7 @@ export function SmartStoryViewer({
recentEmojis={recentEmojis}
renderEmojiPicker={renderEmojiPicker}
replyState={replyState}
selectedStoryIndex={selectedStoryIndex}
stories={stories}
skinTone={skinTone}
{...storiesActions}
Expand Down

0 comments on commit 84411fe

Please sign in to comment.