brackt/app/components/sport-season/UpcomingCalendarPanel.tsx
chrisp 08ae2bb88a
All checks were successful
🚀 Deploy / ʦ🔍 Typecheck & Lint (push) Successful in 1m38s
🚀 Deploy / 🧪 Test (push) Successful in 3m30s
🚀 Deploy / 🐳 Build (push) Successful in 1m9s
🚀 Deploy / 🚀 Deploy (push) Successful in 11s
Fix SSR/hydration timezone flip in event date and time display (#95)
## Summary

- Adds `useHasMounted` hook (using `useSyncExternalStore` — no extra render cycle) to gate UTC timestamp formatting to client-only
- Fixes the visible time-flip in `EventSchedule` and `UpcomingCalendarPanel` where SSR rendered UTC time (e.g. "9:00 PM") and the client re-rendered with local time (e.g. "2:00 PM PDT") after hydration, with `suppressHydrationWarning` silently hiding the mismatch
- Fixes `formatEventDate` in `date-utils.ts` (and `EventSchedule`'s private copy) to parse `YYYY-MM-DD` strings as local midnight instead of UTC midnight, correcting an off-by-one day bug for UTC-negative users across all callers

## Test plan

- [ ] Navigate to a sports season page — confirm event times show in local timezone with no flash/flip on load
- [ ] Hard-reload the page — confirm no brief UTC time visible before settling on local time
- [ ] Check the home page `UpcomingCalendarPanel` — confirm same behavior
- [ ] Verify dates display correctly (e.g. no "Jun 17" showing for a "Jun 18" event near midnight UTC)

Co-authored-by: Chris Parsons <chrisparsons1127@gmail.com>
Reviewed-on: #95
2026-06-17 06:09:29 +00:00

179 lines
6.4 KiB
TypeScript

import { format } from "date-fns";
import { Calendar, ChevronRight, Users } from "lucide-react";
import { useState, useEffect } from "react";
import { useHasMounted } from "~/hooks/useHasMounted";
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";
import { isBracketMajor } from "~/lib/event-utils";
export interface CalendarPanelEvent extends UpcomingParticipantEvent {
sportName: string;
sportSeasonName: string;
/** Present when showing events across multiple leagues (home page) */
leagueName?: string;
leagueId?: string;
sportsSeasonPageUrl?: string;
/** Deep link to the event detail page. Preferred over sportsSeasonPageUrl when set. */
eventDetailUrl?: 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 }) {
const isMounted = useHasMounted();
// Prefer game-level scheduledAt over event-level eventDate for display
const gameDate = event.earliestGameTime ? new Date(event.earliestGameTime) : null;
const dateStr = isMounted && gameDate
? format(gameDate, "MMM d")
: formatEventDate(event.eventDate);
const participantCount = event.relevantParticipants.length;
const isAllCompete = event.eventType !== "playoff_game" && event.eventType !== "group_stage_match" && !isBracketMajor(event.simulatorType);
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">
{dateStr ?? "TBD"}
</span>
{isMounted && gameDate && (
<p className="text-xs text-muted-foreground leading-tight">
{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>
);
const linkUrl = event.eventDetailUrl ?? event.sportsSeasonPageUrl;
if (linkUrl) {
return (
<Link to={linkUrl} 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>
);
}