import React, { useState } from 'react'; import { View, ScrollView, Linking } from 'react-native'; import { useRouter } from 'expo-router'; import { useFocusEffect } from '@react-navigation/native'; import { AppContainer, MyText, tw, MyTouchableOpacity, ProfileImage, colors } from 'common-ui'; import TabLayoutWrapper from '@/components/TabLayoutWrapper'; import { LinearGradient } from 'expo-linear-gradient'; import { Ionicons } from '@expo/vector-icons'; import NextOrderGlimpse from '@/components/NextOrderGlimpse'; import AddressSwitcherSheet from '@/src/components/location/AddressSwitcherSheet'; import { useRegionNeo } from '@/src/hooks/useRegionNeo'; import { useAuth } from '@/src/contexts/AuthContext'; import { useNavigationTarget } from 'common-ui/hooks/useNavigationTarget'; import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks'; import { isInNextOrderGlimpseWindow } from '@packages/shared'; import { trpc } from '@/src/trpc-client'; import { useUserDetails } from '@/src/contexts/AuthContext'; import type { Order } from '@/src/types/orders'; export default function Me() { const router = useRouter(); const { logout } = useAuth(); const { getNavigationTarget } = useNavigationTarget(); const [regionDialogOpen, setRegionDialogOpen] = useState(false); const regionName = useRegionNeo().regionName; // Same number the NextOrderGlimpse dials via tel: — Live Support opens it in WhatsApp. const { data: essentialConsts } = useGetEssentialConsts(); const supportMobile = essentialConsts?.supportMobile ?? null; const waDigits = (supportMobile ?? '').replace(/\D/g, ''); const waPhone = waDigits.length === 10 ? `91${waDigits}` : waDigits; // Same upcoming orders the glimpse above shows; their ids prefill the message. const userDetails = useUserDetails(); const { data: ordersData } = trpc.user.order.getOrders.useQuery( { page: 1, pageSize: 50 }, { enabled: !!userDetails }, ); const upcomingIds = ((ordersData?.data as Order[] | undefined) ?? []) .filter((order) => { if (order.orderStatus.toLowerCase() === 'cancelled') return false; if (order.deliveryStatus.toLowerCase() === 'success') return false; return isInNextOrderGlimpseWindow(order); }) .map((order) => order.orderId) .filter(Boolean); const waText = upcomingIds.length > 0 ? `Hi, my orders are ${upcomingIds.join(', ')}` : null; const openWhatsApp = () => { if (!waPhone) return; const url = waText ? `whatsapp://send?phone=${waPhone}&text=${encodeURIComponent(waText)}` : `whatsapp://send?phone=${waPhone}`; Linking.openURL(url); }; useFocusEffect(() => { const target = getNavigationTarget(); if (target) { router.replace(target as any); } }); const menuSections = [ { title: 'Shopping & Activity', items: [ { title: 'My Orders', icon: 'receipt-outline', color: colors.brand600, onPress: () => router.push('/(drawer)/(tabs)/me/my-orders'), subtitle: 'Orders history & tracking' }, { title: 'My Cart', icon: 'cart-outline', color: '#10B981', onPress: () => router.push('/(drawer)/(tabs)/home/cart'), subtitle: 'Items ready for checkout' }, { title: 'Coupons', icon: 'ticket-outline', color: '#8B5CF6', onPress: () => router.push('/(drawer)/(tabs)/me/coupons'), subtitle: 'View active offers & rewards' }, { title: 'Live Support', icon: 'logo-whatsapp', color: '#25D366', onPress: openWhatsApp, subtitle: supportMobile ? `Chat on WhatsApp • ${supportMobile}` : 'Chat with us on WhatsApp' }, ] }, { title: 'Saved Information', items: [ { title: 'Addresses', icon: 'location-outline', color: '#F59E0B', onPress: () => router.push('/(drawer)/(tabs)/me/addresses'), subtitle: 'Manage delivery locations' }, { title: 'Delivery Address', icon: 'globe-outline', color: '#0EA5E9', onPress: () => setRegionDialogOpen(true), subtitle: regionName ? `Delivering in ${regionName}` : 'Choose or add an address' }, { title: 'Profile Settings', icon: 'person-outline', color: '#3B82F6', onPress: () => router.push('/(drawer)/(tabs)/me/edit-profile'), subtitle: 'Update your personal details' }, ] }, { title: 'Support', items: [ { title: 'Help & Complaints', icon: 'chatbubble-ellipses-outline', color: '#EF4444', onPress: () => router.push('/(drawer)/(tabs)/me/complaints'), subtitle: 'Talk to our customer care' }, { title: 'Terms & Conditions', icon: 'document-text-outline', color: '#6B7280', onPress: () => router.push('/(drawer)/(tabs)/me/terms'), subtitle: 'Read our legal policies' }, ] } ]; return ( {/* Minimal Header Section */} My Account Manage your freshyo experience router.push('/(drawer)/(tabs)/me/edit-profile')} > {/* Upcoming Order Glimpse */} {/* Menu Sections */} {menuSections.map((section, sIndex) => ( {section.title} {section.items.map((item, iIndex) => ( {item.title} {item.subtitle} ))} ))} {/* Footer / Logout */} logout()} > Log Out from Freshyo Version 1.2.0 • Freshyo App setRegionDialogOpen(false)} /> ); }