"use client"; import React, { useCallback, useEffect, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { errorMessage } from "@/lib/utils"; import { formatDate } from "@/lib/date"; import { useDismissingState } from "@/hooks/useDismissingState"; import { BackLink } from "@/components/shared/BackLink"; import { CheckCircle2, HandHeart, QrCode, Repeat } from "lucide-react"; import type { CoverRequest, MySlot, Roster, ServingEvent } from "@/lib/volunteers"; import { apiV2Data } from "@/lib/apiV2"; type Mine = { slots: MySlot[]; cover: CoverRequest[]; events: ServingEvent[] }; type Msg = { type: "ok" | "err"; text: string } | null; const btn = "px-3 py-1.5 rounded-lg text-sm border disabled:opacity-50"; /** My account → Serving: my shifts, cover requests I could take, and events looking for volunteers. */ export default function ServingPage() { const { token } = useAuth(); const [data, setData] = useState(null); const [msg, setMsg] = useDismissingState(null); const [busy, setBusy] = useState(false); const [openEvent, setOpenEvent] = useState(""); const [roster, setRoster] = useState(null); const load = useCallback(async () => { if (!token) return; try { setData(await (apiV2Data("get", "/me/volunteering", { authToken: token }) as Promise)); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not load your shifts" }); } }, [token, setMsg]); useEffect(() => { load(); }, [load]); // ?event=: the sign-up link a supervisor shares (works for hidden events too). useEffect(() => { const wanted = new URLSearchParams(window.location.search).get("event"); if (wanted) setOpenEvent(wanted); }, []); const loadRoster = useCallback(async (id: string) => { if (!token || !id) { setRoster(null); return; } try { setRoster(await (apiV2Data("get", "/events/{id}/volunteer-roles", { params: { id: id }, authToken: token }) as Promise)); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not load the roles" }); } }, [token, setMsg]); useEffect(() => { loadRoster(openEvent); }, [openEvent, loadRoster]); const call = async (fn: () => Promise, ok: string) => { setBusy(true); try { await fn(); setMsg({ type: "ok", text: ok }); await Promise.all([load(), openEvent ? loadRoster(openEvent) : null]); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Something went wrong" }); } finally { setBusy(false); } }; const askCover = (s: MySlot) => { const note = prompt(`Ask the other volunteers to take over ${s.role.name}? The first to accept takes your place. Add a note (optional):`, ""); if (note === null) return; call(() => apiV2Data("post", "/volunteer-slots/{id}/swap", { params: { id: s.slotId }, authToken: token, body: { note } }), "Cover requested — you stay on the roster until someone takes over."); }; const withdraw = (s: MySlot) => confirm(`Withdraw from ${s.role.name} at ${s.event.title}? The organisers will be told.`) && call(() => apiV2Data("delete", "/volunteer-slots/{id}", { params: { id: s.slotId }, authToken: token }), "You're off the roster for this shift."); const upcoming = (data?.slots || []).filter(s => !s.past); const past = (data?.slots || []).filter(s => s.past); return (

Serving

Your volunteer shifts, and events looking for people to serve

{msg &&
{msg.text}
} {!data &&
Loading…
} {data && (

My shifts

{upcoming.length === 0 &&

You're not on any roster yet. Sign up for a role below.

} {upcoming.map(s => (
{s.role.name} · {s.event.title}
{s.role.sessionLabel ? `${s.role.sessionLabel} · ` : ""}{s.shift.text}{s.event.location ? ` · ${s.event.location}` : ""}
{s.role.kidsRoom &&
Kids room: {s.role.kidsRoom}
} {s.role.description &&
{s.role.description}
} {s.hasTicket &&
Your free volunteer ticket is in My Events.
}
{s.checkedInAt ? Checked in : s.swapId && Cover requested}
{s.canCheckIn &&

When you arrive, scan the volunteer check-in QR code with your phone's camera.

} {!s.checkedInAt && (
{s.swapId ? : }
)}
))} {past.length > 0 && (
Last week ({past.length})
    {past.map(s =>
  • {s.role.name} · {s.event.title} · {s.shift.text}{s.checkedInAt ? " · served" : ""}
  • )}
)}
)} {data && data.cover.length > 0 && (

Can you take over?

{data.cover.map(c => (
{c.role.name} · {c.event.title}
{c.role.sessionLabel ? `${c.role.sessionLabel} · ` : ""}{c.shift.text}
{c.from || "A volunteer"} can't make it{c.note ? `: “${c.note}”` : ""}
))}
)} {data && (

Serve at an event

{data.events.length === 0 && !openEvent &&

No events are looking for volunteers right now.

} {(data.events.length > 0 || openEvent) && ( )} {roster && roster.event.id === openEvent && (
{roster.roles.filter(r => !r.past).map(r => (
{r.name}{r.kidsRoom ? · kids room {r.kidsRoom.name} : null}
{r.sessionLabel ? `${r.sessionLabel} · ` : ""}{r.shift.text}
{r.description &&
{r.description}
}
{r.filled} of {r.needed} signed up
{r.mine ? You're serving : r.coverNeeded.length > 0 ? : r.full ? Full : }
))} {roster.roles.every(r => r.past) &&

All shifts at this event are over.

}
)}
)}
); }