Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 'use client'; /** * useOrderTracking Hook * * Real-time order status tracking via WebSocket. * Allows customers to track their order status updates in real-time. */ import { useEffect, useState, useCallback, useRef } from 'react'; import { useSocket } from './useSocket'; import type { OrderUpdatePayload } from '@/lib/socket/types'; export interface StatusUpdate { /** Order status */ status: string; /** Update timestamp */ timestamp: string; /** Status message */ message: string; /** Optional tracking number */ trackingNumber?: string; } export interface UseOrderTrackingReturn { /** Current order status */ currentStatus: string | null; /** Status update history */ statusHistory: StatusUpdate[]; /** Whether connected to real-time updates */ isConnected: boolean; /** Whether actively tracking this order */ isTracking: boolean; /** Tracking number if available */ trackingNumber: string | null; /** Latest status message */ latestMessage: string | null; /** Start tracking an order */ startTracking: () => void; /** Stop tracking */ stopTracking: () => void; /** Clear tracking history */ clearHistory: () => void; /** Connection error if any */ error: Error | null; } export interface UseOrderTrackingOptions { /** Auto-start tracking when connected (default: true) */ autoStart?: boolean; /** Maximum history entries to keep (default: 50) */ maxHistory?: number; } const DEFAULT_OPTIONS: UseOrderTrackingOptions = { autoStart: true, maxHistory: 50, }; export function useOrderTracking( orderId: string, options: UseOrderTrackingOptions = {} ): UseOrderTrackingReturn { const opts = { ...DEFAULT_OPTIONS, ...options }; const { isConnected, on, socket, error } = useSocket(); const [currentStatus, setCurrentStatus] = useState<string | null>(null); const [statusHistory, setStatusHistory] = useState<StatusUpdate[]>([]); const [isTracking, setIsTracking] = useState(false); const [trackingNumber, setTrackingNumber] = useState<string | null>(null); const [latestMessage, setLatestMessage] = useState<string | null>(null); // Track if we've already started tracking to prevent duplicate joins const hasJoinedRef = useRef(false); /** * Start tracking an order */ const startTracking = useCallback(() => { if (!orderId || !socket?.connected) { return; } if (hasJoinedRef.current) { return; // Already tracking } socket.emit('joinOrder', orderId); setIsTracking(true); hasJoinedRef.current = true; }, [orderId, socket]); /** * Stop tracking an order */ const stopTracking = useCallback(() => { if (!orderId || !socket?.connected) { return; } if (!hasJoinedRef.current) { return; // Not tracking } socket.emit('leaveOrder', orderId); setIsTracking(false); hasJoinedRef.current = false; }, [orderId, socket]); /** * Clear status history */ const clearHistory = useCallback(() => { setStatusHistory([]); setCurrentStatus(null); setTrackingNumber(null); setLatestMessage(null); }, []); /** * Auto-start tracking when connected */ useEffect(() => { if (opts.autoStart && isConnected && orderId && !hasJoinedRef.current) { // Perform socket operation directly in effect to avoid setState cascade if (socket?.connected) { socket.emit('joinOrder', orderId); hasJoinedRef.current = true; // Use functional update to batch state change queueMicrotask(() => setIsTracking(true)); } } return () => { if (hasJoinedRef.current && socket?.connected) { socket.emit('leaveOrder', orderId); hasJoinedRef.current = false; } }; }, [opts.autoStart, isConnected, orderId, socket]); /** * Listen for order updates */ useEffect(() => { if (!isConnected || !isTracking) { return; } const unsubscribe = on('orderUpdate', (update: OrderUpdatePayload) => { // Only process updates for our order if (update.orderId !== orderId) { return; } // Update current status setCurrentStatus(update.status); setLatestMessage(update.message); // Update tracking number if provided if (update.trackingNumber) { setTrackingNumber(update.trackingNumber); } // Add to history setStatusHistory((prev) => { const newEntry: StatusUpdate = { status: update.status, timestamp: update.timestamp, message: update.message, trackingNumber: update.trackingNumber, }; // Keep only maxHistory entries const updated = [newEntry, ...prev]; if (updated.length > (opts.maxHistory || 50)) { return updated.slice(0, opts.maxHistory); } return updated; }); }); return unsubscribe; }, [isConnected, isTracking, orderId, on, opts.maxHistory]); /** * Reset tracking state when orderId changes * Use refs and microtask to avoid synchronous setState in effect */ useEffect(() => { hasJoinedRef.current = false; // Defer state updates to avoid cascading renders queueMicrotask(() => { setIsTracking(false); setStatusHistory([]); setCurrentStatus(null); setTrackingNumber(null); setLatestMessage(null); }); }, [orderId]); return { currentStatus, statusHistory, isConnected, isTracking, trackingNumber, latestMessage, startTracking, stopTracking, clearHistory, error, }; } export default useOrderTracking; |