import { createFileRoute, useNavigate, Outlet, useLocation } from '@tanstack/react-router'
import { useAuth } from '../lib/auth-context'
import { MyButton } from 'web-components'
import { AppLayout } from '../components/AppLayout'
import { NextOrderGlimpse } from '../components/me/NextOrderGlimpse'
import { Dialog } from '../components/Dialog'
import { RegionSelector } from '../components/RegionSelector'
import { useRegionStore } from '../lib/stores/region-store'
import {
Package,
ShoppingCart,
Ticket,
MapPin,
Globe,
User,
MessageSquare,
MessageCircle,
FileText,
LogOut,
ChevronRight,
Settings,
} from 'lucide-react'
import React, { useState } from 'react'
import { useGetEssentialConsts } from '../hooks/prominent-api-hooks'
import { isInNextOrderGlimpseWindow } from '@packages/shared'
import { trpc } from '../lib/trpc-client'
export const Route = createFileRoute('/me')({ component: MePage })
interface MenuItem {
title: string
icon: React.ComponentType<{ size?: number | string; color?: string }>
color: string
to?: string
href?: string
subtitle: string
}
interface MenuSection {
title: string
items: MenuItem[]
}
function MenuIcon({ icon: Icon, color }: { icon: MenuItem['icon']; color: string }) {
return (
)
}
function MePage() {
const navigate = useNavigate()
const location = useLocation()
const { user, logout } = useAuth()
const [regionDialogOpen, setRegionDialogOpen] = useState(false)
const selectedRegion = useRegionStore((state) => state.selectedRegion)
const selectedRegionId = useRegionStore((state) => state.selectedRegionId)
// Same number the NextOrderGlimpse dials via tel: — Live Support opens it in WhatsApp.
const { data: essentialConsts } = useGetEssentialConsts()
const supportMobile = (essentialConsts as any)?.supportMobile ?? null
const waDigits = String(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 { data: ordersData } = trpc.user.order.getOrders.useQuery(
{ page: 1, pageSize: 50 },
{ enabled: !!user }
)
const upcomingIds: string[] = ((ordersData as any)?.data ?? [])
.filter((order: any) => {
if (String(order.orderStatus).toLowerCase() === 'cancelled') return false
if (String(order.deliveryStatus).toLowerCase() === 'success') return false
return isInNextOrderGlimpseWindow(order)
})
.map((order: any) => order.orderId)
.filter(Boolean)
const waText = upcomingIds.length > 0 ? `Hi, my orders are ${upcomingIds.join(', ')}` : null
const waHref = waPhone
? waText
? `https://wa.me/${waPhone}?text=${encodeURIComponent(waText)}`
: `https://wa.me/${waPhone}`
: undefined
// Check if we're on the exact /me path (not a child route like /me/orders)
const isExactMePath = location.pathname === '/me'
if (!user) {
return (
Please sign in
navigate({ to: '/login' })}
className="bg-brand-600 text-white"
/>
)
}
const menuSections: MenuSection[] = [
{
title: 'Shopping & Activity',
items: [
{
title: 'My Orders',
icon: Package,
color: '#1570EF',
to: '/me/orders',
subtitle: 'Orders history & tracking',
},
{
title: 'My Cart',
icon: ShoppingCart,
color: '#10B981',
to: '/cart',
subtitle: 'Items ready for checkout',
},
{
title: 'Coupons',
icon: Ticket,
color: '#8B5CF6',
to: '/me/coupons',
subtitle: 'View active offers & rewards',
},
...(waHref
? [
{
title: 'Live Support',
icon: MessageCircle,
color: '#25D366',
href: waHref,
subtitle: supportMobile
? `Chat on WhatsApp • ${supportMobile}`
: 'Chat with us on WhatsApp',
} as MenuItem,
]
: []),
],
},
{
title: 'Saved Information',
items: [
{
title: 'Addresses',
icon: MapPin,
color: '#F59E0B',
to: '/me/addresses',
subtitle: 'Manage delivery locations',
},
{
title: 'Profile Settings',
icon: User,
color: '#3B82F6',
to: '/me/edit-profile',
subtitle: 'Update your personal details',
},
],
},
{
title: 'Support',
items: [
{
title: 'Help & Complaints',
icon: MessageSquare,
color: '#EF4444',
to: '/me/complaints',
subtitle: 'Talk to our customer care',
},
{
title: 'Terms & Conditions',
icon: FileText,
color: '#6B7280',
to: '/me/terms',
subtitle: 'Read our legal policies',
},
],
},
]
return (
{isExactMePath ? (
My Account
Manage your freshyo experience
{menuSections.map((section, sIndex) => (
{section.title}
{section.items.map((item, iIndex) => {
const rowClass = `flex w-full flex-row items-center p-4 text-left transition-colors hover:bg-gray-50 ${
iIndex !== section.items.length - 1 ? 'border-b border-gray-50' : ''
}`
const rowContent = (
<>
{item.title}
{item.subtitle}
>
)
return item.href ? (
{rowContent}
) : (
)
})}
))}
{/* Logout row */}
{/* Delivery region row */}
Version 1.2.0 • Freshyo App
) : (
/* Render child routes when not on exact /me path */
)}
)
}