diff options
author | dan <dan.abramov@gmail.com> | 2025-01-22 18:39:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-22 18:39:57 +0000 |
commit | d4cf6752dd2829b132dae6ecc64649dbc7dc5c50 (patch) | |
tree | 49c534f6b6d3d8177496446b7ee0886abb61b5bb /src | |
parent | 6538013484a42a3b92001725ec2602dab86eb939 (diff) | |
download | voidsky-d4cf6752dd2829b132dae6ecc64649dbc7dc5c50.tar.zst |
Minor video feed refactor (#7536)
* Remove useless TS checks * Remove default argument
Diffstat (limited to 'src')
-rw-r--r-- | src/screens/VideoFeed/index.tsx | 75 |
1 files changed, 33 insertions, 42 deletions
diff --git a/src/screens/VideoFeed/index.tsx b/src/screens/VideoFeed/index.tsx index 3f89ed927..da842d921 100644 --- a/src/screens/VideoFeed/index.tsx +++ b/src/screens/VideoFeed/index.tsx @@ -165,6 +165,7 @@ type CurrentSource = { type VideoItem = { moderation: ModerationDecision post: AppBskyFeedDefs.PostView + video: AppBskyEmbedVideo.View feedContext: string | undefined } @@ -197,30 +198,30 @@ function Feed() { const videos = useMemo(() => { let vids = - data?.pages - .flatMap(page => { - const items: { - _reactKey: string - moderation: ModerationDecision - post: AppBskyFeedDefs.PostView - feedContext: string | undefined - }[] = [] - for (const slice of page.slices) { - const feedPost = slice.items.find( - item => item.uri === slice.feedPostUri, - ) - if (feedPost) { - items.push({ - _reactKey: feedPost._reactKey, - moderation: feedPost.moderation, - post: feedPost.post, - feedContext: slice.feedContext, - }) - } + data?.pages.flatMap(page => { + const items: { + _reactKey: string + moderation: ModerationDecision + post: AppBskyFeedDefs.PostView + video: AppBskyEmbedVideo.View + feedContext: string | undefined + }[] = [] + for (const slice of page.slices) { + const feedPost = slice.items.find( + item => item.uri === slice.feedPostUri, + ) + if (feedPost && AppBskyEmbedVideo.isView(feedPost.post.embed)) { + items.push({ + _reactKey: feedPost._reactKey, + moderation: feedPost.moderation, + post: feedPost.post, + video: feedPost.post.embed, + feedContext: slice.feedContext, + }) } - return items - }) - .filter(item => AppBskyEmbedVideo.isView(item.post.embed)) || [] + } + return items + }) ?? [] const startingVideoIndex = vids?.findIndex(video => { return video.post.uri === params.initialPostUri }) @@ -244,13 +245,7 @@ function Feed() { const renderItem: ListRenderItem<VideoItem> = useCallback( ({item, index}) => { - const {post} = item - - // filtered above, here for TS - if (!post.embed || !AppBskyEmbedVideo.isView(post.embed)) { - return null - } - + const {post, video} = item const player = players?.[index % 3] const currentSource = currentSources[index % 3] @@ -258,11 +253,11 @@ function Feed() { <VideoItem player={player} post={post} - embed={post.embed} + embed={video} active={ isFocused && index === currentIndex && - currentSource?.source === post.embed.playlist + currentSource?.source === video.playlist } adjacent={index === currentIndex - 1 || index === currentIndex + 1} moderation={item.moderation} @@ -275,15 +270,9 @@ function Feed() { ) const updateVideoState = useCallback( - (index?: number) => { + (index: number) => { if (!videos.length) return - if (index === undefined) { - index = currentIndex - } else { - setCurrentIndex(index) - } - const prevSlice = videos.at(index - 1) const prevPost = prevSlice?.post const prevEmbed = prevPost?.embed @@ -384,11 +373,11 @@ function Feed() { setCurrentSources(updatedSources) } }, - [videos, currentSources, currentIndex, players], + [videos, currentSources, players], ) const updateVideoStateInitially = useNonReactiveCallback(() => { - updateVideoState() + updateVideoState(currentIndex) }) useFocusEffect( @@ -410,7 +399,9 @@ function Feed() { const onViewableItemsChanged = useCallback( ({viewableItems}: {viewableItems: ViewToken[]; changed: ViewToken[]}) => { if (viewableItems[0] && viewableItems[0].index !== null) { - updateVideoState(viewableItems[0].index) + const newIndex = viewableItems[0].index + setCurrentIndex(newIndex) + updateVideoState(newIndex) } }, [updateVideoState], |