'use client' import { useState } from 'react' import { toast } from 'sonner' import { Hammer } from 'lucide-react' import { PageHeader, PageTitle } from '@/components/ui/page-title' import { Badge } from '@/components/ui/badge' import { Empty, EmptyHeader, EmptyMedia, EmptyTitle, EmptyDescription } from '@/components/ui/empty' import { reserveEquipment } from '@/lib/actions' import { EQUIPMENT_STATUS_LABEL } from '@/lib/equipment-logic' import { useRouter } from 'next/navigation' type Item = { id: string name: string description: string | null location: string | null status: string required_certification_id: string | null certifications: { name: string } | { name: string }[] | null member_certified: boolean } function certName(i: Item): string | null { const c = Array.isArray(i.certifications) ? i.certifications[0] : i.certifications return c?.name ?? null } export function EquipmentBrowseClient({ equipment }: { equipment: unknown[] }) { const router = useRouter() const items = equipment as Item[] const [openId, setOpenId] = useState(null) const [form, setForm] = useState({ starts_at: '', ends_at: '', notes: '' }) const [busy, setBusy] = useState(false) async function onReserve(e: React.FormEvent, item: Item) { e.preventDefault() if (!form.starts_at || !form.ends_at) return toast.error('Pick a start and end time') setBusy(true) const res = await reserveEquipment({ equipmentId: item.id, starts_at: form.starts_at, ends_at: form.ends_at, notes: form.notes.trim() || null, }) setBusy(false) if ('error' in res && res.error) return toast.error(res.error) toast.success('Reserved') setOpenId(null) setForm({ starts_at: '', ends_at: '', notes: '' }) router.refresh() } return ( <> Equipment
{items.length === 0 ? ( No equipment listed Nothing is available to reserve yet. ) : ( )}
) }