import React, { useState } from 'react'; import { View, ScrollView, TouchableOpacity, Alert, Share } from 'react-native'; import { theme, AppContainer, MyText, tw, MyTouchableOpacity, BottomDialog } from 'common-ui'; import { trpc } from '../../../src/trpc-client'; import MaterialIcons from '@expo/vector-icons/MaterialIcons'; import dayjs from "dayjs"; import { useRouter, useLocalSearchParams } from 'expo-router'; import { LinearGradient } from 'expo-linear-gradient'; export default function SlotDetails() { const router = useRouter(); const { slotId } = useLocalSearchParams(); const { data: slotData, isLoading, error } = trpc.admin.slots.getSlotById.useQuery({ id: parseInt(slotId as string), }); const slot = slotData?.slot; const products = slot?.products || []; const vendorSnippets = slot?.vendorSnippets || []; // Dialog state for snippet products const [dialogOpen, setDialogOpen] = useState(false); const [dialogProducts, setDialogProducts] = useState([]); if (isLoading) { return ( Loading slot details... ); } if (error || !slot) { return ( Error loading slot details ); } return ( {/* Header */} Slot #{slot.id} Delivery: {dayjs(slot.deliveryTime).format('MMM DD, YYYY hh:mm A')} Freeze: {dayjs(slot.freezeTime).format('MMM DD, YYYY hh:mm A')} {slot.isActive ? 'Active' : 'Inactive'} {/* Vendor Snippets Section */} Vendor Snippets {vendorSnippets.length} snippets {vendorSnippets.length === 0 ? ( No vendor snippets for this slot ) : ( {vendorSnippets.map((snippet, index) => { const isExpired = snippet.validTill && dayjs(snippet.validTill).isBefore(dayjs()); return ( {snippet.snippetCode} {isExpired ? 'Expired' : 'Active'} { const snippetProducts = products.filter(p => snippet.productIds.includes(p.id)); setDialogProducts(snippetProducts); setDialogOpen(true); }} style={tw`flex-row items-center mr-4`} > {snippet.productIds.length} products { try { await Share.share({ message: snippet.accessUrl, }); } catch (error) { Alert.alert('Error', 'Failed to share link'); } }} style={tw`flex-row items-center`} > Share {snippet.validTill && ( Expires {dayjs(snippet.validTill).format('MMM DD')} )} {snippet.createdAt && ( Created {dayjs(snippet.createdAt).format('MMM DD, YYYY')} )} ); })} )} {/* Products Section */} Products {products.length} items {products.length === 0 ? ( No products in this slot ) : ( {products.map((product) => ( {product.name} ))} )} {/* FAB for Edit Slot */} router.push(`/edit-slot/${slot.id}` as any)} activeOpacity={0.95} style={{ position: 'absolute', bottom: 32, right: 24, zIndex: 100 }} > setDialogOpen(false)}> Snippet Products {dialogProducts.length === 0 ? ( No products found for this snippet ) : ( dialogProducts.map(product => ( {product.name} {product.shortDescription && ( {product.shortDescription} )} )) )} ); }