import { create } from 'zustand'; import { persist } from 'zustand/middleware'; import { Product } from '@/content/products'; interface RecentlyViewedState { items: Product[]; addItem: (item: Product) => void; clear: () => void; } export const useRecentlyViewedStore = create()( persist( (set) => ({ items: [], addItem: (newItem) => { set((state) => { // Usuń, jeśli już jest na liście (aby przenieść na początek) const filtered = state.items.filter((i) => i.slug !== newItem.slug); // Dodaj na początek, zachowaj tylko ostatnie 4 return { items: [newItem, ...filtered].slice(0, 4) }; }); }, clear: () => set({ items: [] }), }), { name: 'thermcool-recently-viewed' } ) );