All files / hooks useFavorites.ts

97.14% Statements 34/35
100% Branches 4/4
100% Functions 12/12
96.87% Lines 31/32

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                        1x         1x 20x     20x 9x 9x 9x 6x 6x     1x 1x         20x 5x 5x 5x             20x 2x       2x       20x 3x       20x 6x       20x 2x 1x   1x         20x 6x       20x 1x     20x                    
import { useState, useEffect, useCallback } from 'react';
 
export type FavoriteType = 'procedure' | 'drug' | 'tool' | 'manual';
 
export interface Favorite {
  id: string;
  type: FavoriteType;
  title: string;
  path: string;
  addedAt: number;
}
 
const STORAGE_KEY = 'emerges-tes-favorites';
 
/**
 * Hook para gestionar favoritos persistentes
 */
export const useFavorites = () => {
  const [favorites, setFavorites] = useState<Favorite[]>([]);
 
  // Cargar favoritos al montar
  useEffect(() => {
    try {
      const stored = localStorage.getItem(STORAGE_KEY);
      if (stored) {
        const parsed = JSON.parse(stored) as Favorite[];
        setFavorites(parsed);
      }
    } catch (error) {
      console.error('Error loading favorites:', error);
      setFavorites([]);
    }
  }, []);
 
  // Guardar favoritos en localStorage
  const saveFavorites = useCallback((newFavorites: Favorite[]) => {
    try {
      localStorage.setItem(STORAGE_KEY, JSON.stringify(newFavorites));
      setFavorites(newFavorites);
    } catch (error) {
      console.error('Error saving favorites:', error);
    }
  }, []);
 
  // AƱadir a favoritos
  const addFavorite = useCallback((favorite: Omit<Favorite, 'addedAt'>) => {
    const newFavorite: Favorite = {
      ...favorite,
      addedAt: Date.now(),
    };
    saveFavorites([...favorites, newFavorite]);
  }, [favorites, saveFavorites]);
 
  // Eliminar de favoritos
  const removeFavorite = useCallback((id: string) => {
    saveFavorites(favorites.filter((f) => f.id !== id));
  }, [favorites, saveFavorites]);
 
  // Verificar si es favorito
  const isFavorite = useCallback((id: string) => {
    return favorites.some((f) => f.id === id);
  }, [favorites]);
 
  // Toggle favorito
  const toggleFavorite = useCallback((favorite: Omit<Favorite, 'addedAt'>) => {
    if (isFavorite(favorite.id)) {
      removeFavorite(favorite.id);
    } else {
      addFavorite(favorite);
    }
  }, [isFavorite, addFavorite, removeFavorite]);
 
  // Obtener favoritos por tipo
  const getFavoritesByType = useCallback((type: FavoriteType) => {
    return favorites.filter((f) => f.type === type);
  }, [favorites]);
 
  // Limpiar todos los favoritos
  const clearFavorites = useCallback(() => {
    saveFavorites([]);
  }, [saveFavorites]);
 
  return {
    favorites,
    addFavorite,
    removeFavorite,
    toggleFavorite,
    isFavorite,
    getFavoritesByType,
    clearFavorites,
  };
};