feat: implement Favorites feature with persistent storage and sidebar integration and update lake data.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { createContext, useContext, useState, useCallback, type ReactNode } from 'react';
|
||||
|
||||
const STORAGE_KEY = 'hladinator_favorites';
|
||||
|
||||
const loadFavorites = (): string[] => {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
return raw ? JSON.parse(raw) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
interface FavoritesContextType {
|
||||
favorites: string[];
|
||||
toggleFavorite: (id: string) => void;
|
||||
isFavorite: (id: string) => boolean;
|
||||
}
|
||||
|
||||
const FavoritesContext = createContext<FavoritesContextType>({
|
||||
favorites: [],
|
||||
toggleFavorite: () => {},
|
||||
isFavorite: () => false,
|
||||
});
|
||||
|
||||
export const FavoritesProvider = ({ children }: { children: ReactNode }) => {
|
||||
const [favorites, setFavorites] = useState<string[]>(loadFavorites);
|
||||
|
||||
const toggleFavorite = useCallback((id: string) => {
|
||||
setFavorites(prev => {
|
||||
const next = prev.includes(id) ? prev.filter(f => f !== id) : [...prev, id];
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const isFavorite = useCallback((id: string) => favorites.includes(id), [favorites]);
|
||||
|
||||
return (
|
||||
<FavoritesContext.Provider value={{ favorites, toggleFavorite, isFavorite }}>
|
||||
{children}
|
||||
</FavoritesContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useFavorites = () => useContext(FavoritesContext);
|
||||
Reference in New Issue
Block a user