This repository has been archived on 2026-08-11. You can view files and clone it, but cannot push or open issues or pull requests.
Gishen-Mob/lib/stores/favoritesStore.ts
kirukib 7310a349c5 Add Gishen mobile app with complete demo shopping and care flows.
Ship Expo Router screens, seeded stores, and polished UX so auth, shop, checkout, prescriptions, loyalty, family, and account processes work end-to-end offline.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 01:21:32 +03:00

35 lines
971 B
TypeScript

import AsyncStorage from "@react-native-async-storage/async-storage";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { seedFavoriteIds } from "@/lib/data/seed";
type FavState = {
ids: string[];
toggle: (id: string) => void;
has: (id: string) => boolean;
};
export const useFavoritesStore = create<FavState>()(
persist(
(set, get) => ({
ids: seedFavoriteIds,
toggle: (id) => {
const ids = get().ids;
set({
ids: ids.includes(id) ? ids.filter((x) => x !== id) : [...ids, id],
});
},
has: (id) => get().ids.includes(id),
}),
{
name: "gishen-favorites",
storage: createJSONStorage(() => AsyncStorage),
version: 2,
migrate: (persisted: unknown) => {
const state = (persisted ?? {}) as Partial<FavState>;
return !state.ids?.length ? { ...state, ids: seedFavoriteIds } : state;
},
}
)
);