Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 84 additions & 22 deletions apps/next/src/components/ui/restaurant/header/desktop-tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import type { Station } from "@api/index";
import { GridView, Menu as MenuIcon } from "@mui/icons-material";
import { Button, Tab, Tabs } from "@mui/material";
import {
ChevronLeft,
ChevronRight,
GridView,
Menu as MenuIcon,
} from "@mui/icons-material";
import { Button, IconButton, Tab, Tabs } from "@mui/material";
import { useEffect, useState } from "react";
import { useRestaurantUIStore } from "@/context/useRestaurantUIStore";
import { scrollToStation, toTitleCase } from "@/utils/funcs";

/** Max stations shown on the bar at once; arrows page through the rest. */
const VISIBLE_STATIONS = 5;

interface DesktopTabsProps {
isDesktop: boolean;
isLoading: boolean;
Expand All @@ -22,31 +31,84 @@ export function DesktopTabs({
const isCompactView = useRestaurantUIStore((s) => s.isCompactView);
const setIsCompactView = useRestaurantUIStore((s) => s.setIsCompactView);

const [windowStart, setWindowStart] = useState(0);
const maxStart = Math.max(0, stations.length - VISIBLE_STATIONS);

// Keep the window in range and make sure the selected station is visible.
useEffect(() => {
const idx = stations.findIndex(
(s) => s.name.toLowerCase() === selectedStation,
);
setWindowStart((start) => {
let next = Math.min(start, maxStart);
if (idx !== -1) {
if (idx < next) next = idx;
else if (idx >= next + VISIBLE_STATIONS)
next = idx - VISIBLE_STATIONS + 1;
}
return Math.max(0, Math.min(next, maxStart));
});
}, [selectedStation, stations, maxStart]);

if (!isDesktop) return null;

const visibleStations = stations.slice(
windowStart,
windowStart + VISIBLE_STATIONS,
);
const canGoBack = windowStart > 0;
const canGoForward = windowStart < maxStart;
const showArrows = stations.length > VISIBLE_STATIONS;

return (
<div className="mt-3">
{!isLoading && !isError && stations.length > 0 && (
<Tabs
value={selectedStation || false}
onChange={(_event, value: string) => {
const val = value || "";
scrollToStation(val);
setSelectedStation(val);
}}
className="flex w-full overflow-x-auto no-scrollbar !bg-primary-accent/20 dark:!bg-[var(--desktop-tabs-bg)] !rounded-lg !p-2 [&_.MuiTabs-flexContainer]:justify-between [&_.MuiTabs-flexContainer]:gap-2 [&_.MuiTabs-indicator]:hidden"
variant="scrollable"
scrollButtons={false}
>
{stations.map((station) => (
<Tab
key={station.name}
value={station.name.toLowerCase()}
label={toTitleCase(station.name)}
className="!rounded !border !border-transparent !bg-transparent !px-4 !py-1.5 !text-sm !font-medium !text-black dark:!text-white !normal-case !min-h-0 aria-selected:!bg-white dark:aria-selected:!bg-white aria-selected:!text-slate-900 dark:aria-selected:!text-gray-900 aria-selected:!border-slate-200 dark:aria-selected:!border-slate-200 aria-selected:!shadow-sm"
/>
))}
</Tabs>
<div className="flex h-[52px] w-full flex-row items-stretch rounded-lg bg-primary-accent/20 px-2 dark:bg-[var(--desktop-tabs-bg)]">
{showArrows && (
<IconButton
aria-label="Previous stations"
disabled={!canGoBack}
onClick={() => setWindowStart((s) => Math.max(0, s - 1))}
className="!self-center !h-9 !w-9 !text-slate-500 disabled:!opacity-30"
>
<ChevronLeft />
</IconButton>
)}
<Tabs
value={
visibleStations.some(
(s) => s.name.toLowerCase() === selectedStation,
)
? selectedStation
: false
}
onChange={(_event, value: string) => {
const val = value || "";
scrollToStation(val);
setSelectedStation(val);
}}
className="flex h-full min-h-0 min-w-0 flex-1 [&_.MuiTabs-flexContainer]:h-full [&_.MuiTabs-flexContainer]:justify-between [&_.MuiTabs-indicator]:!h-0.5 [&_.MuiTabs-indicator]:!bg-sky-700 dark:[&_.MuiTabs-indicator]:!bg-blue-300"
>
{visibleStations.map((station) => (
<Tab
key={station.name}
value={station.name.toLowerCase()}
label={toTitleCase(station.name)}
className="!flex-auto !whitespace-nowrap !max-w-none !px-3 !text-base !font-medium !text-black dark:!text-white !normal-case !min-h-0 aria-selected:!text-sky-700 dark:aria-selected:!text-blue-300"
/>
))}
</Tabs>
{showArrows && (
<IconButton
aria-label="Next stations"
disabled={!canGoForward}
onClick={() => setWindowStart((s) => Math.min(maxStart, s + 1))}
className="!self-center !h-9 !w-9 !text-slate-500 disabled:!opacity-30"
>
<ChevronRight />
</IconButton>
)}
</div>
)}
{/* Card / compact view toggles */}
<div className="flex justify-end mt-2">
Expand Down
24 changes: 23 additions & 1 deletion apps/next/src/hooks/useRestaurantPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ import { trpc } from "@/utils/trpc";
import type { HallStatusEnum } from "@/utils/types";
import { HallEnum } from "@/utils/types";

const normalizeStationName = (name: string) =>
name.toLowerCase().replace(/[^a-z0-9]/g, "");

/** Stations that don't rotate; shown after the rotating ones. */
const NON_ROTATING_STATIONS: Record<"anteatery" | "brandywine", Set<string>> = {
// Names as returned by CampusDish, normalized.
brandywine: new Set(
["Hearth/Pizza", "Ember/Grill", "Honeycakes/Bakery"].map(
normalizeStationName,
),
),
anteatery: new Set(
["Sizzle Grill", "Bakery", "Oven"].map(normalizeStationName),
),
};

/**
* Returns the current meal period key for a given date by checking which period
* window the date falls within. Falls back to the first period if none match.
Expand Down Expand Up @@ -182,7 +198,13 @@ export function useRestaurantPage(hall: HallEnum): UseRestaurantPageResult {
[hallData, selectedPeriod],
);

const stations: Station[] = currentMenu?.stations ?? [];
// Rotating stations come first; the fixed (non-rotating) ones go last.
const stations: Station[] = useMemo(() => {
const all = currentMenu?.stations ?? [];
const fixed = NON_ROTATING_STATIONS[restaurantId];
const isFixed = (s: Station) => fixed.has(normalizeStationName(s.name));
return [...all.filter((s) => !isFixed(s)), ...all.filter(isFixed)];
}, [currentMenu, restaurantId]);

// --- Auto-select station ---
useEffect(() => {
Expand Down
Loading