feat: implement Favorites feature with persistent storage and sidebar integration and update lake data.

This commit is contained in:
David Fencl
2026-06-05 23:57:17 +02:00
parent b660f0f6c3
commit 27551f9183
16 changed files with 569 additions and 74 deletions
+46
View File
@@ -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);