import React, { useState } from 'react'; import { View, Text, TouchableOpacity, Linking, Platform, LayoutAnimation, UIManager, } from 'react-native'; import { OrderStatus, TrackingEntry } from '@interfaces'; import { formatDate, formatTime } from '@utils/helper'; import { OrderStatusTimelineProps } from './OrderStatusTimeline.props'; import { useAppTheme } from '@theme'; import { getStyles } from './OrderStatusTimeline.styles'; if ( Platform.OS === 'android' && UIManager.setLayoutAnimationEnabledExperimental ) { UIManager.setLayoutAnimationEnabledExperimental(true); } export const STATUS_FLOW: OrderStatus[] = [ 'PENDING', 'CONFIRMED', 'PREPARING', 'READY_FOR_PICKUP', 'OUT_FOR_DELIVERY', 'DELIVERED', ]; export const STATUS_LABEL: Record = { PENDING: 'Order Placed', CONFIRMED: 'Confirmed', PREPARING: 'Preparing', READY_FOR_PICKUP: 'Ready for Pickup', OUT_FOR_DELIVERY: 'Out for Delivery', DELIVERED: 'Delivered', CANCELLED: 'Cancelled', }; const STATUS_DETAILS: Record< OrderStatus, { title: string; desc: string; icon: string } > = { PENDING: { title: 'Waiting for Confirmation', desc: 'The merchant is reviewing your order. We will start preparing it soon.', icon: '⏳', }, CONFIRMED: { title: 'Order Confirmed', desc: 'Your order has been accepted. The chef will start preparing it shortly.', icon: '✅', }, PREPARING: { title: 'Preparing Your Food', desc: 'The kitchen is preparing your delicious order with fresh ingredients.', icon: '🍳', }, READY_FOR_PICKUP: { title: 'Ready for Pickup', desc: 'Your order is ready. The delivery partner is picking it up now.', icon: '📦', }, OUT_FOR_DELIVERY: { title: 'Out for Delivery', desc: 'Our delivery partner is on the way with your food. Keep your phone handy!', icon: '🛵', }, DELIVERED: { title: 'Order Delivered', desc: 'Your order has been successfully delivered. Enjoy your meal!', icon: '🎉', }, CANCELLED: { title: 'Order Cancelled', desc: 'This order was cancelled. Any refund will be processed shortly.', icon: '❌', }, }; const getStepIndex = (status: OrderStatus): number => { switch (status) { case 'PENDING': case 'CONFIRMED': return 0; case 'PREPARING': return 1; case 'READY_FOR_PICKUP': case 'OUT_FOR_DELIVERY': return 2; case 'DELIVERED': return 3; default: return 0; } }; export const OrderStatusTimeline: React.FC = ({ tracking = [], currentStatus, styles: propStyles, }) => { const { colors } = useAppTheme(); const localStyles = getStyles(colors); // If the caller passes the screen styles, they won't have timelineRow etc, so we fallback to localStyles const styles = propStyles && propStyles.timelineRow ? propStyles : localStyles; const [expanded, setExpanded] = useState(false); const toggleExpand = () => { LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); setExpanded(!expanded); }; const handleCall = () => { Linking.openURL('tel:+919876543210'); }; const handleMessage = () => { Linking.openURL('sms:+919876543210'); }; const trackingMap = tracking.reduce>((acc, t) => { acc[t.status] = t.time; return acc; }, {}); const details = STATUS_DETAILS[currentStatus] || STATUS_DETAILS.PENDING; if (currentStatus === 'CANCELLED') { return ( {/* Cancelled Status Banner */} {details.title} {details.desc} {/* Collapsible detailed view */} {expanded ? 'Hide Details' : 'View Detailed History'} {expanded ? '▲' : '▼'} {expanded && ( Order Cancelled {trackingMap['CANCELLED'] && ( {formatDate(trackingMap['CANCELLED'])} at{' '} {formatTime(trackingMap['CANCELLED'])} )} )} ); } const activeStep = getStepIndex(currentStatus); const steps = [ { label: 'Placed', status: 'PENDING' }, { label: 'Preparing', status: 'PREPARING' }, { label: 'On the Way', status: 'OUT_FOR_DELIVERY' }, { label: 'Delivered', status: 'DELIVERED' }, ]; const showDriver = [ 'PREPARING', 'READY_FOR_PICKUP', 'OUT_FOR_DELIVERY', 'DELIVERED', ].includes(currentStatus); const currentIndex = STATUS_FLOW.indexOf(currentStatus); return ( {/* Current Status Header Banner */} {details.icon} {details.title} {details.desc} {/* Horizontal Progress Stepper */} {steps.map((step, idx) => { const isCompleted = idx < activeStep; const isActive = idx === activeStep; return ( {isCompleted ? ( ) : isActive ? ( ) : null} {step.label} ); })} {/* Delivery Partner Details (if driver assigned) */} {showDriver && ( RS Your Delivery Hero Rahul Sharma KA-01-AB-1234 • Honda Activa ⭐ 4.8 📞 Call Partner 💬 Chat )} {/* View Detailed History Toggle */} {expanded ? 'Hide Details' : 'View Detailed History'} {expanded ? '▲' : '▼'} {/* Detailed Vertical Logs */} {expanded && ( {STATUS_FLOW.map((status, index) => { const isCompleted = index <= currentIndex; const isLast = index === STATUS_FLOW.length - 1; const time = trackingMap[status]; return ( {isCompleted && } {!isLast && ( )} {STATUS_LABEL[status]} {time && ( {formatDate(time)} at {formatTime(time)} )} ); })} )} ); };