import { useState } from "react"; import { View, Text, ScrollView, Pressable, Alert } from "react-native"; import { useLocalSearchParams, Stack, useRouter } from "expo-router"; import { useQuery, useMutation } from "convex/react"; import { api } from "../../convex/_generated/api"; import { useAuth } from "../_layout"; import { useTheme } from "../../components/theme"; import { Badge } from "../../components/ui/badge"; import { Rating } from "../../components/ui/rating"; import { Button } from "../../components/ui/button"; import { Avatar } from "../../components/ui/avatar"; import { Loading } from "../../components/ui/loading"; import { Card } from "../../components/ui/card"; import { ReviewCard } from "../../components/review-card"; import { getCategoryName, CATEGORY_EMOJI } from "../../lib/constants"; export default function AdDetailScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const theme = useTheme(); const router = useRouter(); const { token, isAuthenticated, userId } = useAuth(); const ad = useQuery(api.ads.getById, id ? { id: id as any } : "skip"); const handyman = useQuery( api.users.getById, ad ? { id: ad.handymanId } : "skip" ); const reviews = useQuery( api.reviews.getByAd, id ? { adId: id as any } : "skip" ); const reviewCustomerIds = reviews ? [...new Set(reviews.map((r) => r.customerId))] : []; const customer0 = useQuery( api.users.getById, reviewCustomerIds.length > 0 ? { id: reviewCustomerIds[0] as any } : "skip" ); const customer1 = useQuery( api.users.getById, reviewCustomerIds.length > 1 ? { id: reviewCustomerIds[1] as any } : "skip" ); const customer2 = useQuery( api.users.getById, reviewCustomerIds.length > 2 ? { id: reviewCustomerIds[2] as any } : "skip" ); const customer3 = useQuery( api.users.getById, reviewCustomerIds.length > 3 ? { id: reviewCustomerIds[3] as any } : "skip" ); const customer4 = useQuery( api.users.getById, reviewCustomerIds.length > 4 ? { id: reviewCustomerIds[4] as any } : "skip" ); const customersById: Record = {}; if (customer0) customersById[reviewCustomerIds[0] as string] = customer0; if (customer1) customersById[reviewCustomerIds[1] as string] = customer1; if (customer2) customersById[reviewCustomerIds[2] as string] = customer2; if (customer3) customersById[reviewCustomerIds[3] as string] = customer3; if (customer4) customersById[reviewCustomerIds[4] as string] = customer4; const deleteAd = useMutation(api.ads.remove); const startChat = useMutation(api.chats.getOrCreate); const [startingChat, setStartingChat] = useState(false); async function handleStartChat() { if (!token || !ad) return; setStartingChat(true); try { const chatId = await startChat({ token, partnerId: ad.handymanId }); router.push(`/chat/${chatId}` as any); } catch (e: any) { Alert.alert("Грешка", e.message || "Неуспешно започнување разговор"); } finally { setStartingChat(false); } } if (!ad) { return ( <> ); } const isOwner = userId === ad.handymanId; const categoryName = getCategoryName(ad.category); const categoryEmoji = CATEGORY_EMOJI[ad.category] || "📋"; function handleDelete() { Alert.alert( "Избриши оглас", "Дали сте сигурни дека сакате да го избришете овој оглас?", [ { text: "Откажи", style: "cancel" }, { text: "Избриши", style: "destructive", onPress: async () => { try { if (!ad) return; await deleteAd({ token: token!, adId: ad._id as any }); router.back(); } catch (e: any) { Alert.alert("Грешка", e.message || "Неуспешно бришење"); } }, }, ] ); } const existingUserReview = reviews?.find((r) => r.customerId === userId); return ( <> 20 ? ad.title.slice(0, 20) + "…" : ad.title, headerShown: true, headerBackButtonDisplayMode: "minimal", }} /> {categoryEmoji} Нема слики {ad.title} {ad.priceRange && ( {ad.priceRange} )} 📍 {ad.location} {ad.ratingAvg != null && ad.ratingAvg > 0 && ( )} {ad.availability && ( Расположивост {ad.availability} )} Опис {ad.description} { if (handyman) router.push(`/(tabs)/(profile)`); }} style={{ flexDirection: "row", alignItems: "center", gap: theme.spacing.md }} > {handyman?.name || "Мајстор"} Мајстор {!isOwner && isAuthenticated && ( {!existingUserReview && ( )} {existingUserReview && ( Веќе напишавте оценка за овој оглас )} )} {isOwner && ( )} {!isOwner && !isAuthenticated && ( Најавете се за да започнете разговор или да напишете оценка. )} {/* Reviews section */} {reviews && reviews.length > 0 && ( Оцени {ad.ratingAvg?.toFixed(1)} ({ad.reviewCount} {ad.reviewCount === 1 ? "оценка" : "оцени"}) {reviews.map((review) => { const customer = customersById[review.customerId as string]; return ( ); })} )} ); }