UNPKG

@coinmeca/ui

Version:

This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

91 lines 2.94 kB
"use client"; import { create } from 'zustand'; import { produce } from 'immer'; const useNotificationStore = create((set, get) => ({ notis: [], toasts: [], nonce: 0, read: false, count: 0, setNotiList: (notis) => set((state) => ({ notis })), setToasts: (toasts) => set((state) => ({ toasts })), setNonce: (nonce) => set((state) => ({ nonce })), setRead: (read) => set((state) => ({ read })), setCount: (count) => set((state) => ({ count })), setNotis: (notis) => set(produce((state) => { state.notis = notis; state.nonce = notis?.length; })), addNotify: (obj) => set(produce((state) => { if (!obj) return; if (!obj.type) obj.type = "notify"; if (!obj.id) { state.nonce += 1; obj.id = `${Date.now()}${state.nonce}`; } if (!obj.date) obj.date = Date.now(); state.notis = [...state.notis, obj]; state.count += 1; })), addToast: (obj) => set(produce((state) => { if (!obj) return; if (!obj.type) obj.type = obj.remain ? "notify" : "toast"; if (!obj.id) { state.nonce += 1; obj.id = `${Date.now()}${state.nonce}`; } if (!obj.date) obj.date = Date.now(); state.toasts = [...state.toasts, obj]; if (obj.remain) { state.notis.push(obj); state.count += 1; } })), removeNotify: (id) => { if (!id) return; setTimeout(() => set(produce((state) => { state.notis = state.notis.filter(n => n.id !== id); state.count = state.notis.length; })), 300); }, removeToast: (id) => { if (!id) return; setTimeout(() => set(produce((state) => { state.toasts = state.toasts.filter(n => n.id !== id); })), 300); }, resetCount: () => set(() => ({ count: 0 })), saveNotis: (key) => { const { notis, count } = get(); // Use get to access current state if (key && key !== "") { const list = JSON.stringify(notis); localStorage?.setItem(`${key}.noti.list`, list); localStorage?.setItem(`${key}.noti.count`, count.toString()); } }, loadNotis: (key) => { if (key && key !== "") { const list = JSON.parse(localStorage?.getItem(`${key}.noti.list`) || "[]"); const count = parseInt(localStorage?.getItem(`${key}.noti.count`) || "0"); set((state) => ({ ...state, notis: list, count: count > 0 && !isNaN(count) ? count : state.count })); return list; } return []; } })); export default function useNotification() { return useNotificationStore(); } //# sourceMappingURL=useNotification.js.map