From d5ac4091b06e7f8d7862970bbb50ead748a9b3ad Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Wed, 9 Jul 2025 05:20:50 +0800 Subject: [PATCH] refactor: use EntryMediaItem component for media rendering --- .../entry-list/templates/EntrySocialItem.tsx | 194 ++++++++++-------- 1 file changed, 108 insertions(+), 86 deletions(-) diff --git a/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx index dd71cb4d8..8ade98689 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx @@ -1,4 +1,5 @@ import { FeedViewType } from "@follow/constants" +import type { MediaModel } from "@follow/database/schemas/types" import { useEntry } from "@follow/store/entry/hooks" import { useFeedById } from "@follow/store/feed/hooks" import { useEntryTranslation } from "@follow/store/translation/hooks" @@ -6,7 +7,8 @@ import { unreadSyncService } from "@follow/store/unread/store" import { tracker } from "@follow/tracker" import { memo, useCallback } from "react" import { Pressable, Text, View } from "react-native" -import { runOnJS, runOnUI } from "react-native-reanimated" +import type { MeasuredDimensions } from "react-native-reanimated" +import Animated, { measure, runOnJS, runOnUI, useAnimatedRef } from "react-native-reanimated" import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general" import { useLightboxControls } from "@/src/components/lightbox/lightboxState" @@ -70,35 +72,32 @@ export const EntrySocialItem = memo( }, [entry, navigation]) const onPreviewImage = useCallback( - (index: number) => { - runOnUI(() => { - "worklet" - // const rect = measureHandle(aviHandle) - runOnJS(openLightbox)({ - images: (entry?.media ?? []) - .map((mediaItem) => { - const imageUrl = - mediaItem.type === "video" - ? mediaItem.preview_image_url - : mediaItem.type === "photo" - ? mediaItem.url - : undefined - return { - uri: imageUrl ?? "", - dimensions: { - width: mediaItem.width ?? 0, - height: mediaItem.height ?? 0, - }, - thumbUri: imageUrl ?? "", - thumbDimensions: null, - thumbRect: null, - type: "image" as const, - } - }) - .filter((i) => !!i.uri), - index, - }) - })() + (index: number, rect: MeasuredDimensions | null) => { + "worklet" + runOnJS(openLightbox)({ + images: (entry?.media ?? []) + .map((mediaItem) => { + const imageUrl = + mediaItem.type === "video" + ? mediaItem.preview_image_url + : mediaItem.type === "photo" + ? mediaItem.url + : undefined + return { + uri: imageUrl ?? "", + dimensions: { + width: mediaItem.width ?? 0, + height: mediaItem.height ?? 0, + }, + thumbUri: imageUrl ?? "", + thumbDimensions: null, + thumbRect: rect, + type: "image" as const, + } + }) + .filter((i) => !!i.uri), + index, + }) }, [entry?.media, openLightbox], ) @@ -156,62 +155,16 @@ export const EntrySocialItem = memo( {media && media.length > 0 && ( <> - {media.map((mediaItem, index) => { - const imageUrl = - mediaItem.type === "video" - ? mediaItem.preview_image_url - : mediaItem.type === "photo" - ? mediaItem.url - : undefined - const fullWidth = index === media.length - 1 && media.length % 2 === 1 - if (!imageUrl) return null - - const ImageItem = ( - { - onPreviewImage(index) - }} - > - - - ) - - if (mediaItem.type === "video") { - return ( - - - - ) - } - return ( - - {ImageItem} - - ) - })} + {media.map((mediaItem, index) => ( + + ))} )} @@ -222,3 +175,72 @@ export const EntrySocialItem = memo( ) EntrySocialItem.displayName = "EntrySocialItem" + +interface EntryMediaItemProps { + index: number + entryId: string + mediaItem: MediaModel + fullWidth: boolean + onPreviewImage: (index: number, rect: MeasuredDimensions | null) => void +} + +const EntryMediaItem = memo( + ({ mediaItem, index, fullWidth, entryId, onPreviewImage }: EntryMediaItemProps) => { + const aviRef = useAnimatedRef() + + const imageUrl = + mediaItem.type === "video" + ? mediaItem.preview_image_url + : mediaItem.type === "photo" + ? mediaItem.url + : undefined + if (!imageUrl) return null + + const ImageItem = ( + + { + runOnUI(() => { + "worklet" + const rect = measure(aviRef) + onPreviewImage(index, rect) + })() + }} + > + + + + ) + + if (mediaItem.type === "video") { + return ( + + + + ) + } + return {ImageItem} + }, +) + +EntryMediaItem.displayName = "EntryMediaItem"