'use client' import { useEffect, useState, useCallback } from 'react' import { toast } from 'sonner' import { listEquipmentReservations, cancelReservation } from '@/lib/actions' type Reservation = { id: string member_id: string starts_at: string ends_at: string status: string notes: string | null space_members: { display_name: string | null; email: string | null } | null } function who(r: Reservation) { return r.space_members?.display_name ?? r.space_members?.email ?? 'Member' } // Manager view of who reserved a piece of equipment, with a manager cancel. // listEquipmentReservations is equipment.manage-gated server-side. export function EquipmentReservations({ equipmentId }: { equipmentId: string }) { const [loading, setLoading] = useState(false) const [rows, setRows] = useState([]) const [busy, setBusy] = useState(null) const load = useCallback(async () => { setLoading(true) const res = await listEquipmentReservations({ equipmentId }) setLoading(false) if ('error' in res && res.error) { toast.error(res.error) return } setRows(('data' in res ? res.data : []) ?? []) }, [equipmentId]) useEffect(() => { load() }, [load]) async function onCancel(r: Reservation) { setBusy(r.id) const res = await cancelReservation({ reservationId: r.id }) setBusy(null) if ('error' in res && res.error) { toast.error(res.error) return } setRows(prev => prev.map(x => (x.id === r.id ? { ...x, status: 'cancelled' } : x))) toast.success('Reservation cancelled') } return (

Reservations

{loading ? (

Loading…

) : rows.length === 0 ? (

No reservations.

) : (
    {rows.map(r => (
  • {who(r)} {new Date(r.starts_at).toLocaleString()} – {new Date(r.ends_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} {' · '} {r.status} {r.notes &&

    {r.notes}

    }
    {r.status !== 'cancelled' && ( )}
  • ))}
)}
) }