Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | import { useState, useEffect, useMemo } from 'react'
import { useNavigate } from 'react-router-dom'
import { Search, Sparkles, RefreshCw } from 'lucide-react'
import { toast } from 'sonner'
import {
api,
type Recipe,
type Favorite,
type HistoryItem,
type DiscoverSuggestion,
} from '../api/client'
import { useProfile } from '../contexts/ProfileContext'
import { useAIStatus } from '../contexts/AIStatusContext'
import NavHeader from '../components/NavHeader'
import RecipeCard from '../components/RecipeCard'
import { RecipeGridSkeleton } from '../components/Skeletons'
import { cn } from '../lib/utils'
type Tab = 'favorites' | 'discover'
export default function Home() {
const navigate = useNavigate()
const { profile } = useProfile()
const aiStatus = useAIStatus()
const [searchQuery, setSearchQuery] = useState('')
const [activeTab, setActiveTab] = useState<Tab>('favorites')
const [favorites, setFavorites] = useState<Favorite[]>([])
const [history, setHistory] = useState<HistoryItem[]>([])
const [recipesCount, setRecipesCount] = useState(0)
const [loading, setLoading] = useState(true)
const [discoverSuggestions, setDiscoverSuggestions] = useState<DiscoverSuggestion[]>([])
const [discoverLoading, setDiscoverLoading] = useState(false)
const [discoverError, setDiscoverError] = useState(false)
const [, setDiscoverRefreshedAt] = useState<string | null>(null)
useEffect(() => {
loadData()
}, [])
const loadData = async () => {
try {
const [favoritesData, historyData, recipesData] = await Promise.all([
api.favorites.list(),
api.history.list(6),
api.recipes.list(1000),
])
setFavorites(favoritesData)
setHistory(historyData)
setRecipesCount(recipesData.length)
} catch (error) {
console.error('Failed to load data:', error)
toast.error('Failed to load recipes')
} finally {
setLoading(false)
}
}
const loadDiscoverSuggestions = async () => {
if (!profile) return
setDiscoverLoading(true)
setDiscoverError(false)
try {
const result = await api.ai.discover(profile.id)
setDiscoverSuggestions(result.suggestions)
setDiscoverRefreshedAt(result.refreshed_at)
} catch (error) {
console.error('Failed to load discover suggestions:', error)
setDiscoverError(true)
} finally {
setDiscoverLoading(false)
}
}
// Load discover suggestions when tab is clicked (Task 4.3 - move to onClick)
const handleDiscoverTabClick = () => {
setActiveTab('discover')
if (discoverSuggestions.length === 0 && !discoverLoading && !discoverError && aiStatus.available) {
loadDiscoverSuggestions()
}
}
const handleSuggestionClick = (suggestion: DiscoverSuggestion) => {
navigate(`/search?q=${encodeURIComponent(suggestion.search_query)}`)
}
const handleSearchSubmit = (e: React.FormEvent) => {
e.preventDefault()
if (searchQuery.trim()) {
navigate(`/search?q=${encodeURIComponent(searchQuery.trim())}`)
}
}
const handleRecipeClick = async (recipeId: number) => {
try {
await api.history.record(recipeId)
} catch (error) {
console.error('Failed to record history:', error)
}
navigate(`/recipe/${recipeId}`)
}
const handleRemoveFavorite = async (recipe: Recipe) => {
try {
await api.favorites.remove(recipe.id)
setFavorites(favorites.filter((f) => f.recipe.id !== recipe.id))
toast.success('Removed from favorites')
} catch (error) {
console.error('Failed to remove favorite:', error)
toast.error('Failed to remove from favorites')
}
}
// Memoize favorite IDs set (Task 5.2)
const localFavoriteIds = useMemo(
() => new Set(favorites.map((f) => f.recipe.id)),
[favorites]
)
if (!profile) return null
return (
<div className="flex min-h-screen flex-col bg-background">
<NavHeader />
{/* Main content */}
<main className="flex-1 px-4 py-6">
<div className="mx-auto max-w-4xl">
{/* Search bar */}
<form onSubmit={handleSearchSubmit} className="mb-6">
<div className="relative">
<Search className="absolute left-4 top-1/2 h-5 w-5 -translate-y-1/2 text-muted-foreground" />
<input
type="text"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Search recipes or paste a URL..."
className="w-full rounded-xl border border-border bg-input-background py-3 pl-12 pr-4 text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring"
/>
</div>
</form>
{/* Tab toggle - only show if AI is available */}
{aiStatus.available && (
<div className="mb-6 flex justify-center">
<div className="inline-flex rounded-lg bg-muted p-1">
<button
onClick={() => setActiveTab('favorites')}
className={cn(
'rounded-md px-4 py-2 text-sm font-medium transition-colors',
activeTab === 'favorites'
? 'bg-background text-foreground shadow-sm'
: 'text-muted-foreground hover:text-foreground'
)}
>
My Favorites
</button>
<button
onClick={handleDiscoverTabClick}
className={cn(
'rounded-md px-4 py-2 text-sm font-medium transition-colors',
activeTab === 'discover'
? 'bg-background text-foreground shadow-sm'
: 'text-muted-foreground hover:text-foreground'
)}
>
Discover
</button>
</div>
</div>
)}
{loading ? (
<RecipeGridSkeleton count={6} />
) : activeTab === 'favorites' || !aiStatus.available ? (
<>
{/* Recently Viewed */}
{history.length > 0 && (
<section className="mb-8">
<div className="mb-4 flex items-center justify-between">
<h2 className="text-lg font-medium text-foreground">
Recently Viewed
</h2>
<button
onClick={() => navigate('/all-recipes')}
className="text-sm font-medium text-primary hover:underline"
>
My Recipes ({recipesCount})
</button>
</div>
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6">
{history.map((item) => (
<RecipeCard
key={item.recipe.id}
recipe={item.recipe}
isFavorite={localFavoriteIds.has(item.recipe.id)}
onClick={() => handleRecipeClick(item.recipe.id)}
/>
))}
</div>
</section>
)}
{/* Favorites */}
<section>
<div className="mb-4 flex items-center justify-between">
<h2 className="text-lg font-medium text-foreground">
My Favorite Recipes
</h2>
{favorites.length > 0 && (
<button
onClick={() => navigate('/favorites')}
className="text-sm font-medium text-primary hover:underline"
>
View All ({favorites.length})
</button>
)}
</div>
{favorites.length > 0 ? (
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4">
{favorites.map((favorite) => (
<RecipeCard
key={favorite.recipe.id}
recipe={favorite.recipe}
isFavorite
onFavoriteToggle={handleRemoveFavorite}
onClick={() => handleRecipeClick(favorite.recipe.id)}
/>
))}
</div>
) : (
/* Empty state */
<div className="flex flex-col items-center justify-center rounded-xl border-2 border-dashed border-border py-12">
<div className="mb-4 rounded-full bg-muted p-4">
<Search className="h-8 w-8 text-muted-foreground" />
</div>
<h3 className="mb-2 text-lg font-medium text-foreground">
No favorites yet
</h3>
<p className="mb-4 text-center text-muted-foreground">
Search for recipes and add them to your favorites
</p>
<button
onClick={() => document.querySelector('input')?.focus()}
className="rounded-lg bg-primary px-4 py-2 text-primary-foreground transition-colors hover:bg-primary/90"
>
Discover Recipes
</button>
</div>
)}
</section>
</>
) : (
/* Discover tab - AI recommendations */
<div>
{discoverLoading ? (
<div className="flex flex-col items-center justify-center py-12">
<div className="mb-4 rounded-full bg-muted p-4">
<Sparkles className="h-8 w-8 animate-pulse text-primary" />
</div>
<p className="text-muted-foreground">
Generating personalized suggestions...
</p>
</div>
) : discoverSuggestions.length > 0 ? (
<div className="space-y-6">
{/* Header with refresh time */}
<div className="flex items-center justify-between">
<h2 className="text-lg font-medium text-foreground">
Personalized For You
</h2>
<button
onClick={loadDiscoverSuggestions}
className="flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground"
disabled={discoverLoading}
>
<RefreshCw className={cn('h-4 w-4', discoverLoading && 'animate-spin')} />
Refresh
</button>
</div>
{/* Suggestions grid */}
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{discoverSuggestions.map((suggestion, index) => (
<button
key={`${suggestion.type}-${index}`}
onClick={() => handleSuggestionClick(suggestion)}
className="group flex flex-col rounded-xl border border-border bg-card p-4 text-left transition-all hover:border-primary hover:shadow-md"
>
<div className="mb-2 flex items-center gap-2">
<Sparkles className="h-4 w-4 text-primary" />
<span className="text-xs uppercase tracking-wide text-muted-foreground">
{suggestion.type === 'favorites' && 'Based on Favorites'}
{suggestion.type === 'seasonal' && 'Seasonal'}
{suggestion.type === 'new' && 'Try Something New'}
</span>
</div>
<h3 className="mb-1 font-medium text-foreground group-hover:text-primary">
{suggestion.title}
</h3>
<p className="text-sm text-muted-foreground">
{suggestion.description}
</p>
<div className="mt-3 flex items-center gap-1 text-xs text-primary">
<Search className="h-3 w-3" />
<span>Search: {suggestion.search_query}</span>
</div>
</button>
))}
</div>
</div>
) : !aiStatus.available ? (
/* Empty state - AI unavailable (no API key) */
<div className="flex flex-col items-center justify-center py-12">
<div className="mb-4 rounded-full bg-muted p-4">
<Sparkles className="h-8 w-8 text-muted-foreground" />
</div>
<h3 className="mb-2 text-lg font-medium text-foreground">
AI Recommendations
</h3>
<p className="mb-4 text-center text-muted-foreground">
Configure an API key in settings to enable personalized recipe suggestions
</p>
<button
onClick={() => navigate('/settings')}
className="rounded-lg bg-primary px-4 py-2 text-primary-foreground transition-colors hover:bg-primary/90"
>
Go to Settings
</button>
</div>
) : discoverError ? (
/* Empty state - API error */
<div className="flex flex-col items-center justify-center py-12">
<div className="mb-4 rounded-full bg-muted p-4">
<Sparkles className="h-8 w-8 text-muted-foreground" />
</div>
<h3 className="mb-2 text-lg font-medium text-foreground">
Unable to Load Suggestions
</h3>
<p className="mb-4 text-center text-muted-foreground">
Something went wrong. Please try again.
</p>
<button
onClick={loadDiscoverSuggestions}
className="flex items-center gap-2 rounded-lg bg-primary px-4 py-2 text-primary-foreground transition-colors hover:bg-primary/90"
>
<RefreshCw className="h-4 w-4" />
Try Again
</button>
</div>
) : (
/* Empty state - No suggestions available */
<div className="flex flex-col items-center justify-center py-12">
<div className="mb-4 rounded-full bg-muted p-4">
<Sparkles className="h-8 w-8 text-muted-foreground" />
</div>
<h3 className="mb-2 text-lg font-medium text-foreground">
No Suggestions Yet
</h3>
<p className="mb-4 text-center text-muted-foreground">
Add some favorites to get personalized recommendations
</p>
<button
onClick={() => setActiveTab('favorites')}
className="rounded-lg bg-primary px-4 py-2 text-primary-foreground transition-colors hover:bg-primary/90"
>
View Favorites
</button>
</div>
)}
</div>
)}
</div>
</main>
</div>
)
}
|