Skip to content

Commit

Permalink
Fix/overlapping stories (#3)
Browse files Browse the repository at this point in the history
* fix: stories overlap when next story starts playing

* fix: story not changing on video ending

* fix: adjust types

* fix: remove comments

* fix: revert unnecessary changes
  • Loading branch information
David Natan Bzura authored Feb 10, 2021
1 parent 0f7923e commit 32e506e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
12 changes: 5 additions & 7 deletions src/components/StoryDetail/hook.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import React, { MutableRefObject, useRef } from 'react';
import type Carousel from 'react-native-snap-carousel';
import React from 'react';

import { useTrackRaf } from '../../hooks/useTrackRaf';

import type { StoryDetailProps } from '../../types';
import type { StoryDetailProps, CarouselRef } from '../../types';

import { StoryDetailItem } from '../StoryDetailItem';

export const useStoryDetail = ({
initial,
stories,
carouselRef,
onBackPress,
StoryDetailItemHeader,
StoryDetailItemFooter,
}: StoryDetailProps) => {
const carouselRef: MutableRefObject<Carousel<any> | null> = useRef(null);

}: StoryDetailProps & { carouselRef: CarouselRef }) => {
const { trackRaf } = useTrackRaf();

const onBackDropPress = () => onBackPress(initial);
Expand All @@ -35,7 +33,7 @@ export const useStoryDetail = ({
const onVideoEnd = (idx) => {
if (idx <= stories.length - 2) {
trackRaf(() => {
carouselRef.current?.snapToItem(idx + 1, false, false);
carouselRef.current?.snapToItem(idx + 1, false, true);
});
} else {
onBackPress(idx);
Expand Down
5 changes: 3 additions & 2 deletions src/components/StoryDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ export const StoryDetail = React.forwardRef<
},
ref
) => {
const carouselRef: any = useRef(null);

const { onBackDropPress, renderStoryDetailItem } = useStoryDetail({
initial,
stories,
carouselRef,
onBackPress,
StoryDetailItemHeader,
StoryDetailItemFooter,
} as any);

const carouselRef: any = useRef(null);

return (
<StoryDetailExpander ref={ref} isVisible={animated}>
<Overlay
Expand Down
2 changes: 1 addition & 1 deletion src/components/StoryDetailItem/hook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useStoryDetailItem = ({
} else {
setPaused(true);
setProgress(0);
videoRef.current?.seek(0);
setTimeout(() => videoRef.current?.seek(0), 0);
}
}, [isCurrentStory, videoRef]);

Expand Down
2 changes: 0 additions & 2 deletions src/hooks/useTrackRaf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export const useTrackRaf = () => {
cancelAnimationFrame(intervalId);
});
};

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const trackRaf = (callback: (time: number) => void) => {
Expand Down
10 changes: 10 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ export type StoryDetailExpanderRefProps = {

export type StoryDetailExpanderRef = MutableRefObject<StoryDetailExpanderRefProps | null>;

export type CarouselRefProps = {
/**
* A callback to fire expand-on-click animation
*/
current?: object;
snapToItem: (index: number, animated: boolean, fireCallback: boolean) => void;
};

export type CarouselRef = MutableRefObject<CarouselRefProps | null>;

export type RenderStoryDetailItemProps = {
/**
* The story content
Expand Down

0 comments on commit 32e506e

Please sign in to comment.