import { View, Image, Text, TouchableOpacity, ActivityIndicator, Alert, } from "react-native"; import { useEffect, useState } from "react"; import { Ionicons } from "@expo/vector-icons"; import { type ImageAsset } from "@/cloudinary/cloudinary"; import { MasonryFlashList } from "@shopify/flash-list"; import { useRouter } from "expo-router"; import { useStore } from "@/store/store"; export default function HomeScreen() { const router = useRouter(); const setSelectedWallpaper = useStore((store) => store.setSelectedWallpaper); const [images, setImages] = useState(null); const [isLoading, setIsLoading] = useState(false); useEffect(() => { fetchWallpapers(); }, []); async function fetchWallpapers() { try { setIsLoading(true); const res = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/wallpapers`); if (res.status !== 200) { throw new Error("server returned error response"); } const images: ImageAsset[] = await res.json(); setImages(images); } catch (err) { Alert.alert( "Unable to fetch wallpaper", "Check your internet connection. Otherwise, it's probably our fault.", ); } finally { setIsLoading(false); } } function content() { if (isLoading) { return ( ); } if (images) { return ( ( Wallpapers )} renderItem={({ item }) => ( { setSelectedWallpaper(item); router.push("/wallpaper"); }} > {item.display_name} )} estimatedItemSize={200} /> ); } return ( { fetchWallpapers(); }} > Reload ); } return {content()}; }