refactor: use EntryMediaItem component for media rendering

This commit is contained in:
lawvs
2025-07-09 05:20:50 +08:00
parent 0150803e47
commit d5ac4091b0
@@ -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 && (
<View className="ml-10 flex flex-row flex-wrap justify-between">
<>
{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 = (
<NativePressable
onPress={() => {
onPreviewImage(index)
}}
>
<Image
proxy={{
width: fullWidth ? 400 : 200,
}}
source={{ uri: imageUrl }}
blurhash={mediaItem.blurhash}
className="border-secondary-system-background w-full rounded-lg border"
aspectRatio={
fullWidth && mediaItem.width && mediaItem.height
? mediaItem.width / mediaItem.height
: 1
}
/>
</NativePressable>
)
if (mediaItem.type === "video") {
return (
<View key={`${entryId}-${mediaItem.url}`} className="w-full">
<VideoPlayer
source={{
uri: mediaItem.url,
}}
height={mediaItem.height}
width={mediaItem.width}
placeholder={ImageItem}
view={FeedViewType.SocialMedia}
/>
</View>
)
}
return (
<Pressable
key={`${entryId}-${imageUrl}`}
className={fullWidth ? "w-full" : "w-1/2 p-0.5"}
>
{ImageItem}
</Pressable>
)
})}
{media.map((mediaItem, index) => (
<EntryMediaItem
key={`${entryId}-${mediaItem.url}`}
index={index}
mediaItem={mediaItem}
fullWidth={index === media.length - 1 && media.length % 2 === 1}
entryId={entryId}
onPreviewImage={onPreviewImage}
/>
))}
</>
</View>
)}
@@ -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<View>()
const imageUrl =
mediaItem.type === "video"
? mediaItem.preview_image_url
: mediaItem.type === "photo"
? mediaItem.url
: undefined
if (!imageUrl) return null
const ImageItem = (
<Animated.View ref={aviRef} collapsable={false}>
<NativePressable
onPress={() => {
runOnUI(() => {
"worklet"
const rect = measure(aviRef)
onPreviewImage(index, rect)
})()
}}
>
<Image
proxy={{
width: fullWidth ? 400 : 200,
}}
source={{ uri: imageUrl }}
blurhash={mediaItem.blurhash}
className="border-secondary-system-background w-full rounded-lg border"
aspectRatio={
fullWidth && mediaItem.width && mediaItem.height
? mediaItem.width / mediaItem.height
: 1
}
/>
</NativePressable>
</Animated.View>
)
if (mediaItem.type === "video") {
return (
<View key={`${entryId}-${mediaItem.url}`} className="w-full">
<VideoPlayer
source={{
uri: mediaItem.url,
}}
height={mediaItem.height}
width={mediaItem.width}
placeholder={ImageItem}
view={FeedViewType.SocialMedia}
/>
</View>
)
}
return <Pressable className={fullWidth ? "w-full" : "w-1/2 p-0.5"}>{ImageItem}</Pressable>
},
)
EntryMediaItem.displayName = "EntryMediaItem"