Archived
Dashboard Today and Waiting for you; Ctrl+K command palette (1.49.0)
Staff, supervisor and admin dashboards start with what's on today (checked in vs places, children in now, volunteers here; money in today and this week for supervisors) and, for supervisors and admins, what's waiting for someone (unpaid registrations, consent, waitlists, cover requests, roster gaps; for admins cashups, reconciling, privacy requests, failed webhooks), each linking to where it's dealt with. Ctrl+K opens a palette of pages, settings, reports and actions for the role, with search: events, payments by amount, people and registrations (admins). All campus-scoped. Bulk actions and saved filters follow as 1.49.1 and 1.49.2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
"use client";
|
||||
|
||||
import React, { useCallback, useEffect, 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 { apiFetch } from "@/lib/api";
|
||||
import { useVisiblePolling } from "@/hooks/useVisiblePolling";
|
||||
import { CampusBadge } from "@/components/shared/CampusFilter";
|
||||
import type { Event } from "@/types";
|
||||
|
||||
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 apiFetch<Today>("/api/dashboard/today", { authToken: token })); } catch { /* keep what's shown */ }
|
||||
}, [token]);
|
||||
useEffect(() => { 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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
"use client";
|
||||
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Search, Calendar, User, Banknote, ClipboardList, CornerDownLeft, Zap, FileText, Settings, BarChart2 } from "lucide-react";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { apiFetch } from "@/lib/api";
|
||||
import { matchPages, type CommandPage } from "@/lib/commandPages";
|
||||
|
||||
type Result = { type: "event" | "payment" | "person" | "registration"; id: string; title: string; subtitle: string; href: string };
|
||||
type Item = { key: string; group: string; title: string; subtitle?: string; href: string; icon: React.ElementType };
|
||||
|
||||
const RESULT_GROUP: Record<Result["type"], [string, React.ElementType]> = {
|
||||
event: ["Events", Calendar], payment: ["Payments", Banknote], person: ["People", User], registration: ["Registrations", ClipboardList],
|
||||
};
|
||||
const PAGE_ICON: Record<CommandPage["group"], React.ElementType> = { Actions: Zap, Pages: FileText, Settings, Reports: BarChart2 };
|
||||
|
||||
/** Opens with Ctrl+K / ⌘K (or the event below) for staff, supervisors and admins. */
|
||||
export const OPEN_PALETTE_EVENT = "cross-events:open-palette";
|
||||
|
||||
/**
|
||||
* Ctrl+K command palette (1.49.0): jump to a page, setting or report, start an action ("New
|
||||
* event"), or find an event, a payment by amount ("R450"), a person or a registration — what the
|
||||
* person may see. ↑/↓ to move, Enter to open, Esc to close.
|
||||
*/
|
||||
export function CommandPalette() {
|
||||
const { user, token } = useAuth();
|
||||
const router = useRouter();
|
||||
const role = user?.role || "user";
|
||||
const allowed = role === "staff" || role === "supervisor" || role === "admin";
|
||||
const [open, setOpen] = useState(false);
|
||||
const [query, setQuery] = useState("");
|
||||
const [results, setResults] = useState<Result[]>([]);
|
||||
const [active, setActive] = useState(0);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const listRef = useRef<HTMLUListElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!allowed) return;
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "k") {
|
||||
e.preventDefault();
|
||||
setOpen(o => !o);
|
||||
}
|
||||
};
|
||||
const onOpen = () => setOpen(true);
|
||||
window.addEventListener("keydown", onKey);
|
||||
window.addEventListener(OPEN_PALETTE_EVENT, onOpen);
|
||||
return () => { window.removeEventListener("keydown", onKey); window.removeEventListener(OPEN_PALETTE_EVENT, onOpen); };
|
||||
}, [allowed]);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) { setQuery(""); setResults([]); setActive(0); setTimeout(() => inputRef.current?.focus(), 0); }
|
||||
}, [open]);
|
||||
|
||||
// Search as they type (after a short pause); pages are matched here, instantly.
|
||||
useEffect(() => {
|
||||
if (!open || !token) return;
|
||||
const q = query.trim();
|
||||
if (q.length < 2) { setResults([]); return; }
|
||||
const timer = setTimeout(() => {
|
||||
apiFetch<{ results: Result[] }>(`/api/dashboard/search?q=${encodeURIComponent(q)}`, { authToken: token })
|
||||
.then(r => setResults(r.results || []))
|
||||
.catch(() => setResults([]));
|
||||
}, 200);
|
||||
return () => clearTimeout(timer);
|
||||
}, [query, open, token]);
|
||||
|
||||
const items = useMemo<Item[]>(() => {
|
||||
const pages = matchPages(role, query).slice(0, query.trim() ? 8 : 10).map(p => ({ key: `page:${p.href}`, group: p.group, title: p.label, href: p.href, icon: PAGE_ICON[p.group] }));
|
||||
const found = results.map(r => ({ key: `${r.type}:${r.id}`, group: RESULT_GROUP[r.type][0], title: r.title, subtitle: r.subtitle, href: r.href, icon: RESULT_GROUP[r.type][1] }));
|
||||
return query.trim() ? [...found, ...pages] : pages;
|
||||
}, [role, query, results]);
|
||||
|
||||
useEffect(() => { setActive(a => Math.min(a, Math.max(0, items.length - 1))); }, [items.length]);
|
||||
useEffect(() => { listRef.current?.querySelector(`[data-index="${active}"]`)?.scrollIntoView({ block: "nearest" }); }, [active]);
|
||||
|
||||
const go = useCallback((item: Item | undefined) => {
|
||||
if (!item) return;
|
||||
setOpen(false);
|
||||
router.push(item.href);
|
||||
}, [router]);
|
||||
|
||||
if (!allowed || !open) return null;
|
||||
|
||||
const onKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === "Escape") { e.preventDefault(); setOpen(false); }
|
||||
else if (e.key === "ArrowDown") { e.preventDefault(); setActive(a => Math.min(items.length - 1, a + 1)); }
|
||||
else if (e.key === "ArrowUp") { e.preventDefault(); setActive(a => Math.max(0, a - 1)); }
|
||||
else if (e.key === "Enter") { e.preventDefault(); go(items[active]); }
|
||||
};
|
||||
|
||||
let lastGroup = "";
|
||||
return (
|
||||
<div className="fixed inset-0 z-[60] flex items-start justify-center bg-black/40 p-4 pt-[12vh]" onMouseDown={() => setOpen(false)}>
|
||||
<div role="dialog" aria-modal="true" aria-label="Go to or search" className="w-full max-w-xl rounded-xl bg-white shadow-2xl overflow-hidden" onMouseDown={e => e.stopPropagation()}>
|
||||
<div className="flex items-center gap-2 border-b px-3">
|
||||
<Search className="w-4 h-4 text-gray-400 shrink-0" />
|
||||
<input
|
||||
ref={inputRef} value={query} onChange={e => { setQuery(e.target.value); setActive(0); }} onKeyDown={onKeyDown}
|
||||
placeholder={role === "admin" ? "Go to a page, or find an event, person, payment (R450)…" : role === "supervisor" ? "Go to a page, or find an event or payment (R450)…" : "Go to a page or find an event…"}
|
||||
className="flex-1 py-3 text-sm outline-none bg-transparent" aria-label="Search" role="combobox" aria-expanded="true" aria-controls="palette-results"
|
||||
aria-activedescendant={items[active] ? `palette-item-${active}` : undefined}
|
||||
/>
|
||||
<kbd className="text-[10px] text-gray-400 border rounded px-1.5 py-0.5">Esc</kbd>
|
||||
</div>
|
||||
<ul id="palette-results" ref={listRef} role="listbox" className="max-h-[55vh] overflow-y-auto py-1" data-testid="palette-results">
|
||||
{items.length === 0 && <li className="px-4 py-6 text-sm text-gray-500 text-center">{query.trim().length < 2 ? "Type to search." : "Nothing found."}</li>}
|
||||
{items.map((item, i) => {
|
||||
const header = item.group !== lastGroup ? item.group : null;
|
||||
lastGroup = item.group;
|
||||
const Icon = item.icon;
|
||||
return (
|
||||
<React.Fragment key={item.key}>
|
||||
{header && <li className="px-4 pt-2 pb-1 text-[11px] font-semibold uppercase tracking-wide text-gray-400" role="presentation">{header}</li>}
|
||||
<li
|
||||
id={`palette-item-${i}`} data-index={i} role="option" aria-selected={i === active}
|
||||
className={`mx-1 flex items-center gap-3 rounded-lg px-3 py-2 cursor-pointer ${i === active ? "bg-brand-50" : "hover:bg-gray-50"}`}
|
||||
onMouseMove={() => setActive(i)} onClick={() => go(item)}
|
||||
>
|
||||
<Icon className="w-4 h-4 text-gray-500 shrink-0" />
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block text-sm text-gray-900 truncate">{item.title}</span>
|
||||
{item.subtitle && <span className="block text-xs text-gray-500 truncate">{item.subtitle}</span>}
|
||||
</span>
|
||||
{i === active && <CornerDownLeft className="w-3.5 h-3.5 text-gray-400" />}
|
||||
</li>
|
||||
</React.Fragment>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<div className="border-t px-4 py-2 text-[11px] text-gray-400 flex gap-4">
|
||||
<span>↑↓ to move</span><span>Enter to open</span><span>Ctrl K to open or close</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** A button that opens the palette (for people who don't use the keyboard shortcut). */
|
||||
export function CommandPaletteButton({ className = "" }: { className?: string }) {
|
||||
const { user } = useAuth();
|
||||
if (!user || !["staff", "supervisor", "admin"].includes(user.role || "")) return null;
|
||||
return (
|
||||
<button type="button" onClick={() => window.dispatchEvent(new Event(OPEN_PALETTE_EVENT))}
|
||||
className={`inline-flex items-center gap-2 rounded-lg border bg-white px-3 py-1.5 text-sm text-gray-500 hover:bg-gray-50 ${className}`}>
|
||||
<Search className="w-4 h-4" /> Search or jump to… <kbd className="ml-2 text-[10px] border rounded px-1.5 py-0.5">Ctrl K</kbd>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user