Skip to content
4 changes: 2 additions & 2 deletions apps/next/src/components/ui/card/event-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import EventTypeBadge from "@/components/ui/event-type-badge";
import { useMediaQuery } from "@/hooks/useMediaQuery";
import type { EventCategory } from "@/utils/classifyEvent";
import {
dateRangeToString,
eventDateRangeToString,
eventTimeRangeToString,
isHappeningToday,
toTitleCase,
Expand Down Expand Up @@ -86,7 +86,7 @@ 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>{eventDateRangeToString(props)}</span>
</div>
<div className="flex gap-3 items-center">
<AccessTime sx={{ fontSize: 16 }} />
Expand Down
11 changes: 7 additions & 4 deletions apps/next/src/components/ui/card/upcoming-event-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Box, Card, Dialog, Typography } from "@mui/material";
import type { Event } from "@peterplate/validators";
import { useState } from "react";
import { classifyEvent } from "@/utils/classifyEvent";
import { dateRangeToString, timeToString, toTitleCase } from "@/utils/funcs";
import {
eventDateRangeToString,
eventTimeRangeToString,
toTitleCase,
} from "@/utils/funcs";
import EventDialogContent from "../event-dialog-content";
import EventTypeBadge from "../event-type-badge";

Expand Down Expand Up @@ -48,14 +52,13 @@ export default function UpcomingEventCard({
<Box className="flex items-center gap-1.5">
<CalendarMonth style={{ fontSize: iconSize }} />
<Typography variant="caption" color="text.secondary">
{dateRangeToString(event.start, event.end)}
{eventDateRangeToString(event)}
</Typography>
</Box>
<Box className="flex items-center gap-1.5">
<AccessTime style={{ fontSize: iconSize }} />
<Typography variant="caption" color="text.secondary">
{timeToString(event.start)}
{event.end ? ` - ${timeToString(event.end)}` : ""}
{eventTimeRangeToString(event)}
</Typography>
</Box>
</>
Expand Down
7 changes: 4 additions & 3 deletions apps/next/src/components/ui/event-dialog-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import EventImage from "@/components/ui/event-image";
import EventTypeBadge from "@/components/ui/event-type-badge";
import { classifyEvent } from "@/utils/classifyEvent";
import {
dateRangeToString,
eventDateRangeToString,
eventTimeRangeToString,
generateGCalLink,
isUpcomingEvent,
isValidDate,
toTitleCase,
} from "@/utils/funcs";
import { HallEnum } from "@/utils/types";
Expand Down Expand Up @@ -69,7 +70,7 @@ export default function EventDialogContent({
>
<div className="flex gap-2 items-center">
<CalendarTodayOutlinedIcon sx={{ fontSize: 16 }} />
<span>{dateRangeToString(props.start, props.end)}</span>
<span>{eventDateRangeToString(props)}</span>
</div>
<div className="flex gap-2 items-center">
<AccessTimeOutlinedIcon sx={{ fontSize: 16 }} />
Expand All @@ -87,7 +88,7 @@ export default function EventDialogContent({
>
{props.description}
</Typography>
{isUpcomingEvent(props) && (
{isValidDate(props.start) && isUpcomingEvent(props) && (
<div className="w-full flex items-center justify-center pt-4">
<a
href={generateGCalLink(
Expand Down
7 changes: 4 additions & 3 deletions apps/next/src/components/ui/event-drawer-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import EventImage from "@/components/ui/event-image";
import EventTypeBadge from "@/components/ui/event-type-badge";
import { classifyEvent } from "@/utils/classifyEvent";
import {
dateRangeToString,
eventDateRangeToString,
eventTimeRangeToString,
generateGCalLink,
isUpcomingEvent,
isValidDate,
toTitleCase,
} from "@/utils/funcs";
import { HallEnum } from "@/utils/types";
Expand Down Expand Up @@ -49,7 +50,7 @@ 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>{eventDateRangeToString(props)}</span>
</div>
<div className="flex gap-2 items-center">
<AccessTimeOutlinedIcon sx={{ fontSize: 16 }} />
Expand All @@ -67,7 +68,7 @@ export default function EventDrawerContent(props: Event): React.JSX.Element {
>
{props.description?.replace(/\u00A0+/g, " ")}
</Typography>
{isUpcomingEvent(props) && (
{isValidDate(props.start) && isUpcomingEvent(props) && (
<div className="w-full flex items-center justify-center pt-4">
<a
href={generateGCalLink(
Expand Down
11 changes: 8 additions & 3 deletions apps/next/src/components/ui/restaurant/header/mobile-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from "@mui/material";
import type { DishWithRating, Event } from "@peterplate/validators";
import { useRestaurantUIStore } from "@/context/useRestaurantUIStore";
import { scrollToStation, toTitleCase } from "@/utils/funcs";
import { isValidDate, scrollToStation, toTitleCase } from "@/utils/funcs";
import { cn } from "@/utils/tw";

interface MobileActionsProps {
Expand Down Expand Up @@ -163,8 +163,13 @@ export function MobileActions({
<div className="p-4 overflow-y-auto">
{hallEvents.length > 0 ? (
hallEvents.map((event) => {
const start = event.start ? new Date(event.start) : null;
const end = event.end ? new Date(event.end) : null;
// Use whichever date is known when the other is missing
const start = new Date(
isValidDate(event.start) ? event.start : event.end,
);
const end = new Date(
isValidDate(event.end) ? event.end : event.start,
);
const now = new Date();
const isActive = start && end && now >= start && now <= end;
const dateRange =
Expand Down
11 changes: 8 additions & 3 deletions apps/next/src/components/ui/restaurant/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Typography,
} from "@mui/material";
import type React from "react";
import { formatOpenCloseTime, toTitleCase } from "@/utils/funcs";
import { formatOpenCloseTime, isValidDate, toTitleCase } from "@/utils/funcs";
import {
ANTEATERY_MAP_EMBED_URL,
ANTEATERY_MAP_LINK_URL,
Expand Down Expand Up @@ -157,8 +157,13 @@ export function Sidebar({
{hallEvents.length > 0 ? (
<div className="space-y-2">
{hallEvents.map((event) => {
const start = event.start ? new Date(event.start) : null;
const end = event.end ? new Date(event.end) : null;
// Use whichever date is known when the other is missing
const start = new Date(
isValidDate(event.start) ? event.start : event.end,
);
const end = new Date(
isValidDate(event.end) ? event.end : event.start,
);
const now = new Date();
const isActive = start && end && now >= start && now <= end;
const dateRange =
Expand Down
30 changes: 29 additions & 1 deletion apps/next/src/utils/funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,23 @@ export function isUpcomingEvent(event: Event) {
return isFuture(event.end) || isFuture(event.start);
}

/** 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;
};

/**
* Whether the API gave an event an end time. A missing end time comes through
* as the epoch, because the schema coerces null to a Date.
Expand All @@ -466,8 +483,19 @@ export function isHappeningToday(event: Pick<Event, "start" | "end">) {
);
}

/** "11:00am – 4:00pm", or only the start time when there's no end time. */
/** "October 2 – 30, 2026", only the day that's known, or "TBD" with neither. */
export function eventDateRangeToString(event: Pick<Event, "start" | "end">) {
const firstDay = isValidDate(event.start) ? event.start : event.end;
return isValidDate(firstDay) ? dateRangeToString(firstDay, event.end) : "TBD";
}

/** "11:00am – 4:00pm", only the time that's known, or "TBD" with neither. */
export function eventTimeRangeToString(event: Pick<Event, "start" | "end">) {
if (!isValidDate(event.start)) {
return isValidDate(event.end)
? `Ends at ${timeToString(event.end)}`
: "TBD";
}
return hasEndTime(event)
? `${timeToString(event.start)} – ${timeToString(event.end)}`
: timeToString(event.start);
Expand Down
Loading