recipe_app/frontend/src/components/RecipeBookTabs.tsx

92 lines
3.3 KiB
TypeScript
Raw Normal View History

import { useState, useEffect } from "react";
2025-07-11 17:06:41 -07:00
import { Link, useLocation } from 'react-router-dom';
import { getRecipes } from "../services/frontendApi.js";
2025-07-11 17:06:41 -07:00
const RecipeBookTabs = () => {
const location = useLocation();
const [lastViewedRecipeId, setLastViewedRecipeId] = useState<number | null>(null);
const loadRandomRecipeId = async () => {
try {
const recipes = await getRecipes();
if (recipes.length > 0) {
const randomIndex = Math.floor(Math.random() * recipes.length);
setLastViewedRecipeId(recipes[randomIndex].id);
}
} catch (error) {
console.error('Error loading recipes:', error);
}
};
// update lastViewedRecipe id if we navigate to /recipe/${id}
useEffect(() => {
const match = /^\/recipe\/(\d+)$/.exec(location.pathname);
if (match) {
setLastViewedRecipeId(parseInt(match[1]));
}
}, [location.pathname, lastViewedRecipeId]);
// choose random recipe on first load
useEffect(() => {
if (!lastViewedRecipeId) {
loadRandomRecipeId();
}
}, []);
2025-07-11 17:06:41 -07:00
const tabs = [
{ id: '/', label: 'All Recipes', icon: '📚' },
{ id: `/recipe/${lastViewedRecipeId}`, label: 'Recipe', icon: '🥗' },
2025-07-11 17:06:41 -07:00
{ id: '/add-recipe', label: 'Add Recipe', icon: '' },
{ id: '/about', label: 'About', icon: '🍽️' },
];
return (
<div className="bg-amber-50 h-16 flex-shrink-0 rounded-tl-lg rounded-tr-lg">
2025-07-11 17:06:41 -07:00
<div className="relative h-full">
<div className="absolute bottom-0 left-0 right-0 h-0.5 bg-amber-200"></div>
<div className="flex space-x-1 px-6 pt-4 h-full items-end justify-center">
{tabs.map((tab) => {
const isActive = location.pathname === tab.id || (location.pathname.startsWith(tab.id) && tab.id === "/recipe/");
return (
<Link
key={tab.id}
to={tab.id}
className={`
2025-08-05 15:09:52 -07:00
relative px-6 py-3 rounded-t-lg font-bold text-sm transform text-amber-800
2025-07-11 17:06:41 -07:00
${isActive
2025-08-05 15:09:52 -07:00
? 'bg-amber-100 scale-105 z-10 border-t-2 border-amber-200'
: 'bg-amber-200 hover:bg-amber-100 hover:text-amber-700 hover:scale-102 shadow-sm'
2025-07-11 17:06:41 -07:00
}
before:absolute before:bottom-0 before:left-0 before:right-0 before:h-0.5
${isActive ? '' : ''}
`}
>
<div className="flex items-center space-x-2">
<span className="text-lg">{tab.icon}</span>
<span>{tab.label}</span>
</div>
{isActive && (
<>
<div className={`absolute -left-2 bottom-0 w-2 h-2 bg-white ${location.pathname === '/' ? 'hidden' : ''}`}>
2025-07-11 17:06:41 -07:00
<div className="absolute top-0 left-0 w-2 h-2 bg-amber-200 rounded-br-lg"></div>
</div>
<div className={`absolute -right-2 bottom-0 w-2 h-2 bg-white ${location.pathname === '/about' ? 'hidden' : ''}`}>
2025-07-11 17:06:41 -07:00
<div className="absolute top-0 right-0 w-2 h-2 bg-amber-200 rounded-bl-lg"></div>
</div>
</>
)}
</Link>
);
})}
</div>
</div>
</div>
);
};
export default RecipeBookTabs;