From d71f0ed16d175285f2e8cbde6091994c2aa1d962 Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Mon, 9 Mar 2026 00:00:39 +0100 Subject: Enhance route details handling and add favorites functionality; improve error logging and response structure --- src/frontend/app/hooks/useFavorites.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/frontend/app/hooks/useFavorites.ts (limited to 'src/frontend/app/hooks') diff --git a/src/frontend/app/hooks/useFavorites.ts b/src/frontend/app/hooks/useFavorites.ts new file mode 100644 index 0000000..962ac2d --- /dev/null +++ b/src/frontend/app/hooks/useFavorites.ts @@ -0,0 +1,28 @@ +import { useState } from "react"; + +/** + * A simple hook for managing favorite items in localStorage. + * @param key LocalStorage key to use + * @returns [favorites, toggleFavorite, isFavorite] + */ +export function useFavorites(key: string) { + const [favorites, setFavorites] = useState(() => { + if (typeof window === "undefined") return []; + const stored = localStorage.getItem(key); + return stored ? JSON.parse(stored) : []; + }); + + const toggleFavorite = (id: string) => { + setFavorites((prev) => { + const next = prev.includes(id) + ? prev.filter((item) => item !== id) + : [...prev, id]; + localStorage.setItem(key, JSON.stringify(next)); + return next; + }); + }; + + const isFavorite = (id: string) => favorites.includes(id); + + return { favorites, toggleFavorite, isFavorite }; +} -- cgit v1.3