Files
crosscode-platform/apps/events/frontend/src/components/dashboard/TodayPanel.tsx
T
joshuaandClaude Opus 5.5 abd2c36e97 WIP 2.0.0: React Compiler lint rules back to errors; docs
- The 192 places the rules flagged are fixed and the rules are errors
  again (npx eslint src: nothing). lib/hooks.ts: useStateSync (state
  that follows other state, set while rendering), useExternalEffect
  (API loads and browser reads), useNow; lib/navigation.ts hardNavigate
  for full reloads after sign-out or identity switches; other pages use
  the router. Resets moved into the handlers that cause them.
- The banner shows and hides at its times on open pages.
- HANDOVER, roadmap, README, FEATURES, SETTINGS, CHANGELOG updated;
  2.0.0 section drafted in WHATS-NEW.

e2e 102/102 (PayFast sandbox skipped); production build passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 17:45:25 +02:00

124 lines
6.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import React, { useCallback, useState } from "react";
import Link from "next/link";
import { CalendarClock, ArrowRight, Baby, HandHeart, UserCheck, Banknote, Inbox, CheckCircle2 } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { useVisiblePolling } from "@/hooks/useVisiblePolling";
import { CampusBadge } from "@/components/shared/CampusFilter";
import type { Event } from "@/types";
import { apiV2Data } from "@/lib/apiV2";
import { useExternalEffect } from "@/lib/hooks";
type TodayEvent = {
id: string; title: string; location: string | null; campus: Event["campus"]; isHidden: boolean; kidsCheckIn: boolean;
startsAt: string; endsAt: string; sessionLabel: string | null;
places: number; checkedIn: number; kidsIn: number | null;
volunteers: { needed: number; rostered: number; checkedIn: number } | null;
};
type Pending = { key: string; label: string; count: number; href: string; tone: string };
type Today = { events: TodayEvent[]; money: { today: number; todayCount: number; week: number } | null; pending: Pending[] };
const TONE: Record<string, string> = {
amber: "bg-amber-50 text-amber-800", rose: "bg-rose-50 text-rose-700", blue: "bg-blue-50 text-blue-700",
violet: "bg-violet-50 text-violet-700", green: "bg-green-50 text-green-700",
};
const time = (d: string) => new Date(d).toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
const rand = (n: number) => `R ${n.toFixed(2)}`;
/**
* The top of every staff dashboard (1.49.0): what's on today and how it's going, and — for
* supervisors and admins — money in and what's waiting for someone, each linking to where it's
* dealt with. Refreshes every 30 seconds while the tab is visible.
*/
export function TodayPanel({ supervisor }: { supervisor: boolean }) {
const { token } = useAuth();
const [data, setData] = useState<Today | null>(null);
const load = useCallback(async () => {
if (!token) return;
try { setData(await (apiV2Data("get", "/dashboard/today", { authToken: token }) as Promise<Today>)); } catch { /* keep what's shown */ }
}, [token]);
useExternalEffect(() => { load(); }, [load]);
useVisiblePolling(load, 30000, !!token);
if (!data) return null;
return (
<div className={`grid gap-6 mb-6 ${supervisor ? "lg:grid-cols-3" : ""}`} data-testid="today-panel">
<div className={`border rounded-xl p-4 bg-white shadow-sm min-w-0 ${supervisor ? "lg:col-span-2" : ""}`}>
<div className="flex items-center gap-2 mb-3">
<CalendarClock className="w-5 h-5 text-brand-600" />
<h2 className="text-lg font-semibold">Today</h2>
<span className="text-xs text-gray-500">{new Date().toLocaleDateString(undefined, { weekday: "long", day: "numeric", month: "long" })}</span>
</div>
{data.events.length === 0 ? (
<p className="text-sm text-gray-500">Nothing on today.</p>
) : (
<ul className="divide-y" data-testid="today-events">
{data.events.map(e => {
const pct = e.places ? Math.min(100, Math.round((e.checkedIn / e.places) * 100)) : 0;
return (
<li key={e.id} className="py-3 first:pt-0 last:pb-0">
<div className="flex flex-wrap items-start justify-between gap-2">
<div className="min-w-0">
<div className="font-medium text-gray-900 flex items-center gap-2 flex-wrap">
{e.title}{e.sessionLabel && <span className="text-xs font-normal text-gray-500">· {e.sessionLabel}</span>}
<CampusBadge campus={e.campus} />
</div>
<div className="text-xs text-gray-500">{time(e.startsAt)}–{time(e.endsAt)}{e.location ? ` · ${e.location}` : ""}</div>
</div>
<Link href={e.kidsCheckIn ? "/dashboard/staff/kids" : `/dashboard/staff/ticket-scanning`} className="inline-flex items-center gap-1 text-xs text-brand-700 hover:underline shrink-0">
{e.kidsCheckIn ? "Kids desk" : "Scanner"} <ArrowRight className="w-3 h-3" />
</Link>
</div>
<div className="mt-2 flex flex-wrap gap-x-4 gap-y-1 text-xs text-gray-600">
{!e.kidsCheckIn && (
<span className="inline-flex items-center gap-1.5">
<UserCheck className="w-3.5 h-3.5" /> {e.checkedIn} of {e.places} checked in
<span className="inline-block w-20 h-1.5 rounded bg-gray-100 overflow-hidden"><span className="block h-full bg-brand-500" style={{ width: `${pct}%` }} /></span>
</span>
)}
{e.kidsIn !== null && <span className="inline-flex items-center gap-1"><Baby className="w-3.5 h-3.5" /> {e.kidsIn} children in now</span>}
{e.volunteers && (
<span className="inline-flex items-center gap-1"><HandHeart className="w-3.5 h-3.5" /> {e.volunteers.checkedIn} of {e.volunteers.rostered} volunteers here{e.volunteers.rostered < e.volunteers.needed ? ` (${e.volunteers.needed - e.volunteers.rostered} short)` : ""}</span>
)}
</div>
</li>
);
})}
</ul>
)}
{data.money && (
<div className="mt-4 pt-3 border-t flex flex-wrap gap-x-6 gap-y-1 text-sm">
<span className="inline-flex items-center gap-1.5"><Banknote className="w-4 h-4 text-green-600" /> Today <strong>{rand(data.money.today)}</strong> <span className="text-xs text-gray-500">({data.money.todayCount} payment{data.money.todayCount === 1 ? "" : "s"})</span></span>
<span>Last 7 days <strong>{rand(data.money.week)}</strong></span>
</div>
)}
</div>
{supervisor && (
<div className="border rounded-xl p-4 bg-white shadow-sm min-w-0">
<div className="flex items-center gap-2 mb-3">
<Inbox className="w-5 h-5 text-brand-600" />
<h2 className="text-lg font-semibold">Waiting for you</h2>
</div>
{data.pending.length === 0 ? (
<p className="text-sm text-gray-500 inline-flex items-center gap-1.5"><CheckCircle2 className="w-4 h-4 text-green-600" /> All caught up.</p>
) : (
<ul className="space-y-2" data-testid="waiting">
{data.pending.map(p => (
<li key={p.key}>
<Link href={p.href} className="flex items-center gap-3 rounded-lg px-2 py-1.5 hover:bg-gray-50">
<span className={`min-w-[2rem] text-center text-sm font-semibold rounded-md px-1.5 py-0.5 ${TONE[p.tone] || "bg-gray-100 text-gray-700"}`}>{p.count}</span>
<span className="text-sm text-gray-700 flex-1">{p.label}</span>
<ArrowRight className="w-3.5 h-3.5 text-gray-400" />
</Link>
</li>
))}
</ul>
)}
</div>
)}
</div>
);
}