freshyo/apps/user-ui/app/(drawer)/(tabs)/me/index.tsx
2026-11-08 13:49:17 +05:30

226 lines
No EOL
8.7 KiB
TypeScript

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 (
<TabLayoutWrapper>
<View style={tw`flex-1 bg-gray-50`}>
<ScrollView
style={tw`flex-1`}
showsVerticalScrollIndicator={false}
contentContainerStyle={tw`pb-8`}
>
{/* Minimal Header Section */}
<View style={tw`px-6 pt-8 pb-10 flex-row items-center justify-between`}>
<View>
<MyText style={tw`text-3xl font-extrabold text-gray-900 tracking-tight`}>
My Account
</MyText>
<MyText style={tw`text-gray-400 text-sm mt-1`}>
Manage your freshyo experience
</MyText>
</View>
<MyTouchableOpacity
testID="me-settings"
style={tw`bg-gray-100 p-3 rounded-2xl`}
onPress={() => router.push('/(drawer)/(tabs)/me/edit-profile')}
>
<Ionicons name="settings-outline" size={22} color="#1F2937" />
</MyTouchableOpacity>
</View>
{/* Upcoming Order Glimpse */}
<NextOrderGlimpse />
{/* Menu Sections */}
<View style={tw`px-6`}>
{menuSections.map((section, sIndex) => (
<View key={sIndex} style={tw`mb-8`}>
<MyText style={tw`text-sm font-bold text-gray-400 uppercase tracking-[2px] mb-4 px-2`}>
{section.title}
</MyText>
<View style={tw`bg-white rounded-[32px] shadow-sm border border-gray-100 overflow-hidden`}>
{section.items.map((item, iIndex) => (
<MyTouchableOpacity
key={iIndex}
testID={`me-menu-${item.title.toLowerCase().replace(/[^a-z0-9]+/g, '-')}`}
onPress={item.onPress}
style={tw`flex-row items-center p-4.5 ${iIndex !== section.items.length - 1 ? 'border-b border-gray-50' : ''}`}
>
<View style={[tw`w-12 h-12 rounded-2xl items-center justify-center mr-4`, { backgroundColor: `${item.color}15` }]}>
<Ionicons name={item.icon as any} size={24} color={item.color} />
</View>
<View style={tw`flex-1`}>
<MyText style={tw`text-base font-semibold text-gray-800`}>{item.title}</MyText>
<MyText style={tw`text-xs text-gray-400 mt-0.5`}>{item.subtitle}</MyText>
</View>
<View style={tw`bg-gray-50 w-8 h-8 rounded-full items-center justify-center`}>
<Ionicons name="chevron-forward" size={16} color="#94A3B8" />
</View>
</MyTouchableOpacity>
))}
</View>
</View>
))}
{/* Footer / Logout */}
<MyTouchableOpacity
testID="me-logout"
style={tw`flex-row items-center justify-center p-5 bg-white rounded-[24px] border border-red-50 shadow-sm mt-2`}
onPress={() => logout()}
>
<Ionicons name="log-out-outline" size={22} color="#F43F5E" style={tw`mr-2`} />
<MyText style={tw`text-rose-500 font-bold text-base`}>Log Out from Freshyo</MyText>
</MyTouchableOpacity>
<MyText testID="me-version" style={tw`text-center text-gray-300 text-[10px] mt-12 uppercase tracking-[3px]`}>
Version 1.2.0 • Freshyo App
</MyText>
</View>
</ScrollView>
<AddressSwitcherSheet open={regionDialogOpen} onClose={() => setRegionDialogOpen(false)} />
</View>
</TabLayoutWrapper>
);
}