From 68f1d35b699745cdc67fc49452be3aa9cc96a8b3 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Tue, 8 Jul 2025 01:04:23 +0800 Subject: [PATCH] refactor: simplify MediaCarousel components --- .../src/components/ui/avatar/UserAvatar.tsx | 43 +++++- .../components/ui/carousel/MediaCarousel.tsx | 144 ++++++++---------- .../components/ui/image/galeria/context.tsx | 20 --- .../src/components/ui/image/galeria/index.tsx | 66 -------- .../src/components/ui/image/galeria/types.ts | 12 -- .../entry-list/templates/EntryPictureItem.tsx | 6 +- 6 files changed, 104 insertions(+), 187 deletions(-) delete mode 100644 apps/mobile/src/components/ui/image/galeria/context.tsx delete mode 100644 apps/mobile/src/components/ui/image/galeria/index.tsx delete mode 100644 apps/mobile/src/components/ui/image/galeria/types.ts diff --git a/apps/mobile/src/components/ui/avatar/UserAvatar.tsx b/apps/mobile/src/components/ui/avatar/UserAvatar.tsx index 07fc22a40..f6aa98069 100644 --- a/apps/mobile/src/components/ui/avatar/UserAvatar.tsx +++ b/apps/mobile/src/components/ui/avatar/UserAvatar.tsx @@ -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() + + 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 ( - {imageContent} - + {imageContent} ) : ( imageContent ) diff --git a/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx b/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx index e9a6fe530..1c5b434c5 100644 --- a/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx +++ b/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx @@ -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 entryId: string media: MediaModel[] - onPreview?: () => void + onPreview?: (index: number) => void aspectRatio: number view: FeedViewType }) => { @@ -46,86 +45,69 @@ export const MediaCarousel = ({ }} > - - media - .map((m) => - m.type === "video" ? m.preview_image_url : m.type === "photo" ? m.url : undefined, - ) - .filter(Boolean) as string[], - [media], - )} + { + 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 }} > - { - 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 = ( - - - - ) + {media.map((m, index) => { + const imageUrl = m.type === "video" ? m.preview_image_url : m.url + if (!imageUrl) { + return null + } + const ImageItem = ( + onPreview?.(index)}> + + + ) - if (m.type === "photo") { - return ( - - - {ImageItem} - - - ) - } else if (m.type === "video") { - return ( - - + if (m.type === "photo") { + return ( + + + {ImageItem} - ) - } else { - return null - } - })} - - + + ) + } else if (m.type === "video") { + return ( + + + + ) + } else { + return null + } + })} + {/* Indicators */} {hasMany && ( diff --git a/apps/mobile/src/components/ui/image/galeria/context.tsx b/apps/mobile/src/components/ui/image/galeria/context.tsx deleted file mode 100644 index a2d97e68c..000000000 --- a/apps/mobile/src/components/ui/image/galeria/context.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import type { ContextType } from "react" -import { createContext } from "react" -import type { Image } from "react-native" - -type ImageSource = string | Parameters[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 diff --git a/apps/mobile/src/components/ui/image/galeria/index.tsx b/apps/mobile/src/components/ui/image/galeria/index.tsx deleted file mode 100644 index 1da77ab6a..000000000 --- a/apps/mobile/src/components/ui/image/galeria/index.tsx +++ /dev/null @@ -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>) => ReactNode) & { - Image: (props: GaleriaViewProps) => ReactNode -} - -const noop = () => {} - -const Galeria: GaleriaInterface = Object.assign( - function Galeria({ - children, - urls, - theme = "dark", - ids, - }: { - children: React.ReactNode - } & Partial>) { - return ( - ({ - urls, - theme, - initialIndex: 0, - open: false, - src: "", - setOpen: noop, - ids, - }), - [urls, theme, ids], - )} - > - {children} - - ) - }, - { - Image(props: GaleriaViewProps) { - // const { urls, initialIndex } = use(GaleriaContext) - - return ( - { - props.onPreview?.({ nativeEvent: { index: props.index ?? 0 } }) - }, [props])} - > - {props.children} - - ) - }, - }, -) - -export { Galeria } diff --git a/apps/mobile/src/components/ui/image/galeria/types.ts b/apps/mobile/src/components/ui/image/galeria/types.ts deleted file mode 100644 index e5bb20dee..000000000 --- a/apps/mobile/src/components/ui/image/galeria/types.ts +++ /dev/null @@ -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 -} diff --git a/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx index ea6bc0ac2..faa897e8b 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx @@ -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 media: MediaModel[] entryId: string - onPreview?: () => void + onPreview?: (index: number) => void aspectRatio?: number }) => { const firstMedia = media[0]