All files / src/hooks useHomeData.ts

53.33% Statements 24/45
20% Branches 2/10
54.54% Functions 6/11
53.84% Lines 21/39

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            12x 12x 12x 12x 12x   12x 6x 6x 6x 6x         6x 6x 6x 6x               6x     6x     12x 12x       12x                 12x                                   12x                    
import { useState, useEffect, useMemo, useCallback } from 'react'
import { useNavigate } from 'react-router-dom'
import { toast } from 'sonner'
import { api, type Recipe, type Favorite, type HistoryItem } from '../api/client'
 
export function useHomeData() {
  const navigate = useNavigate()
  const [favorites, setFavorites] = useState<Favorite[]>([])
  const [history, setHistory] = useState<HistoryItem[]>([])
  const [recipesCount, setRecipesCount] = useState(0)
  const [loading, setLoading] = useState(true)
 
  useEffect(() => {
    let cancelled = false
    ;(async () => {
      try {
        const [favoritesData, historyData, recipesData] = await Promise.all([
          api.favorites.list(),
          api.history.list(6),
          api.recipes.list(1000),
        ])
        Eif (!cancelled) {
          setFavorites(favoritesData)
          setHistory(historyData)
          setRecipesCount(recipesData.length)
        }
      } catch (error) {
        if (!cancelled) {
          console.error('Failed to load data:', error)
          toast.error('Failed to load recipes')
        }
      } finally {
        Eif (!cancelled) setLoading(false)
      }
    })()
    return () => { cancelled = true }
  }, [])
 
  const favoriteIds = useMemo(
    () => new Set(favorites.map((f) => f.recipe.id)),
    [favorites]
  )
 
  const handleRecipeClick = useCallback(async (recipeId: number) => {
    try {
      await api.history.record(recipeId)
    } catch (error) {
      console.error('Failed to record history:', error)
    }
    navigate(`/recipe/${recipeId}`)
  }, [navigate])
 
  const handleToggleFavorite = useCallback(async (recipe: Recipe) => {
    const isFav = favoriteIds.has(recipe.id)
    try {
      if (isFav) {
        await api.favorites.remove(recipe.id)
        setFavorites((prev) => prev.filter((f) => f.recipe.id !== recipe.id))
        toast.success('Removed from favorites')
      } else {
        const newFav = await api.favorites.add(recipe.id)
        setFavorites((prev) => [...prev, newFav])
        toast.success('Added to favorites')
      }
    } catch (error) {
      console.error('Failed to toggle favorite:', error)
      toast.error(isFav ? 'Failed to remove from favorites' : 'Failed to add to favorites')
    }
  }, [favoriteIds])
 
  return {
    favorites,
    history,
    recipesCount,
    loading,
    favoriteIds,
    handleRecipeClick,
    handleToggleFavorite,
  }
}
 
← Back to Dashboard