All files / src/screens Home.tsx

50% Statements 12/24
37.5% Branches 6/16
11.11% Functions 1/9
52.17% Lines 12/23

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                                                                                                      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 { 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'
 
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 [activeTab, setActiveTab] = useState<Tab>('favorites')
 
  const { favorites, history, recipesCount, loading, favoriteIds, handleRecipeClick, handleToggleFavorite } = useHomeData()
 
  const discover = useDiscoverTab({
    profileId: profile?.id,
    aiAvailable: aiStatus.available,
  })
 
  const handleSearchSubmit = (e: React.FormEvent) => {
    e.preventDefault()
    if (searchQuery.trim()) {
      navigate(`/search?q=${encodeURIComponent(searchQuery.trim())}`)
    }
  }
 
  const handleDiscoverTabClick = () => {
    setActiveTab('discover')
    discover.loadIfEmpty()
  }
 
  Iif (!profile) return null
 
  const showFavorites = activeTab === 'favorites' || !aiStatus.available
 
  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..."
                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>
 
          {aiStatus.available && (
            <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={aiStatus.available}
              onRefresh={() => discover.load(true)}
              onRetry={() => discover.load()}
              onSwitchToFavorites={() => setActiveTab('favorites')}
            />
          )}
        </div>
      </main>
    </div>
  )
}
 
← Back to Dashboard