Skip to content
11 changes: 9 additions & 2 deletions apps/next/src/app/events/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
EVENT_CATEGORIES,
type EventCategory,
} from "@/utils/classifyEvent";
import { isUpcomingEvent } from "@/utils/funcs";
import { isUpcomingEvent, isValidDate } from "@/utils/funcs";
import { trpc } from "@/utils/trpc";
import "react-big-calendar/lib/css/react-big-calendar.css";

Expand All @@ -45,7 +45,11 @@ const Events = () => {
() =>
(events ?? [])
.filter(isUpcomingEvent)
.sort((a, b) => a.start.getTime() - b.start.getTime())
.sort((a, b) => {
const aTime = a.start?.getTime() || Infinity;
const bTime = b.start?.getTime() || Infinity;
return aTime === bTime ? 0 : aTime - bTime;
})
.map((event) => ({
...event,
eventType: classifyEvent(event.title, event.description),
Expand Down Expand Up @@ -76,6 +80,9 @@ const Events = () => {
const seen = new Set<string>();
const months: { year: number; monthIndex: number }[] = [];
for (const event of filteredEvents) {
if (!isValidDate(event.start) || !isValidDate(event.end)) {
continue;
}
const year = event.start.getFullYear();
const monthIndex = event.start.getMonth();
const key = `${year}-${monthIndex}`;
Expand Down
19 changes: 16 additions & 3 deletions apps/next/src/components/ui/card/event-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import EventImage from "@/components/ui/event-image";
import EventTypeBadge from "@/components/ui/event-type-badge";
import { useMediaQuery } from "@/hooks/useMediaQuery";
import type { EventCategory } from "@/utils/classifyEvent";
import { dateRangeToString, timeToString, toTitleCase } from "@/utils/funcs";
import {
dateRangeToString,
isValidDate,
timeToString,
toTitleCase,
} from "@/utils/funcs";
import EventDialogContent from "../event-dialog-content";
import EventDrawerContent from "../event-drawer-content";
import OngoingBadge from "../ongoing-badge";
Expand Down Expand Up @@ -41,6 +46,8 @@ const EventCardContent = React.forwardRef<
HTMLDivElement,
EventCardContentProps
>(({ props, ...divProps }, ref) => {
const hasValidDate = isValidDate(props.start) && isValidDate(props.end);

return (
<div ref={ref} {...divProps}>
<Card
Expand Down Expand Up @@ -72,12 +79,18 @@ const EventCardContent = React.forwardRef<
<div className="flex flex-col gap-2 text-sm text-zinc-500 dark:text-zinc-400 mt-1">
<div className="flex gap-3 items-center">
<CalendarTodayOutlinedIcon sx={{ fontSize: 16 }} />
<span>{dateRangeToString(props.start, props.end)}</span>
<span>
{hasValidDate
? dateRangeToString(props.start, props.end)
: "TBD"}
</span>
</div>
<div className="flex gap-3 items-center">
<AccessTime sx={{ fontSize: 16 }} />
<span>
{timeToString(props.start)} – {timeToString(props.end)}
{hasValidDate
? `${timeToString(props.start)} - ${timeToString(props.end)}`
: "TBD"}
</span>
</div>
<div className="flex gap-3 items-center">
Expand Down
56 changes: 33 additions & 23 deletions apps/next/src/components/ui/event-dialog-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { classifyEvent } from "@/utils/classifyEvent";
import {
dateRangeToString,
generateGCalLink,
isValidDate,
timeToString,
toTitleCase,
} from "@/utils/funcs";
Expand All @@ -30,6 +31,8 @@ export default function EventDialogContent({
onClose,
...props
}: Event & { onClose: () => void }): React.JSX.Element {
const hasValidDate = isValidDate(props.start) && isValidDate(props.end);

return (
<Box className="dark:bg-[var(--surface-modal)]">
<div className="relative">
Expand Down Expand Up @@ -68,12 +71,17 @@ export default function EventDialogContent({
>
<div className="flex gap-2 items-center">
<CalendarTodayOutlinedIcon sx={{ fontSize: 16 }} />
<span>{dateRangeToString(props.start, props.end)}</span>
<span>
{hasValidDate ? dateRangeToString(props.start, props.end) : "TBD"}
</span>
</div>

<div className="flex gap-2 items-center">
<AccessTimeOutlinedIcon sx={{ fontSize: 16 }} />
<span>
{timeToString(props.start)} – {timeToString(props.end)}
{hasValidDate
? `${timeToString(props.start)} – ${timeToString(props.end)}`
: "TBD"}
</span>
</div>
<div className="flex gap-2 items-center">
Expand All @@ -88,28 +96,30 @@ export default function EventDialogContent({
>
{props.description}
</Typography>
<div className="w-full flex items-center justify-center pt-4">
<a
href={generateGCalLink(
props.title,
props.description,
props.restaurantId === "brandywine"
? HallEnum.BRANDYWINE
: HallEnum.ANTEATERY,
props.start,
)}
rel="noreferrer"
target="_blank"
>
<Button
variant="contained"
className="[&_svg]:size-5 whitespace-nowrap"
startIcon={<CalendarTodayOutlinedIcon />}
{hasValidDate && (
<div className="w-full flex items-center justify-center pt-4">
<a
href={generateGCalLink(
props.title,
props.description,
props.restaurantId === "brandywine"
? HallEnum.BRANDYWINE
: HallEnum.ANTEATERY,
props.start,
)}
rel="noreferrer"
target="_blank"
>
Add to Google Calendar
</Button>
</a>
</div>
<Button
variant="contained"
className="[&_svg]:size-5 whitespace-nowrap"
startIcon={<CalendarTodayOutlinedIcon />}
>
Add to Google Calendar
</Button>
</a>
</div>
)}
</DialogContent>
</Box>
);
Expand Down
17 changes: 14 additions & 3 deletions apps/next/src/components/ui/event-drawer-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import type { Event } from "@peterplate/validators";
import EventImage from "@/components/ui/event-image";
import EventTypeBadge from "@/components/ui/event-type-badge";
import { classifyEvent } from "@/utils/classifyEvent";
import { dateRangeToString, timeToString, toTitleCase } from "@/utils/funcs";
import {
dateRangeToString,
isValidDate,
timeToString,
toTitleCase,
} from "@/utils/funcs";

/**
* `EventDrawerContent` renders the detailed view of an event within a drawer (mobile view).
Expand All @@ -18,6 +23,8 @@ import { dateRangeToString, timeToString, toTitleCase } from "@/utils/funcs";
* @returns {JSX.Element} The rendered content for the event drawer.
*/
export default function EventDrawerContent(props: Event): React.JSX.Element {
const hasValidDate = isValidDate(props.start) && isValidDate(props.end);

return (
<Box className="dark:bg-[var(--surface-modal)]">
<div className="relative">
Expand All @@ -42,12 +49,16 @@ export default function EventDrawerContent(props: Event): React.JSX.Element {
<div className="flex flex-col gap-2 text-sm text-zinc-500 dark:text-zinc-400 mt-1">
<div className="flex gap-2 items-center">
<CalendarTodayOutlinedIcon sx={{ fontSize: 16 }} />
<span>{dateRangeToString(props.start, props.end)}</span>
<span>
{hasValidDate ? dateRangeToString(props.start, props.end) : "TBD"}
</span>
</div>
<div className="flex gap-2 items-center">
<AccessTimeOutlinedIcon sx={{ fontSize: 16 }} />
<span>
{timeToString(props.start)} – {timeToString(props.end)}
{hasValidDate
? `${timeToString(props.start)} - ${timeToString(props.end)}`
: "TBD"}
</span>
</div>
<div className="flex gap-2 items-center">
Expand Down
8 changes: 6 additions & 2 deletions apps/next/src/components/ui/mobile-list-event-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import LocationOnIcon from "@mui/icons-material/LocationOn";
import { format } from "date-fns";
import type { EventWithType } from "@/app/events/page";
import { isValidDate } from "@/utils/funcs";

interface MobileListEventRowProps {
event: EventWithType;
Expand All @@ -17,8 +18,11 @@ const ROW_CLASSES = [
].join(" ");

const MobileListEventRow = ({ event, onClick }: MobileListEventRowProps) => {
const startStr = format(new Date(event.start), "h:mm a");
const endStr = format(new Date(event.end), "h:mm a");
const hasValidDate = isValidDate(event.start) && isValidDate(event.end);
const startStr = hasValidDate
? format(new Date(event.start), "h:mm a")
: "TBD";
const endStr = hasValidDate ? format(new Date(event.end), "h:mm a") : "TBD";
const locationLabel =
event.restaurantId === "anteatery" ? "Anteatery" : "Brandywine";

Expand Down
44 changes: 28 additions & 16 deletions apps/next/src/components/ui/mobile-list-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ArrowForwardIosIcon from "@mui/icons-material/ArrowForwardIos";
import { format, isSameDay, startOfDay } from "date-fns";
import { useEffect, useMemo, useRef } from "react";
import type { EventWithType } from "@/app/events/page";
import { isValidDate } from "@/utils/funcs";
import MobileListEventRow from "./mobile-list-event-row";

interface MobileListViewProps {
Expand All @@ -16,6 +17,7 @@ interface MobileListViewProps {
}

interface DayGroup {
key: string;
date: Date;
events: EventWithType[];
}
Expand Down Expand Up @@ -50,24 +52,32 @@ const MobileListView = ({
const months: Record<string, MonthGroup> = {};
sorted.forEach((event) => {
const start = new Date(event.start);
const monthKey = format(start, "yyyy-MM");
const hasValidDate = isValidDate(event.start) && isValidDate(event.end);
const monthKey = hasValidDate ? format(start, "yyyy-MM") : "TBD";
if (!months[monthKey]) {
months[monthKey] = { key: monthKey, date: start, days: [] };
}
const dayKey = format(startOfDay(start), "yyyy-MM-dd");
const existingDay = months[monthKey].days.find(
(d) => format(d.date, "yyyy-MM-dd") === dayKey,
);
const dayKey = hasValidDate
? format(startOfDay(start), "yyyy-MM-dd")
: "TBD";
const existingDay = months[monthKey].days.find((d) => d.key === dayKey);
if (existingDay) {
existingDay.events.push(event);
} else {
months[monthKey].days.push({ date: start, events: [event] });
months[monthKey].days.push({
key: dayKey,
date: hasValidDate ? start : new Date(0),
events: [event],
});
}
});

return Object.values(months).sort(
(a, b) => a.date.getTime() - b.date.getTime(),
);
return Object.values(months).sort((a, b) => {
if (a.key === "TBD") return 1;
if (b.key === "TBD") return -1;

return a.date.getTime() - b.date.getTime();
});
}, [filteredEvents]);

const scrollToMonth = (offset: number) => (currentKey: string) => {
Expand Down Expand Up @@ -105,7 +115,9 @@ const MobileListView = ({
className="text-xl sm:text-2xl font-bold text-sky-700 dark:text-blue-300 focus:outline-none flex items-center gap-2"
onClick={onOpenMonthPicker}
>
{format(month.date, "MMMM yyyy")}{" "}
{month.key === "TBD"
? "TBD"
: format(month.date, "MMMM yyyy")}{" "}
<span className="text-xs sm:text-sm">▼</span>
</button>
<div className="flex items-center gap-5">
Expand All @@ -126,15 +138,15 @@ const MobileListView = ({

<div className="flex flex-col gap-6 mt-2 mb-4">
{month.days.map((day) => {
const isToday = isSameDay(day.date, new Date());
const isToday =
day.key !== "TBD" && isSameDay(day.date, new Date());
return (
<div
key={day.date.toISOString()}
className="flex flex-col gap-1"
>
<div key={day.key} className="flex flex-col gap-1">
<div className="flex items-center justify-between pb-2 border-b border-slate-200 dark:border-zinc-600">
<h3 className="font-semibold text-black dark:text-zinc-100">
{format(day.date, "EEEE - MMMM d, yyyy")}
{day.key === "TBD"
? "Date TBD"
: format(day.date, "EEEE - MMMM d, yyyy")}
</h3>
{isToday && (
<span className="bg-sky-700 text-white dark:bg-blue-300 dark:text-zinc-900 text-sm px-2 py-0.5 rounded-md">
Expand Down
33 changes: 32 additions & 1 deletion apps/next/src/utils/funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,40 @@ function getPopularDishes(

/** Whether an event hasn't ended yet. */
export function isUpcomingEvent(event: Event) {
return isFuture(event.end) || isFuture(event.start);
// Keep cards missing dates
const start = event.start ? new Date(event.start).getTime() : NaN;
const end = event.end ? new Date(event.end).getTime() : NaN;
const bothDatesMissing =
(!Number.isFinite(start) || start === 0) &&
(!Number.isFinite(end) || end === 0);

if (bothDatesMissing) {
return true;
}

return (
(Number.isFinite(start) && start > Date.now()) ||
(Number.isFinite(end) && end > Date.now())
);
}

/** Whether an event's date is valid or not. **/
export const isValidDate = (value: unknown): boolean => {
if (
value === null ||
value === undefined ||
value === "" ||
value === 0 ||
value === "0"
) {
return false;
}

const date = new Date(value as string | number);

return !Number.isNaN(date.getTime()) && date.getTime() !== 0;
};

export function sortedEvents(events: Event[], numCards: number) {
return (events ?? [])
.filter(isUpcomingEvent)
Expand Down
Loading