freshyo/apps/admin-web/src/routes/dashboard.slots.detail.tsx
2026-09-06 23:05:20 +05:30

239 lines
10 KiB
TypeScript

import React, { useState } from 'react'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { p as MyText, BottomDialog } from 'web-components'
import { trpc } from '@/lib/trpc-client'
import { ErrorToast } from '@/services/toaster'
import { theme } from '@/lib/theme'
import { Code2, ShoppingBag, Link2, Clock, Package, Pencil } from 'lucide-react'
import dayjs from 'dayjs'
export const Route = createFileRoute('/dashboard/slots/detail')({
validateSearch: (s: Record<string, unknown>) => ({
slotId: (s.slotId as string) ?? ''
}),
component: SlotDetails,
})
function SlotDetails() {
const navigate = useNavigate()
const { slotId } = Route.useSearch()
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<any[]>([])
if (isLoading) {
return (
<div className="flex flex-1 flex-col items-center justify-center bg-white">
<MyText className="text-gray-600">Loading slot details...</MyText>
</div>
)
}
if (error || !slot) {
return (
<div className="flex flex-1 flex-col items-center justify-center bg-white">
<MyText className="text-red-600">Error loading slot details</MyText>
</div>
)
}
return (
<div className="relative flex-1 bg-white px-4">
<div className="flex-1 overflow-y-auto pb-32">
{/* Header */}
<div className="mb-6">
<MyText className="mb-2 block text-2xl font-black text-gray-900">
Slot #{slot.id}
</MyText>
<div className="flex flex-row items-center justify-between">
<div className="flex-1">
<MyText className="mb-1 block text-sm text-gray-600">
Delivery: {dayjs(slot.deliveryTime).format('MMM DD, YYYY hh:mm A')}
</MyText>
<MyText className="block text-sm text-gray-600">
Freeze: {dayjs(slot.freezeTime).format('MMM DD, YYYY hh:mm A')}
</MyText>
</div>
<span className={`rounded-full px-3 py-1 ${slot.isActive ? 'bg-green-100' : 'bg-red-100'}`}>
<MyText className={`text-xs font-bold ${slot.isActive ? 'text-green-700' : 'text-red-700'}`}>
{slot.isActive ? 'Active' : 'Inactive'}
</MyText>
</span>
</div>
</div>
{/* Vendor Snippets Section */}
<div className="mb-8">
<div className="mb-4 flex flex-row items-center justify-between">
<MyText className="text-lg font-bold text-gray-900">Vendor Snippets</MyText>
<span className="rounded-full bg-gray-100 px-2 py-1">
<MyText className="text-xs font-medium text-gray-600">{vendorSnippets.length} snippets</MyText>
</span>
</div>
{vendorSnippets.length === 0 ? (
<div className="items-center rounded-lg bg-gray-50 p-6 text-center">
<Code2 size={32} color="#9CA3AF" className="mx-auto" />
<MyText className="mt-2 block text-center text-gray-500">No vendor snippets for this slot</MyText>
</div>
) : (
<div className="flex flex-col gap-2">
{vendorSnippets.map((snippet, index) => {
const isExpired = snippet.validTill && dayjs(snippet.validTill).isBefore(dayjs())
return (
<div key={snippet.id} className="rounded-lg bg-gray-50 p-4">
<div className="mb-3 flex flex-row items-center justify-between">
<div className="flex-1">
<MyText className="block font-bold text-gray-900" numberOfLines={1}>
{snippet.snippetCode}
</MyText>
</div>
<div className="flex flex-row items-center">
<span className={`rounded-full px-2 py-1 ${isExpired ? 'bg-red-100' : 'bg-green-100'}`}>
<MyText className={`text-xs font-medium ${isExpired ? 'text-red-700' : 'text-green-700'}`}>
{isExpired ? 'Expired' : 'Active'}
</MyText>
</span>
</div>
</div>
<div className="flex flex-row items-center justify-between">
<div className="flex flex-row items-center">
<button
onClick={() => {
const snippetProducts = products.filter((p) => snippet.skuIds.includes(p.id))
setDialogProducts(snippetProducts)
setDialogOpen(true)
}}
className="mr-4 flex flex-row items-center"
>
<ShoppingBag size={14} color="#94A3B8" />
<MyText className="ml-1 text-xs font-medium text-blue-600 underline">
{snippet.skuIds.length} products
</MyText>
</button>
<button
onClick={async () => {
try {
if (navigator.share) {
await navigator.share({ text: snippet.accessUrl })
} else {
await navigator.clipboard.writeText(snippet.accessUrl)
window.alert('Link copied to clipboard')
}
} catch (err) {
ErrorToast('Failed to share link')
}
}}
className="flex flex-row items-center"
>
<Link2 size={14} color={theme.colors.brand500} />
<MyText className="ml-1 text-xs font-medium text-brand-600">
Share
</MyText>
</button>
</div>
{snippet.validTill && (
<span className="flex flex-row items-center">
<Clock size={14} color="#94A3B8" />
<MyText className="ml-1 text-xs font-medium text-gray-600">
Expires {dayjs(snippet.validTill).format('MMM DD')}
</MyText>
</span>
)}
</div>
{snippet.createdAt && (
<div className="mt-2 border-t border-gray-200 pt-2">
<MyText className="block text-xs text-gray-500">
Created {dayjs(snippet.createdAt).format('MMM DD, YYYY')}
</MyText>
</div>
)}
</div>
)
})}
</div>
)}
</div>
{/* Products Section */}
<div className="mb-8">
<div className="mb-4 flex flex-row items-center justify-between">
<MyText className="text-lg font-bold text-gray-900">Products</MyText>
<span className="rounded-full bg-gray-100 px-2 py-1">
<MyText className="text-xs font-medium text-gray-600">{products.length} items</MyText>
</span>
</div>
{products.length === 0 ? (
<div className="items-center rounded-lg bg-gray-50 p-6 text-center">
<Package size={32} color="#9CA3AF" className="mx-auto" />
<MyText className="mt-2 block text-center text-gray-500">No products in this slot</MyText>
</div>
) : (
<div className="flex flex-row flex-wrap gap-2 py-2">
{products.map((product) => (
<span key={product.id} className="rounded-full border border-blue-200 bg-blue-50 px-3 py-2">
<MyText className="text-sm font-medium text-blue-700" numberOfLines={1}>
{product.name}
</MyText>
</span>
))}
</div>
)}
</div>
</div>
{/* FAB for Edit Slot */}
<button
onClick={() => navigate({ to: '/dashboard/slots/$id/edit', params: { id: String(slot.id) } })}
aria-label="Edit slot"
className="fixed flex h-16 w-16 items-center justify-center rounded-[24px] shadow-lg"
style={{ bottom: 32, right: 24, zIndex: 100, background: 'linear-gradient(135deg,#F83758,#E91E63)' }}
>
<Pencil size={24} color="white" />
</button>
<BottomDialog open={dialogOpen} onClose={() => setDialogOpen(false)}>
<div className="py-4">
<MyText className="mb-4 block text-center text-lg font-semibold">Snippet Products</MyText>
<div className="max-h-80 overflow-y-auto">
{dialogProducts.length === 0 ? (
<div className="items-center px-4 py-8 text-center">
<MyText className="block text-center text-gray-500">
No products found for this snippet
</MyText>
</div>
) : (
dialogProducts.map((product) => (
<div key={product.id} className="border-b border-gray-100 px-4 py-3">
<MyText className="block text-base font-medium text-gray-900">
{product.name}
</MyText>
{product.shortDescription && (
<MyText className="mt-1 block text-sm text-gray-600">
{product.shortDescription}
</MyText>
)}
</div>
))
)}
</div>
</div>
</BottomDialog>
</div>
)
}