"use client"; import React, { useState } from "react"; import { apiFetch } from "@/lib/api"; import { errorMessage } from "@/lib/utils"; import { AlertTriangle, BellRing, HeartPulse, LogOut, ShieldAlert, Users } from "lucide-react"; import { ageRange, type KidsBoard, type KidsCheckIn, type KidsRoom } from "@/lib/kids"; type Props = { board: KidsBoard; token: string | null; /** Only this room ('' = every room). */ roomId: string; onChanged: () => void; onMessage: (m: { type: "ok" | "err"; text: string }) => void; }; const time = (iso: string) => new Date(iso).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); export function RoomStatusBadges({ room }: { room: KidsRoom }) { return ( {room.children}{room.capacity ? ` / ${room.capacity}` : ""} in {room.leaders} leader{room.leaders === 1 ? "" : "s"}{room.ratio ? ` (needs ${room.leadersNeeded})` : ""} {room.full && Full} {room.overRatio && Over ratio} ); } /** Live cards of the children in the rooms, with paging, moving and checking out without the code. */ export function RoomBoard({ board, token, roomId, onChanged, onMessage }: Props) { const [busy, setBusy] = useState(null); const rooms = board.rooms.filter(r => !roomId || r.id === roomId); const alerts = board.alerts.filter(a => !a.resolvedAt && a.kind === "wrong_code" && (!roomId || !a.roomId || a.roomId === roomId)); const act = async (id: string, fn: () => Promise, ok: string) => { setBusy(id); try { await fn(); onMessage({ type: "ok", text: ok }); onChanged(); } catch (e) { onMessage({ type: "err", text: errorMessage(e) || "Something went wrong" }); } finally { setBusy(null); } }; const page = (c: KidsCheckIn) => { const extra = prompt(`Message the parents of ${c.firstName}? They'll be asked to come to the room. Add a note (optional):`, ""); if (extra === null) return; act(c.id, () => apiFetch(`/api/kids/check-ins/${c.id}/page`, { method: "POST", authToken: token, body: { message: extra } }), `Message sent to ${c.firstName}'s parents.`); }; const override = (c: KidsCheckIn) => { const who = prompt(`Who is collecting ${c.firstName}?`, c.family?.parents[0]?.name || ""); if (!who) return; const reason = prompt("Why without the pickup code? (recorded in the audit log)", ""); if (!reason) return; act(c.id, () => apiFetch(`/api/kids/check-ins/${c.id}/override`, { method: "POST", authToken: token, body: { collectedBy: who, reason } }), `${c.firstName} checked out.`); }; const move = (c: KidsCheckIn, to: string) => act(c.id, () => apiFetch(`/api/kids/check-ins/${c.id}/room`, { method: "PUT", authToken: token, body: { roomId: to } }), `${c.firstName} moved.`); const resolve = (id: string) => act(id, () => apiFetch(`/api/kids/alerts/${id}/resolve`, { method: "POST", authToken: token }), "Alert cleared."); return ( {alerts.map(a => ( {a.message}{time(a.at)}{a.by ? ` · ${a.by}` : ""} — check who is collecting before handing over any child. resolve(a.id)} className="text-sm px-3 py-1 rounded-lg border border-red-300 bg-white">Dealt with ))} {rooms.map(r => { const inRoom = board.checkIns.filter(c => c.roomId === r.id && !c.checkedOutAt); const out = board.checkIns.filter(c => c.roomId === r.id && c.checkedOutAt); return ( {r.name} {ageRange(r)} {r.leaderNames.length > 0 && On duty: {r.leaderNames.join(", ")}} {inRoom.length === 0 && No children in this room.} {inRoom.map(c => ( {c.name}{c.age != null && · {c.age}} In {time(c.checkedInAt)} · code {c.code}{c.offline ? " · offline" : ""} {c.allergies && Allergies: {c.allergies}} {c.medicalNotes && {c.medicalNotes}} {c.custodyNotes && {c.custodyNotes}} {c.family && ( {c.family.parents.map(p => {p.name}{p.phone ? ` · ${p.phone}` : ""})} {c.family.adults.length > 0 && May also collect: {c.family.adults.map(a => `${a.name}${a.relationship ? ` (${a.relationship})` : ""}`).join(", ")}} )} page(c)} className="inline-flex items-center gap-1 text-xs px-2 py-1 rounded-lg border"> Call parent e.target.value && move(c, e.target.value)} className="text-xs border rounded-lg px-2 py-1"> Move to… {board.rooms.filter(o => o.id !== r.id).map(o => {o.name})} override(c)} className="inline-flex items-center gap-1 text-xs px-2 py-1 rounded-lg border text-gray-600"> Without code ))} {out.length > 0 && ( Collected ({out.length}) {out.map(c => {c.name} — {time(c.checkedOutAt!)} by {c.collectedBy || "—"}{c.override ? " (without code)" : ""})} )} ); })} {!roomId && board.checkIns.some(c => !c.roomId && !c.checkedOutAt) && ( No room Their room was deleted — choose a new one. {board.checkIns.filter(c => !c.roomId && !c.checkedOutAt).map(c => ( {c.name} e.target.value && move(c, e.target.value)} className="text-xs border rounded-lg px-2 py-1 bg-white"> Move to… {board.rooms.map(o => {o.name})} ))} )} ); }
No children in this room.
Their room was deleted — choose a new one.