refactor: simplify MediaCarousel components
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import { cn } from "@follow/utils/utils"
|
||||
import { Text, View } from "react-native"
|
||||
import type { Image as ExpoImage } from "expo-image"
|
||||
import { useCallback } from "react"
|
||||
import { Text, TouchableOpacity, View } from "react-native"
|
||||
import { measure, runOnJS, runOnUI, useAnimatedRef } from "react-native-reanimated"
|
||||
|
||||
import { User4CuteFiIcon } from "@/src/icons/user_4_cute_fi"
|
||||
|
||||
import { Galeria } from "../image/galeria"
|
||||
import { useLightboxControls } from "../../lightbox/lightboxState"
|
||||
import { Image } from "../image/Image"
|
||||
|
||||
interface UserAvatarProps {
|
||||
@@ -24,6 +27,37 @@ export const UserAvatar = ({
|
||||
color,
|
||||
preview = true,
|
||||
}: UserAvatarProps) => {
|
||||
const { openLightbox } = useLightboxControls()
|
||||
const aviRef = useAnimatedRef<ExpoImage>()
|
||||
|
||||
const onPreview = useCallback(() => {
|
||||
runOnUI(() => {
|
||||
"worklet"
|
||||
if (!image) {
|
||||
return
|
||||
}
|
||||
const rect = measure(aviRef)
|
||||
runOnJS(openLightbox)({
|
||||
images: [
|
||||
{
|
||||
uri: image,
|
||||
thumbUri: image,
|
||||
thumbDimensions: null,
|
||||
thumbRect: rect,
|
||||
dimensions: rect
|
||||
? {
|
||||
height: rect.height,
|
||||
width: rect.width,
|
||||
}
|
||||
: null,
|
||||
type: "image",
|
||||
},
|
||||
],
|
||||
index: 0,
|
||||
})
|
||||
})()
|
||||
}, [aviRef, image, openLightbox])
|
||||
|
||||
if (!image) {
|
||||
return (
|
||||
<View
|
||||
@@ -51,6 +85,7 @@ export const UserAvatar = ({
|
||||
|
||||
const imageContent = (
|
||||
<Image
|
||||
ref={aviRef}
|
||||
source={{ uri: image }}
|
||||
className={cn("rounded-full", className)}
|
||||
style={{ width: size, height: size }}
|
||||
@@ -62,9 +97,7 @@ export const UserAvatar = ({
|
||||
)
|
||||
|
||||
return preview ? (
|
||||
<Galeria urls={[image]}>
|
||||
<Galeria.Image index={0}>{imageContent}</Galeria.Image>
|
||||
</Galeria>
|
||||
<TouchableOpacity onPress={onPreview}>{imageContent}</TouchableOpacity>
|
||||
) : (
|
||||
imageContent
|
||||
)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { FeedViewType } from "@follow/constants"
|
||||
import type { MediaModel } from "@follow/database/schemas/types"
|
||||
import type { Ref } from "react"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { ScrollView, View } from "react-native"
|
||||
import { useEffect, useState } from "react"
|
||||
import { ScrollView, TouchableOpacity, View } from "react-native"
|
||||
import Animated, {
|
||||
interpolateColor,
|
||||
useAnimatedStyle,
|
||||
@@ -10,7 +10,6 @@ import Animated, {
|
||||
withSpring,
|
||||
} from "react-native-reanimated"
|
||||
|
||||
import { Galeria } from "@/src/components/ui/image/galeria"
|
||||
import { EntryGridFooter } from "@/src/modules/entry-content/EntryGridFooter"
|
||||
|
||||
import { Image } from "../image/Image"
|
||||
@@ -28,7 +27,7 @@ export const MediaCarousel = ({
|
||||
ref?: Ref<View>
|
||||
entryId: string
|
||||
media: MediaModel[]
|
||||
onPreview?: () => void
|
||||
onPreview?: (index: number) => void
|
||||
aspectRatio: number
|
||||
view: FeedViewType
|
||||
}) => {
|
||||
@@ -46,86 +45,69 @@ export const MediaCarousel = ({
|
||||
}}
|
||||
>
|
||||
<View ref={ref} className="relative overflow-hidden rounded-md">
|
||||
<Galeria
|
||||
urls={useMemo(
|
||||
() =>
|
||||
media
|
||||
.map((m) =>
|
||||
m.type === "video" ? m.preview_image_url : m.type === "photo" ? m.url : undefined,
|
||||
)
|
||||
.filter(Boolean) as string[],
|
||||
[media],
|
||||
)}
|
||||
<ScrollView
|
||||
onScroll={(e) => {
|
||||
setActiveIndex(Math.round(e.nativeEvent.contentOffset.x / containerWidth))
|
||||
}}
|
||||
scrollEventThrottle={16}
|
||||
scrollEnabled={hasMany}
|
||||
horizontal
|
||||
showsHorizontalScrollIndicator={false}
|
||||
pagingEnabled
|
||||
className="flex-1"
|
||||
// We need to fixed the height of the container to prevent the carousel from resizing
|
||||
// See https://github.com/Shopify/flash-list/issues/797
|
||||
style={{ height: containerHeight }}
|
||||
>
|
||||
<ScrollView
|
||||
onScroll={(e) => {
|
||||
setActiveIndex(Math.round(e.nativeEvent.contentOffset.x / containerWidth))
|
||||
}}
|
||||
scrollEventThrottle={16}
|
||||
scrollEnabled={hasMany}
|
||||
horizontal
|
||||
showsHorizontalScrollIndicator={false}
|
||||
pagingEnabled
|
||||
className="flex-1"
|
||||
// We need to fixed the height of the container to prevent the carousel from resizing
|
||||
// See https://github.com/Shopify/flash-list/issues/797
|
||||
style={{ height: containerHeight }}
|
||||
>
|
||||
{media.map((m, index) => {
|
||||
const imageUrl = m.type === "video" ? m.preview_image_url : m.url
|
||||
if (!imageUrl) {
|
||||
return null
|
||||
}
|
||||
const ImageItem = (
|
||||
<Galeria.Image onPreview={onPreview} index={index}>
|
||||
<Image
|
||||
proxy={{
|
||||
height: 400,
|
||||
}}
|
||||
source={{ uri: imageUrl }}
|
||||
blurhash={m.blurhash}
|
||||
className="w-full"
|
||||
aspectRatio={aspectRatio}
|
||||
placeholderContentFit="cover"
|
||||
/>
|
||||
</Galeria.Image>
|
||||
)
|
||||
{media.map((m, index) => {
|
||||
const imageUrl = m.type === "video" ? m.preview_image_url : m.url
|
||||
if (!imageUrl) {
|
||||
return null
|
||||
}
|
||||
const ImageItem = (
|
||||
<TouchableOpacity onPress={() => onPreview?.(index)}>
|
||||
<Image
|
||||
proxy={{
|
||||
height: 400,
|
||||
}}
|
||||
source={{ uri: imageUrl }}
|
||||
blurhash={m.blurhash}
|
||||
className="w-full"
|
||||
aspectRatio={aspectRatio}
|
||||
placeholderContentFit="cover"
|
||||
/>
|
||||
</TouchableOpacity>
|
||||
)
|
||||
|
||||
if (m.type === "photo") {
|
||||
return (
|
||||
<View
|
||||
key={imageUrl}
|
||||
className="relative"
|
||||
style={{ width: containerWidth, height: containerHeight }}
|
||||
>
|
||||
<ImageContextMenu entryId={entryId} imageUrl={imageUrl} view={view}>
|
||||
{ImageItem}
|
||||
</ImageContextMenu>
|
||||
</View>
|
||||
)
|
||||
} else if (m.type === "video") {
|
||||
return (
|
||||
<ImageContextMenu
|
||||
key={imageUrl}
|
||||
entryId={entryId}
|
||||
imageUrl={imageUrl}
|
||||
view={view}
|
||||
>
|
||||
<VideoPlayer
|
||||
source={m.url}
|
||||
height={containerHeight}
|
||||
width={containerWidth}
|
||||
placeholder={ImageItem}
|
||||
view={view}
|
||||
/>
|
||||
if (m.type === "photo") {
|
||||
return (
|
||||
<View
|
||||
key={imageUrl}
|
||||
className="relative"
|
||||
style={{ width: containerWidth, height: containerHeight }}
|
||||
>
|
||||
<ImageContextMenu entryId={entryId} imageUrl={imageUrl} view={view}>
|
||||
{ImageItem}
|
||||
</ImageContextMenu>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})}
|
||||
</ScrollView>
|
||||
</Galeria>
|
||||
</View>
|
||||
)
|
||||
} else if (m.type === "video") {
|
||||
return (
|
||||
<ImageContextMenu key={imageUrl} entryId={entryId} imageUrl={imageUrl} view={view}>
|
||||
<VideoPlayer
|
||||
source={m.url}
|
||||
height={containerHeight}
|
||||
width={containerWidth}
|
||||
placeholder={ImageItem}
|
||||
view={view}
|
||||
/>
|
||||
</ImageContextMenu>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})}
|
||||
</ScrollView>
|
||||
|
||||
{/* Indicators */}
|
||||
{hasMany && (
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import type { ContextType } from "react"
|
||||
import { createContext } from "react"
|
||||
import type { Image } from "react-native"
|
||||
|
||||
type ImageSource = string | Parameters<typeof Image.resolveAssetSource>[0]
|
||||
|
||||
export const GaleriaContext = createContext({
|
||||
initialIndex: 0,
|
||||
open: false,
|
||||
urls: [] as unknown as undefined | ImageSource[],
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
ids: undefined as string[] | undefined,
|
||||
setOpen: () => {},
|
||||
theme: "dark" as "dark" | "light",
|
||||
src: "",
|
||||
})
|
||||
|
||||
export type GaleriaContext = ContextType<typeof GaleriaContext>
|
||||
@@ -1,66 +0,0 @@
|
||||
import type { ReactNode } from "react"
|
||||
import { useCallback, useMemo } from "react"
|
||||
import { TouchableOpacity } from "react-native"
|
||||
|
||||
import { GaleriaContext } from "./context"
|
||||
import type { GaleriaViewProps } from "./types"
|
||||
|
||||
type GaleriaInterface = (({
|
||||
children,
|
||||
urls,
|
||||
theme,
|
||||
ids,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
} & Partial<Pick<GaleriaContext, "theme" | "ids" | "urls">>) => ReactNode) & {
|
||||
Image: (props: GaleriaViewProps) => ReactNode
|
||||
}
|
||||
|
||||
const noop = () => {}
|
||||
|
||||
const Galeria: GaleriaInterface = Object.assign(
|
||||
function Galeria({
|
||||
children,
|
||||
urls,
|
||||
theme = "dark",
|
||||
ids,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
} & Partial<Pick<GaleriaContext, "theme" | "ids" | "urls">>) {
|
||||
return (
|
||||
<GaleriaContext
|
||||
value={useMemo(
|
||||
() => ({
|
||||
urls,
|
||||
theme,
|
||||
initialIndex: 0,
|
||||
open: false,
|
||||
src: "",
|
||||
setOpen: noop,
|
||||
ids,
|
||||
}),
|
||||
[urls, theme, ids],
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</GaleriaContext>
|
||||
)
|
||||
},
|
||||
{
|
||||
Image(props: GaleriaViewProps) {
|
||||
// const { urls, initialIndex } = use(GaleriaContext)
|
||||
|
||||
return (
|
||||
<TouchableOpacity
|
||||
onPress={useCallback(() => {
|
||||
props.onPreview?.({ nativeEvent: { index: props.index ?? 0 } })
|
||||
}, [props])}
|
||||
>
|
||||
{props.children}
|
||||
</TouchableOpacity>
|
||||
)
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
export { Galeria }
|
||||
@@ -1,12 +0,0 @@
|
||||
import type { ViewStyle } from "react-native"
|
||||
|
||||
export type GaleriaViewProps = {
|
||||
index?: number
|
||||
id?: string
|
||||
children: React.ReactElement
|
||||
style?: ViewStyle
|
||||
dynamicAspectRatio?: boolean
|
||||
onPreview?: (e: { nativeEvent: { index: number } }) => void
|
||||
onClosePreview?: (e: { nativeEvent: never }) => void
|
||||
onIndexChange?: (e: { nativeEvent: { index: number } }) => void
|
||||
}
|
||||
@@ -46,7 +46,7 @@ export function EntryPictureItem({ id }: { id: string }) {
|
||||
ref={aviRef}
|
||||
media={item.media}
|
||||
entryId={id}
|
||||
onPreview={() => {
|
||||
onPreview={(index) => {
|
||||
const feed = getFeedById(item.feedId!)
|
||||
if (!feed) {
|
||||
return
|
||||
@@ -73,7 +73,7 @@ export function EntryPictureItem({ id }: { id: string }) {
|
||||
: null,
|
||||
type: "image",
|
||||
})),
|
||||
index: 0,
|
||||
index,
|
||||
})
|
||||
})()
|
||||
const fullEntry = getEntry(id)
|
||||
@@ -97,7 +97,7 @@ const MediaItems = ({
|
||||
ref?: Ref<View>
|
||||
media: MediaModel[]
|
||||
entryId: string
|
||||
onPreview?: () => void
|
||||
onPreview?: (index: number) => void
|
||||
aspectRatio?: number
|
||||
}) => {
|
||||
const firstMedia = media[0]
|
||||
|
||||
Reference in New Issue
Block a user