import React, { useState } from "react";
import { View, Dimensions, Image, Alert, ScrollView, StatusBar as RNStatusBar } from "react-native";
import { StatusBar as ExpoStatusBar } from 'expo-status-bar';
import { LinearGradient } from "expo-linear-gradient";
import { useRouter } from "expo-router";
import {
theme,
tw,
useManualRefresh,
useMarkDataFetchers,
LoadingDialog,
AppContainer,
MyTouchableOpacity,
MyText, MyTextInput, SearchBar, useStatusBarStore
} from "common-ui";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
import FontAwesome5 from "@expo/vector-icons/FontAwesome5";
import { Ionicons } from "@expo/vector-icons";
import ProductCard from "@/components/ProductCard";
import { trpc } from "@/src/trpc-client";
import { useGetCart } from "@/hooks/cart-query-hooks";
import { useProductSlotIdentifier } from "@/hooks/useProductSlotIdentifier";
import FloatingCartBar from "@/components/floating-cart-bar";
import AddressSelector from "@/components/AddressSelector";
import BannerCarousel from "@/components/BannerCarousel";
import TestingPhaseNote from "@/components/TestingPhaseNote";
import { useUserDetails } from "@/src/contexts/AuthContext";
import TabLayoutWrapper from "@/components/TabLayoutWrapper";
import { useNavigationStore } from "@/src/store/navigationStore";
import { useGetEssentialConsts } from "@/src/api-hooks/essential-consts.api";
dayjs.extend(relativeTime);
// import { StatusBar } from "expo-status-bar";
const { width: screenWidth } = Dimensions.get("window");
const itemWidth = screenWidth * 0.45; // 45% of screen width
const RenderStore = ({
item,
}: {
item: any;
}) => {
const router = useRouter();
const { setNavigatedFromHome, setSelectedStoreId } = useNavigationStore();
const handlePress = () => {
setNavigatedFromHome(true);
setSelectedStoreId(item.id);
router.push('/(drawer)/(tabs)/stores');
};
return (
{item.signedImageUrl ? (
) : (
)}
{item.name.replace(/^The\s+/i, "")}
);
};
// const headerColor = '#fedf89'
// const headerColor = '#444ce7'
const headerColor = '#f81260'
export default function Dashboard() {
const router = useRouter();
const userDetails = useUserDetails();
const [inputQuery, setInputQuery] = useState("");
const [searchQuery, setSearchQuery] = useState("");
const [selectedTagId, setSelectedTagId] = useState(null);
const [isLoadingDialogOpen, setIsLoadingDialogOpen] = useState(false);
const [gradientHeight, setGradientHeight] = useState(0);
const [stickyBarLayout, setStickyBarLayout] = useState({ y: 0, height: 0 });
const [whiteSectionLayout, setWhiteSectionLayout] = useState({ y: 0 });
const { backgroundColor } = useStatusBarStore();
const {
data: productsData,
isLoading,
error,
refetch,
} = trpc.common.product.getAllProductsSummary.useQuery({
searchQuery: searchQuery || undefined,
tagId: selectedTagId || undefined,
});
const { data: essentialConsts, isLoading: isLoadingConsts, error: constsError } = useGetEssentialConsts();
const { data: tagsData } = trpc.common.product.getDashboardTags.useQuery();
const { data: cartData, refetch: refetchCart } = useGetCart();
const { data: storesData } = trpc.user.stores.getStores.useQuery();
const { data: defaultAddressResponse } =
trpc.user.address.getDefaultAddress.useQuery();
const { data: slotsData } = trpc.user.slots.getSlotsWithProducts.useQuery();
const products = productsData?.products || [];
const dashboardTags = tagsData?.tags || [];
const defaultAddress = defaultAddressResponse?.data;
const { getQuickestSlot } = useProductSlotIdentifier();
// Extract popular items IDs as an array to preserve order
const popularItemIds = (() => {
const popularItems = essentialConsts?.popularItems;
if (!popularItems) return [];
if (Array.isArray(popularItems)) {
// Already an array of IDs
return popularItems.map((id: any) => parseInt(id)).filter((id: number) => !isNaN(id));
} else if (typeof popularItems === 'string') {
// Comma-separated string
return popularItems
.split(',')
.map((id: string) => parseInt(id.trim()))
.filter((id: number) => !isNaN(id));
}
return [];
})();
// Filter products to only include those whose ID exists in popularItemIds, preserving order
// Only filter when both products and essentialConsts are loaded
const popularProducts = popularItemIds
.map(id => products.find(product => product.id === id))
.filter((product): product is NonNullable => product != null);
useManualRefresh(() => {
refetch();
});
useMarkDataFetchers(() => {
refetch();
});
const handleScroll = (event: any) => {
// const scrollY = event.nativeEvent.contentOffset.y;
// const stickyBarBottom = stickyBarLayout.y + stickyBarLayout.height;
// const whiteSectionTop = whiteSectionLayout.y;
// const shouldBeWhite = scrollY + stickyBarBottom >= whiteSectionTop;
// if (shouldBeWhite) {
// updateStatusBarColor('dark', '#ffffff');
// } else {
// updateStatusBarColor('light', headerColor);
// }
};
// React.useFocu(() => {
// // Initial status bar color
// return () => updateStatusBarColor('dark', '#ffffff');
// }, []);
if (isLoading || isLoadingConsts) {
return (
{isLoading ? 'Loading products...' : 'Loading app settings...'}
);
}
if (error || constsError) {
return (
Oops!
{error ? 'Failed to load products' : 'Failed to load app settings'}
);
}
return (
{/* */}
router.push("/(drawer)/(tabs)/me")}
style={tw`bg-white/10 rounded-full border border-white/10`}
>
{userDetails?.profileImage ? (
) : (
)}
{
const { y, height } = event.nativeEvent.layout;
setStickyBarLayout({ y, height });
}}
>
{ }}
onPress={() => router.push("/(drawer)/(tabs)/home/search-results")}
editable={false}
containerStyle={tw` bg-white`}
onSubmitEditing={() => {
if (inputQuery.trim()) {
router.push(
`/(drawer)/(tabs)/home/search-results?q=${encodeURIComponent(
inputQuery.trim()
)}`
);
}
}}
returnKeyType="search"
/>
{
const { y, height } = event.nativeEvent.layout;
setGradientHeight(y + height);
}}
>
{/* Stores Section */}
{storesData?.stores && storesData.stores.length > 0 && (
Our Stores
Fresh from our locations
{storesData.stores.map((store, index) => (
))}
)}
{/* Banner Carousel */}
{/* White Section */}
{
const { y } = event.nativeEvent.layout;
setWhiteSectionLayout({ y });
}}
>
{/* Section Title */}
Popular Items
Trending fresh picks just for you
{popularProducts.map((item, index: number) => (
router.push(
`/(drawer)/(tabs)/home/product-detail/${item.id}`
)
}
showDeliveryInfo={false}
miniView={true}
/>
))}
{/* Upcoming Deliveries Section */}
{slotsData?.slots && slotsData.slots.length > 0 && (
Delivery Slots
Plan your fresh deliveries ahead
{slotsData.slots.slice(0, 5).map((slot) => {
const now = dayjs();
const freezeTime = dayjs(slot.freezeTime);
const isClosingSoon =
freezeTime.diff(now, "hour") < 4 && freezeTime.isAfter(now);
return (
router.push(
`/(drawer)/(tabs)/home/slot-view?slotId=${slot.id}`
)
}
activeOpacity={0.9}
>
{/* Status Badge */}
{isClosingSoon && (
CLOSING SOON
)}
{/* Main Time Grid */}
Delivery At
{dayjs(slot.deliveryTime).format("h:mm A")}
{dayjs(slot.deliveryTime).format("ddd, MMM DD")}
Order By
{dayjs(slot.freezeTime).format("h:mm A")}
{dayjs(slot.freezeTime).format("ddd, MMM DD")}
{/* Product Teaser */}
{slot.products.slice(0, 3).map((p, i) => (
0 && tw`-ml-3`,
]}
>
{p.images?.[0] ? (
) : (
)}
))}
View all {slot.products.length} items
);
})}
)}
);
}