🛠️ Refactor favorites store, improve add/remove functions

This commit is contained in:
pheralb
2025-09-24 14:09:03 +01:00
parent 60314a9648
commit 45de1a631b
2 changed files with 35 additions and 29 deletions
+10 -8
View File
@@ -94,14 +94,16 @@
<FolderHeart size={18} strokeWidth={1.5} /> <FolderHeart size={18} strokeWidth={1.5} />
{/if} {/if}
<p>Favorites</p> <p>Favorites</p>
<span>-</span> {#if favoritesCount > 0}
{#if !searchTerm} <span>-</span>
<span>{favoritesCount} SVGs</span> {#if !searchTerm}
{:else} <span>{favoritesCount} SVGs</span>
<p> {:else}
<span class="font-mono">{filteredFavorites.length}</span> <p>
<span>search results</span> <span class="font-mono">{filteredFavorites.length}</span>
</p> <span>search results</span>
</p>
{/if}
{/if} {/if}
</div> </div>
{#if favoritesCount > 0} {#if favoritesCount > 0}
+25 -21
View File
@@ -7,7 +7,6 @@ import { browser } from "$app/environment";
const localStorageKey = "svgl_favorites"; const localStorageKey = "svgl_favorites";
function createFavoritesStore() { function createFavoritesStore() {
// Check if the favorites exist in the SVGs array:
const validateFavorites = (favorites: iSVG[]): iSVG[] => { const validateFavorites = (favorites: iSVG[]): iSVG[] => {
return favorites.filter((favorite) => { return favorites.filter((favorite) => {
const existsInSvgs = svgs.some((svg) => { const existsInSvgs = svgs.some((svg) => {
@@ -62,10 +61,10 @@ function createFavoritesStore() {
// Add SVG to favorites: // Add SVG to favorites:
addToFavorites: (item: iSVG) => addToFavorites: (item: iSVG) =>
update((favorites) => { update((favorites) => {
const exists = favorites.some((fav) => const exists = favorites.some(
typeof item === "object" && item.id (fav) =>
? fav.id === item.id fav.title === item.title &&
: fav === item, JSON.stringify(fav.route) === JSON.stringify(item.route),
); );
if (!exists) { if (!exists) {
const newFavorites = [...favorites, item]; const newFavorites = [...favorites, item];
@@ -78,10 +77,12 @@ function createFavoritesStore() {
// Delete SVG from favorites: // Delete SVG from favorites:
removeFromFavorites: (item: iSVG) => removeFromFavorites: (item: iSVG) =>
update((favorites) => { update((favorites) => {
const newFavorites = favorites.filter((fav) => const newFavorites = favorites.filter(
typeof item === "object" && item.id (fav) =>
? fav.id !== item.id !(
: fav !== item, fav.title === item.title &&
JSON.stringify(fav.route) === JSON.stringify(item.route)
),
); );
saveFavorites(newFavorites); saveFavorites(newFavorites);
return newFavorites; return newFavorites;
@@ -90,18 +91,20 @@ function createFavoritesStore() {
// Toggle (add/remove) SVG from favorites: // Toggle (add/remove) SVG from favorites:
toggleFavorite: (item: iSVG) => toggleFavorite: (item: iSVG) =>
update((favorites) => { update((favorites) => {
const exists = favorites.some((fav) => const exists = favorites.some(
typeof item === "object" && item.id (fav) =>
? fav.id === item.id fav.title === item.title &&
: fav === item, JSON.stringify(fav.route) === JSON.stringify(item.route),
); );
let newFavorites; let newFavorites;
if (exists) { if (exists) {
newFavorites = favorites.filter((fav) => newFavorites = favorites.filter(
typeof item === "object" && item.id (fav) =>
? fav.id !== item.id !(
: fav !== item, fav.title === item.title &&
JSON.stringify(fav.route) === JSON.stringify(item.route)
),
); );
} else { } else {
newFavorites = [...favorites, item]; newFavorites = [...favorites, item];
@@ -113,18 +116,19 @@ function createFavoritesStore() {
// Check if SVG is in favorites: // Check if SVG is in favorites:
isFavorite: (item: iSVG, currentFavorites: iSVG[]) => { isFavorite: (item: iSVG, currentFavorites: iSVG[]) => {
return currentFavorites.some((fav) => return currentFavorites.some(
typeof item === "object" && item.id ? fav.id === item.id : fav === item, (fav) =>
fav.title === item.title &&
JSON.stringify(fav.route) === JSON.stringify(item.route),
); );
}, },
// Delete all favorites: // Clear favorites:
clearFavorites: () => { clearFavorites: () => {
set([]); set([]);
saveFavorites([]); saveFavorites([]);
}, },
// Get count of favorites:
getCount: (currentFavorites: iSVG[]) => currentFavorites.length, getCount: (currentFavorites: iSVG[]) => currentFavorites.length,
validateAndCleanup: () => { validateAndCleanup: () => {