import React, { useEffect, useRef } from 'react'; import { View, Text, ScrollView, TouchableOpacity, Alert, Switch } from 'react-native'; import { useAppTheme, borderRadius, spacing, shadow, typography } from '@theme'; import { PrimaryButton } from '@components'; import { BellIcon, PersonIcon, WalletIcon, ClipboardIcon } from '@icons'; import { useAppDispatch, useAppSelector } from '@store'; import { setOnlineStatus } from '@store/slices/authSlice'; import { setNewJobOffer } from '@store/slices/jobSlice'; import { mockJobOffer } from '@store/mockData/mockJobs'; import { useNavigation } from '@react-navigation/native'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { AppStackParamList } from '@navigation/navigationTypes'; import { RouteNames, JobStatus } from '@utils/constants'; import { formatCurrency } from '@utils/formatters'; import { getStyles } from './dashboardScreen.styles'; const DashboardScreen: React.FC = () => { const { colors } = useAppTheme(); const styles = getStyles(colors); const dispatch = useAppDispatch(); const navigation = useNavigation>(); const isOnline = useAppSelector((state) => state.auth.isOnline); const { todayEarnings, completedCount, onlineMinutes, weeklyData } = useAppSelector( (state) => state.earnings ); const { jobStatus, activeJob } = useAppSelector((state) => state.job); const timerRef = useRef | null>(null); // Background simulation: when online and idle, trigger new order offer after 12 seconds useEffect(() => { if (isOnline && jobStatus === JobStatus.Idle) { timerRef.current = setTimeout(() => { dispatch(setNewJobOffer(mockJobOffer)); navigation.navigate(RouteNames.NewJobRequest); }, 12000); } else { if (timerRef.current) { clearTimeout(timerRef.current); timerRef.current = null; } } return () => { if (timerRef.current) { clearTimeout(timerRef.current); } }; }, [isOnline, jobStatus, dispatch, navigation]); const handleToggleOnline = () => { dispatch(setOnlineStatus(!isOnline)); }; const handleSimulateOffer = () => { if (!isOnline) { Alert.alert('Go Online', 'Please go online to receive order offers.'); return; } if (jobStatus !== JobStatus.Idle) { Alert.alert('Active Delivery', 'You already have an active job offer or order.'); return; } dispatch(setNewJobOffer(mockJobOffer)); navigation.navigate(RouteNames.NewJobRequest); }; const handleActiveJobBannerPress = () => { // Navigate back to the screen corresponding to the current job step switch (jobStatus) { case JobStatus.NewRequest: navigation.navigate(RouteNames.NewJobRequest); break; case JobStatus.Accepted: navigation.navigate(RouteNames.OrderAccepted); break; case JobStatus.ArrivedAtStore: navigation.navigate(RouteNames.ConfirmPickup); break; case JobStatus.PickedUp: navigation.navigate(RouteNames.OrderPickedUp); break; case JobStatus.EnRoute: navigation.navigate(RouteNames.LiveTracking); break; case JobStatus.ArrivedAtCustomer: navigation.navigate(RouteNames.DeliverOrder); break; default: Alert.alert('Status Error', 'Unknown active delivery state.'); } }; const formatOnlineTime = (mins: number) => { const hours = Math.floor(mins / 60); const remainingMins = mins % 60; return `${hours}h ${remainingMins < 10 ? `0${remainingMins}` : remainingMins}m`; }; // Helper to render chart bar heights dynamically based on amounts const maxWeeklyAmount = Math.max(...weeklyData.map((d) => d.amount), 1); return ( {/* Header Section */} Good Morning ☀️ Rahul Kumar {isOnline ? 'Online' : 'Offline'} {/* Offline Warning Banner */} {!isOnline && ( You are offline. Go online to start receiving orders. )} {/* Today's Summary Card */} Today's Summary {formatCurrency(isOnline ? todayEarnings : 0)} Earnings {isOnline ? completedCount : 0} Completed {formatOnlineTime(isOnline ? onlineMinutes : 0)} Online Time {/* Conditional Layout for Online vs Offline */} {isOnline ? ( <> {/* Performance Stats */} Your Performance 4.8 ★ Rating 95% Acceptance 98% Completion {/* Weekly Earnings Chart (Styled SVG/Flex columns) */} Weekly Overview {weeklyData.map((data, index) => { // Bar height percent const heightPercent = `${(data.amount / maxWeeklyAmount) * 85}%`; return ( ₹{data.amount} {data.day} ); })} {/* Developer Simulation trigger */} ⚡ Simulate Immediate Job Offer ) : ( Go Online to start receiving delivery requests Make sure you are in a high-demand delivery zone to maximize your earnings. )} {/* Help & Support Cards at bottom */} Help & Support Alert.alert('Emergency SOS', 'Calling nearest security/emergency dispatch...')} > Emergency SOS Immediate Assistance navigation.navigate(RouteNames.BottomTabs, { screen: RouteNames.Profile })} > FAQ Center Help & Guides Alert.alert('Support Chat', 'Initiating live helpdesk agent chat...')} > Live Support 24/7 Chat Help {/* Active Job Floating Bottom Banner */} {jobStatus !== JobStatus.Idle && jobStatus !== JobStatus.NewRequest && activeJob && ( Order {activeJob.orderId} {jobStatus === JobStatus.Accepted && 'Heading to pickup store...'} {jobStatus === JobStatus.ArrivedAtStore && 'At pickup store, check items...'} {jobStatus === JobStatus.PickedUp && 'Order picked up, routing to customer...'} {jobStatus === JobStatus.EnRoute && 'On the way to customer...'} {jobStatus === JobStatus.ArrivedAtCustomer && 'Arrived at customer location...'} View )} ); }; export default DashboardScreen;