brackt/app/components/sport-season/UpcomingCalendarPanel.tsx
chrisp a9d4954242
All checks were successful
🚀 Deploy / 🧪 Test (push) Successful in 1m28s
🚀 Deploy / ʦ TypeScript (push) Successful in 1m17s
🚀 Deploy / 🔍 Lint (push) Successful in 49s
🚀 Deploy / 🐳 Build (push) Successful in 14m50s
🚀 Deploy / 🚀 Deploy (push) Successful in 10s
Fix World Cup group stage display and upcoming events (#55)
## Summary

- **Group stage match list**: Removed MD 1/2/3 headings; matches now listed chronologically under date separators (e.g. "Jun 14"). Kick-off time appears below the team names instead of between them.
- **Sort order**: \`findMatchesByGroupIds\` / \`findMatchesByEventId\` now order by \`scheduledAt ASC NULLS LAST\` so unscheduled matches always trail scheduled ones.
- **Groups/Bracket toggle**: When both group stage and knockout bracket exist, a toggle appears (mirroring the NBA/AFL standings toggle). Sports with both regular-season standings and a group stage get all views available.
- **Upcoming events**: \`getUpcomingEventsForDraftedParticipants\` now also queries \`groupStageMatches\`, so World Cup group fixtures appear on the home page calendar sorted by kick-off time with a label like "Group A — France vs Germany".
- **\`isAllCompete\` fix**: \`UpcomingEventsCard\` and \`UpcomingCalendarPanel\` now treat \`group_stage_match\` as a bracket-style event, showing team badges instead of "N of your picks".

Fixes #53

## Test plan

- [ ] Navigate to a World Cup sports season page — groups should be listed chronologically under date headers with no MD labels; kick-off time should appear below each match row
- [ ] Once the knockout bracket has matches, confirm the Groups/Bracket toggle appears and both views render correctly
- [ ] On the home page, a user with drafted World Cup participants should see upcoming group fixtures in the calendar panel with individual team badges (not a participant count)
- [ ] NBA/AFL/NHL pages unaffected — their standings/playoffs/finished toggle still works normally
- [ ] \`npm run typecheck\` and \`npm run test:run\` pass

Co-authored-by: Chris Parsons <chrisparsons1127@gmail.com>
Reviewed-on: #55
2026-05-27 05:43:08 +00:00

176 lines
6.2 KiB
TypeScript

import { format } from "date-fns";
import { Calendar, ChevronRight, Users } from "lucide-react";
import { useState, useEffect } from "react";
import { Link } from "react-router";
import { Badge } from "~/components/ui/badge";
import {
Card,
CardContent,
CardHeader,
CardTitle,
} from "~/components/ui/card";
import { formatEventDate } from "~/lib/date-utils";
import type { UpcomingParticipantEvent } from "~/models/scoring-event";
export interface CalendarPanelEvent extends UpcomingParticipantEvent {
sportName: string;
sportSeasonName: string;
/** Present when showing events across multiple leagues (home page) */
leagueName?: string;
leagueId?: string;
sportsSeasonPageUrl?: string;
}
interface Props {
events: CalendarPanelEvent[];
/** When true, renders a league name badge on each row */
showLeague?: boolean;
/** If provided, only the first N events are rendered */
limit?: number;
/** If provided and events.length > limit, renders a "View all" footer link */
viewAllUrl?: string;
/** Override the default empty state message */
emptyMessage?: string;
}
function ParticipantList({ participants }: { participants: Array<{ id: string; name: string }> }) {
const MAX_SHOWN = 3;
const shown = participants.slice(0, MAX_SHOWN);
const overflow = participants.length - MAX_SHOWN;
return (
<span className="flex flex-wrap items-center gap-1">
{shown.map((p) => (
<Badge key={p.id} variant="secondary" className="text-xs font-normal">
{p.name}
</Badge>
))}
{overflow > 0 && (
<Badge variant="outline" className="text-xs text-muted-foreground">
+{overflow} more
</Badge>
)}
</span>
);
}
function EventRow({ event, showLeague }: { event: CalendarPanelEvent; showLeague: boolean }) {
// Prefer game-level scheduledAt over event-level eventDate for display
const gameDate = event.earliestGameTime ? new Date(event.earliestGameTime) : null;
const dateStr = gameDate
? format(gameDate, "MMM d")
: formatEventDate(event.eventDate);
const participantCount = event.relevantParticipants.length;
const isAllCompete = event.eventType !== "playoff_game" && event.eventType !== "group_stage_match";
const displayName = event.matchLabel
? `${event.name}${event.matchLabel}`
: event.name;
const content = (
<div className="flex items-start gap-3 py-2.5 border-b last:border-0 group">
{/* Date pill */}
<div className="w-14 shrink-0 text-center">
<span className="text-xs font-semibold text-electric" suppressHydrationWarning>
{dateStr ?? "TBD"}
</span>
{gameDate && (
<p
className="text-xs text-muted-foreground leading-tight"
suppressHydrationWarning
>
{gameDate.toLocaleTimeString(undefined, {
hour: "numeric",
minute: "2-digit",
})}
</p>
)}
</div>
{/* Event details */}
<div className="flex-1 min-w-0 space-y-1">
<div className="flex items-center flex-wrap gap-2">
<span className="text-sm font-medium truncate">{displayName}</span>
{showLeague && event.leagueName && (
<Badge variant="outline" className="text-xs shrink-0">
{event.leagueName}
</Badge>
)}
</div>
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
<span className="truncate">{event.sportName} · {event.sportSeasonName}</span>
</div>
<div className="flex items-center gap-1.5">
<Users className="h-3 w-3 text-muted-foreground shrink-0" />
{isAllCompete && participantCount > 1 ? (
<span className="text-xs text-muted-foreground">
{participantCount} of your picks
</span>
) : (
<ParticipantList participants={event.relevantParticipants} />
)}
</div>
</div>
</div>
);
if (event.sportsSeasonPageUrl) {
return (
<Link to={event.sportsSeasonPageUrl} className="block hover:bg-muted/40 -mx-2 px-2 rounded transition-colors">
{content}
</Link>
);
}
return content;
}
export function UpcomingCalendarPanel({ events, showLeague = false, limit, viewAllUrl, emptyMessage }: Props) {
// Filter out past events client-side using local timezone. Server sends events
// starting from yesterday UTC as a buffer; useEffect trims to today-and-forward
// after hydration so the correct local date is used.
const [localFilteredEvents, setLocalFilteredEvents] = useState(events);
useEffect(() => {
const todayStr = new Intl.DateTimeFormat("en-CA").format(new Date());
setLocalFilteredEvents(events.filter((e) => (e.earliestGameTime ?? e.eventDate ?? "9999-12-31") >= todayStr));
}, [events]);
const displayedEvents = limit !== undefined ? localFilteredEvents.slice(0, limit) : localFilteredEvents;
const hiddenCount = limit !== undefined ? Math.max(0, localFilteredEvents.length - limit) : 0;
return (
<Card>
<CardHeader className="pb-3">
<CardTitle className="flex items-center gap-2 text-base">
<Calendar className="h-4 w-4 text-electric" />
Upcoming Events
</CardTitle>
</CardHeader>
<CardContent>
{localFilteredEvents.length === 0 ? (
<p className="text-sm text-muted-foreground py-2">
{emptyMessage ?? "No upcoming events in the next 30 days."}
</p>
) : (
<div>
{displayedEvents.map((event) => (
<EventRow key={event.id} event={event} showLeague={showLeague} />
))}
{viewAllUrl && (
<div className="mt-3 pt-2 border-t">
<Link
to={viewAllUrl}
className="flex items-center gap-1 text-sm text-electric hover:underline"
>
{hiddenCount > 0
? `View all ${localFilteredEvents.length} upcoming events`
: "View all upcoming events"}
<ChevronRight className="h-3.5 w-3.5" />
</Link>
</div>
)}
</div>
)}
</CardContent>
</Card>
);
}