zustand-fetching
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -1,10 +0,5 @@ | ||
export type LeitenModalState = { | ||
open: boolean; | ||
hidden: boolean; | ||
}; | ||
export interface LeitenModalManagerState { | ||
modals: Record<string, LeitenModalState>; | ||
queue: string[]; | ||
} | ||
export declare const useLeitenModalManager: import("zustand").UseBoundStore<import("zustand").StoreApi<LeitenModalManagerState>>; | ||
export declare const useLeitenModalStack: import("zustand").UseBoundStore<import("zustand").StoreApi<LeitenModalManagerState>>; | ||
export declare const leitenModalManagerAction: (key: string, value: boolean, replace?: boolean) => void; |
import { produce } from "immer"; | ||
import { create } from "zustand"; | ||
export const useLeitenModalManager = create(() => ({ | ||
modals: {}, | ||
export const useLeitenModalStack = create(() => ({ | ||
queue: [], | ||
})); | ||
export const leitenModalManagerAction = (key, value, replace) => { | ||
const nextState = produce(useLeitenModalManager.getState(), (draft) => { | ||
draft.modals[key] = { open: value, hidden: false }; | ||
const nextState = produce(useLeitenModalStack.getState(), (draft) => { | ||
let queue = draft.queue.filter((modal) => modal !== key); | ||
if (!replace) { | ||
queue.forEach((item) => { | ||
draft.modals[item].hidden = true; | ||
}); | ||
} | ||
else { | ||
queue.forEach((item) => { | ||
draft.modals[item].hidden = false; | ||
draft.modals[item].open = false; | ||
}); | ||
if (replace) { | ||
queue = []; | ||
@@ -26,10 +15,5 @@ } | ||
} | ||
const last = queue[queue.length - 1]; | ||
if (last) { | ||
draft.modals[last].open = true; | ||
draft.modals[last].hidden = false; | ||
} | ||
draft.queue = queue; | ||
}); | ||
useLeitenModalManager.setState(nextState); | ||
useLeitenModalStack.setState(nextState); | ||
}; |
@@ -5,3 +5,3 @@ import { produce } from "immer"; | ||
import { shallow } from "zustand/shallow"; | ||
import { leitenModalManagerAction, useLeitenModalManager, } from "./hooks/useLeitenModals"; | ||
import { leitenModalManagerAction, useLeitenModalStack, } from "./hooks/useLeitenModals"; | ||
export const leitenModal = (store, path, extra) => { | ||
@@ -22,3 +22,3 @@ const initialData = get(store.getState(), path, "_empty"); | ||
}; | ||
const getState = () => useLeitenModalManager.getState().modals[key]; | ||
const isOpen = () => useLeitenModalStack.getState().queue.includes(key); | ||
const action = (params) => { | ||
@@ -37,4 +37,4 @@ var _a; | ||
else if (params.type === "TOGGLE") { | ||
setState(!getState()); | ||
if (!getState() && (extra === null || extra === void 0 ? void 0 : extra.clearOnClose)) { | ||
setState(!isOpen()); | ||
if (!isOpen() && (extra === null || extra === void 0 ? void 0 : extra.clearOnClose)) { | ||
setContent(initialData); | ||
@@ -53,8 +53,6 @@ } | ||
const useOpen = () => { | ||
return useLeitenModalManager((state) => { | ||
var _a, _b, _c, _d; | ||
return [ | ||
(_b = (_a = state.modals[key]) === null || _a === void 0 ? void 0 : _a.open) !== null && _b !== void 0 ? _b : false, | ||
(_d = (_c = state.modals[key]) === null || _c === void 0 ? void 0 : _c.hidden) !== null && _d !== void 0 ? _d : false, | ||
]; | ||
return useLeitenModalStack((state) => { | ||
const open = state.queue.includes(key); | ||
const hidden = open && state.queue[state.queue.length - 1] !== key; | ||
return [open, hidden]; | ||
}, shallow); | ||
@@ -61,0 +59,0 @@ }; |
{ | ||
"name": "zustand-fetching", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Zustand fetching helpers", |
88092
1785