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 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x | import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { Search } from 'lucide-react'
import { toast } from 'sonner'
import { useProfile } from '../contexts/ProfileContext'
import { useAIStatus } from '../contexts/AIStatusContext'
import { useHomeData } from '../hooks/useHomeData'
import { useDiscoverTab } from '../hooks/useDiscoverTab'
import NavHeader from '../components/NavHeader'
import { RecipeGridSkeleton } from '../components/Skeletons'
import { cn } from '../lib/utils'
import FavoritesTab from './FavoritesTab'
import DiscoverTab from './DiscoverTab'
import { api } from '../api/client'
type Tab = 'favorites' | 'discover'
function TabToggle({ activeTab, onFavoritesClick, onDiscoverClick }: {
activeTab: Tab
onFavoritesClick: () => void
onDiscoverClick: () => void
}) {
return (
<div className="mb-6 flex justify-center">
<div className="inline-flex rounded-lg bg-muted p-1">
<button
onClick={onFavoritesClick}
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={onDiscoverClick}
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>
)
}
export default function Home() {
const navigate = useNavigate()
const { profile } = useProfile()
const aiStatus = useAIStatus()
const [searchQuery, setSearchQuery] = useState('')
const [importing, setImporting] = useState(false)
const [activeTab, setActiveTab] = useState<Tab>('favorites')
const { favorites, history, recipesCount, loading, favoriteIds, handleRecipeClick, handleToggleFavorite } = useHomeData()
const discoverAvailable = aiStatus.isFeatureAvailable('discover')
const discover = useDiscoverTab({
profileId: profile?.id,
aiAvailable: discoverAvailable,
})
const handleSearchSubmit = async (e: React.FormEvent) => {
e.preventDefault()
const trimmed = searchQuery.trim()
if (!trimmed) return
if (trimmed.startsWith('http://') || trimmed.startsWith('https://')) {
setImporting(true)
try {
const recipe = await api.recipes.scrape(trimmed)
await api.history.record(recipe.id)
toast.success(`Imported: ${recipe.title}`)
navigate(`/recipe/${recipe.id}`)
} catch (err) {
const msg = err instanceof Error ? err.message : null
toast.error(msg || 'Could not import recipe from that URL. Try a different link.')
} finally {
setImporting(false)
}
return
}
navigate(`/search?q=${encodeURIComponent(trimmed)}`)
}
const handleDiscoverTabClick = () => {
setActiveTab('discover')
discover.loadIfEmpty()
}
Iif (!profile) return null
const showFavorites = activeTab === 'favorites' || !discoverAvailable
return (
<div className="flex min-h-screen flex-col bg-background">
<NavHeader />
<main className="flex-1 px-4 py-6">
<div className="mx-auto max-w-4xl">
<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..."
disabled={importing}
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 disabled:opacity-60"
/>
</div>
</form>
{discoverAvailable && (
<TabToggle
activeTab={activeTab}
onFavoritesClick={() => setActiveTab('favorites')}
onDiscoverClick={handleDiscoverTabClick}
/>
)}
{loading ? (
<RecipeGridSkeleton count={6} />
) : showFavorites ? (
<FavoritesTab
history={history}
favorites={favorites}
recipesCount={recipesCount}
favoriteIds={favoriteIds}
onRecipeClick={handleRecipeClick}
onFavoriteToggle={handleToggleFavorite}
/>
) : (
<DiscoverTab
suggestions={discover.suggestions}
loading={discover.loading}
error={discover.error}
aiAvailable={discoverAvailable}
onRefresh={() => discover.load(true)}
onRetry={() => discover.load()}
onSwitchToFavorites={() => setActiveTab('favorites')}
/>
)}
</div>
</main>
</div>
)
}
|