| 1 |
import { create } from 'zustand'; |
| 2 |
import { createJSONStorage, devtools, persist } from 'zustand/middleware'; |
| 3 |
import { safeLocalStorage } from './safe-local-storage'; |
| 4 |
|
| 5 |
const ONE_WEEK_IN_MILLISECONDS = 1000 * 60 * 60 * 24 * 7; |
| 6 |
|
| 7 |
const state = (set, get) => ({ |
| 8 |
images: [], |
| 9 |
expiration: 0, |
| 10 |
isEmpty: () => get().images.length === 0, |
| 11 |
hasExpired: () => Date.now() > get().expiration, |
| 12 |
updateCache: (images) => |
| 13 |
set({ images, expiration: Date.now() + ONE_WEEK_IN_MILLISECONDS }), |
| 14 |
}); |
| 15 |
|
| 16 |
export const useUnsplashCacheStore = create( |
| 17 |
persist(devtools(state, { name: 'Extendify Unsplash Images' }), { |
| 18 |
name: `extendify-unsplash-images-${window.extSharedData.siteId}`, |
| 19 |
storage: createJSONStorage(() => safeLocalStorage), |
| 20 |
}), |
| 21 |
); |
| 22 |
|