import React, { useRef, useEffect } from 'react'; import { MdTwoWheeler, MdLocationOn, MdRestaurant, MdMyLocation, MdAccessTime, MdInventory2 } from 'react-icons/md'; import dayjs from 'dayjs'; import { CircularProgress } from '@mui/material'; import { getStatusStyle, getActiveOrder } from './dispatchShared'; import { OpenToast } from 'components/nearle_components/OpenToast'; const ActiveSection = ({ visibleRiders, riders, focusedStop, handleRiderFocus, setFocusedStop, calculateEstMeters, getRiderColor, formatMeters, isLoading }) => { // Sort by live distance ascending (closest drop-off first; null/unknown last) const activeDeliveries = visibleRiders .map((r) => getActiveOrder(r.orders)) .filter(Boolean) .sort((a, b) => { const mA = calculateEstMeters(a.rider_id || a.userid, a) ?? Infinity; const mB = calculateEstMeters(b.rider_id || b.userid, b) ?? Infinity; return mA - mB; }); // Toast notifications: detect status transitions across renders const prevStatusMapRef = useRef(null); useEffect(() => { const currentMap = {}; visibleRiders.forEach((r) => { (r.orders || []).forEach((o) => { currentMap[String(o.orderid)] = { status: String(o.orderstatus || '').toLowerCase(), riderName: o.rider_name || o.ridername || 'Rider', customer: o.deliverycustomer || o.customername || `Order #${o.orderid}` }; }); }); const prev = prevStatusMapRef.current; if (prev !== null) { Object.entries(currentMap).forEach(([oid, cur]) => { const old = prev[oid]; if (!old) { // Brand-new order that is already active → rider has taken a new order if (cur.status === 'active') { OpenToast(`🛵 ${cur.riderName} is now delivering to ${cur.customer}`, 'success', 3000); } return; } // Rider transitioned into active → started heading to customer if (old.status !== 'active' && cur.status === 'active') { OpenToast(`🛵 ${cur.riderName} is now delivering to ${cur.customer}`, 'success', 3000); } // Rider delivered → reached customer location if (old.status === 'active' && cur.status === 'delivered') { OpenToast(`📍 ${cur.riderName} has reached ${cur.customer}'s location`, 'info', 3000); } }); } prevStatusMapRef.current = currentMap; }, [visibleRiders]); if (isLoading) { return (