aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/hooks/useFavorites.ts
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2026-03-09 00:00:39 +0100
committerAriel Costas Guerrero <ariel@costas.dev>2026-03-09 00:00:50 +0100
commitd71f0ed16d175285f2e8cbde6091994c2aa1d962 (patch)
treee8b0bcc3f432fa9d5243dd4595af256511643151 /src/frontend/app/hooks/useFavorites.ts
parent5288cfbed34f94c4321b8d9dc497cfd0da3ffd26 (diff)
Enhance route details handling and add favorites functionality; improve error logging and response structure
Diffstat (limited to 'src/frontend/app/hooks/useFavorites.ts')
-rw-r--r--src/frontend/app/hooks/useFavorites.ts28
1 files changed, 28 insertions, 0 deletions
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<string[]>(() => {
+ 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 };
+}