|
1 | 1 | import { Image } from "expo-image"; |
2 | | -import { FlatList, View } from "react-native"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { |
| 4 | + Dimensions, |
| 5 | + FlatList, |
| 6 | + type NativeScrollEvent, |
| 7 | + type NativeSyntheticEvent, |
| 8 | + View, |
| 9 | +} from "react-native"; |
3 | 10 | import { MosqueMark } from "@/components/ui/mosque-mark"; |
| 11 | +import { useThemeColors } from "@/lib/theme"; |
4 | 12 |
|
5 | 13 | type Props = { |
6 | 14 | photos: string[]; |
7 | 15 | }; |
8 | 16 |
|
9 | 17 | export function MosquePhotoHero({ photos }: Props) { |
10 | | - const uri = photos[0]; |
| 18 | + const width = Dimensions.get("window").width; |
| 19 | + const [index, setIndex] = useState(0); |
| 20 | + const colors = useThemeColors(); |
11 | 21 |
|
12 | | - if (!uri) { |
| 22 | + if (photos.length === 0) { |
13 | 23 | return ( |
14 | 24 | <View className="aspect-[4/3] items-center justify-center bg-green-tint"> |
15 | 25 | <MosqueMark size="xl" /> |
16 | 26 | </View> |
17 | 27 | ); |
18 | 28 | } |
19 | 29 |
|
20 | | - return ( |
21 | | - <View className="aspect-[4/3] bg-line"> |
22 | | - <Image |
23 | | - source={{ uri }} |
24 | | - contentFit="cover" |
25 | | - style={{ width: "100%", height: "100%" }} |
26 | | - transition={200} |
27 | | - /> |
28 | | - </View> |
29 | | - ); |
30 | | -} |
31 | | - |
32 | | -export function MosquePhotoStrip({ photos }: Props) { |
33 | | - const rest = photos.slice(1); |
34 | | - if (rest.length === 0) return null; |
35 | | - |
36 | | - return ( |
37 | | - <FlatList |
38 | | - data={rest} |
39 | | - keyExtractor={(uri) => uri} |
40 | | - horizontal |
41 | | - showsHorizontalScrollIndicator={false} |
42 | | - contentContainerStyle={{ paddingHorizontal: 24, gap: 8 }} |
43 | | - renderItem={({ item }) => ( |
| 30 | + if (photos.length === 1) { |
| 31 | + return ( |
| 32 | + <View className="aspect-[4/3] bg-line"> |
44 | 33 | <Image |
45 | | - source={{ uri: item }} |
| 34 | + source={{ uri: photos[0] }} |
46 | 35 | contentFit="cover" |
47 | | - style={{ width: 120, height: 88, borderRadius: 8 }} |
| 36 | + style={{ width: "100%", height: "100%" }} |
48 | 37 | transition={200} |
49 | 38 | /> |
50 | | - )} |
51 | | - /> |
| 39 | + </View> |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + const onScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 44 | + const next = Math.round(e.nativeEvent.contentOffset.x / width); |
| 45 | + if (next !== index) setIndex(next); |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <View className="aspect-[4/3] bg-line"> |
| 50 | + <FlatList |
| 51 | + data={photos} |
| 52 | + keyExtractor={(uri) => uri} |
| 53 | + horizontal |
| 54 | + pagingEnabled |
| 55 | + showsHorizontalScrollIndicator={false} |
| 56 | + onScroll={onScroll} |
| 57 | + scrollEventThrottle={16} |
| 58 | + renderItem={({ item }) => ( |
| 59 | + <View style={{ width, aspectRatio: 4 / 3 }}> |
| 60 | + <Image |
| 61 | + source={{ uri: item }} |
| 62 | + contentFit="cover" |
| 63 | + style={{ width: "100%", height: "100%" }} |
| 64 | + transition={200} |
| 65 | + /> |
| 66 | + </View> |
| 67 | + )} |
| 68 | + /> |
| 69 | + |
| 70 | + <View |
| 71 | + pointerEvents="none" |
| 72 | + className="absolute inset-x-0 bottom-s-3 flex-row justify-center gap-s-1" |
| 73 | + > |
| 74 | + {photos.map((uri, i) => { |
| 75 | + const active = i === index; |
| 76 | + return ( |
| 77 | + <View |
| 78 | + key={uri} |
| 79 | + style={{ |
| 80 | + width: active ? 18 : 6, |
| 81 | + height: 6, |
| 82 | + borderRadius: 3, |
| 83 | + backgroundColor: active ? colors.white : `${colors.white}80`, |
| 84 | + }} |
| 85 | + /> |
| 86 | + ); |
| 87 | + })} |
| 88 | + </View> |
| 89 | + </View> |
52 | 90 | ); |
53 | 91 | } |
0 commit comments