import { useCallback, useState } from "react"; import { View, Text, ScrollView, Alert, RefreshControl } from "react-native"; import { useLocalSearchParams, Stack, useRouter } from "expo-router"; import { Ionicons } from "@expo/vector-icons"; 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 { Button } from "../../components/ui/button"; import { Card } from "../../components/ui/card"; import { Loading } from "../../components/ui/loading"; import { getCategoryName } from "../../lib/constants"; export default function PostDetailScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const router = useRouter(); const theme = useTheme(); const { token, userId, isAuthenticated } = useAuth(); const post = useQuery(api.posts.getById, { id: id as any }); const currentUser = useQuery( api.users.getCurrentUser, isAuthenticated && token ? { token } : "skip" ); const closePost = useMutation(api.posts.close); const getOrCreateChat = useMutation(api.chats.getOrCreate); const [refreshing, setRefreshing] = useState(false); const onRefresh = useCallback(() => { setRefreshing(true); setTimeout(() => setRefreshing(false), 800); }, []); if (!post) { return ( <> ); } const customerId = post.customerId as string; const isOwner = isAuthenticated && userId && customerId === userId; const isHandyman = currentUser?.role === "handyman"; const isOpen = post.status === "open"; async function handleClose() { Alert.alert( "Затвори побарување", "Дали сте сигурни дека сакате да го затворите оваа побарување?", [ { text: "Откажи", style: "cancel" }, { text: "Затвори", style: "destructive", onPress: async () => { try { await closePost({ token: token!, postId: id as any }); } catch (e: any) { Alert.alert("Грешка", e.message || "Неуспешно затворање"); } }, }, ] ); } async function handleRespond() { try { const chatId = await getOrCreateChat({ token: token!, partnerId: post!.customerId }); router.push(`/chat/${chatId}` as any); } catch (e: any) { Alert.alert("Грешка", e.message || "Неуспешно поврзување"); } } const categoryName = post.category ? getCategoryName(post.category) : null; const timeAgo = getTimeAgo(post.createdAt); return ( <> } contentContainerStyle={{ padding: theme.spacing.lg, gap: theme.spacing.md, paddingBottom: 100 }} > {post.title} {categoryName && ( Категорија: )} {post.location && ( Локација: {post.location} )} {post.budget && ( Буџет: {post.budget} )} Објавено: {timeAgo} Опис {post.description} {isOwner && isOpen && ( )} {!isOwner && isHandyman && isOpen && isAuthenticated && ( )} ); } function getTimeAgo(timestamp: number): string { const seconds = Math.floor((Date.now() - timestamp) / 1000); if (seconds < 60) return "Пред неколку секунди"; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `Пред ${minutes} мин`; const hours = Math.floor(minutes / 60); if (hours < 24) return `Пред ${hours} ч`; const days = Math.floor(hours / 24); if (days < 30) return `Пред ${days} ден`; const months = Math.floor(days / 30); return `Пред ${months} месеци`; }