mt-flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
88 lines (87 loc) • 3.2 kB
JavaScript
;
'use client';
Object.defineProperty(exports, "__esModule", { value: true });
exports.useResStore = exports.ResAppProvider = exports.createResAppStore = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const zustand_1 = require("zustand");
const middleware_1 = require("zustand/middleware");
const resview_slice_1 = require("./resview.slice");
const middleware_2 = require("zustand/middleware");
const middleware_3 = require("zustand/middleware");
const dash_slice_1 = require("./dash.slice");
const createResAppStore = (initProps) => {
return (0, zustand_1.create)()((0, middleware_3.devtools)((0, middleware_1.persist)((0, middleware_2.subscribeWithSelector)((...a) => {
return {
...(0, resview_slice_1.createResViewSlice)(...a),
...(0, dash_slice_1.createDashSlice)(...a),
//初始值,
//提示:可以用SSR的方式设置初始值,能覆盖各个Slice中内部的初始值,这样,Slice内部可以不用管初始值的相关部分。
...initProps,
};
}), {
name: `resAppStore-${initProps?.resView?.id}`,
version: 1,
// skipHydration: true,
onRehydrateStorage(state) {
// console.log("onRehydrateStorage[resView]", state)
},
})));
};
exports.createResAppStore = createResAppStore;
//-----------context
const Context = (0, react_1.createContext)(null);
function ResAppProvider({ children, ...props }) {
const storeRef = (0, react_1.useRef)();
if (!storeRef.current) {
storeRef.current = (0, exports.createResAppStore)(props);
}
(0, react_1.useEffect)(() => {
storeRef.current?.getState().setParentItemId(props?.parentItemId);
}, [
props?.parentItemId
]);
(0, react_1.useEffect)(() => {
console.log("props?.depth", props?.depth);
storeRef.current?.getState().setDepth(props?.depth);
}, [
props?.depth
]);
(0, react_1.useEffect)(() => {
console.log("props?.resView", props?.resView);
if (props?.resView) {
storeRef.current?.getState().setResView(props.resView);
}
}, [
props?.resView
]);
return ((0, jsx_runtime_1.jsxs)(Context.Provider, { value: storeRef.current, children: [children, (0, jsx_runtime_1.jsx)(SubscriptionSetup, {})] }));
}
exports.ResAppProvider = ResAppProvider;
//------------事件订阅
function SubscriptionSetup() {
const store = useResStore();
(0, react_1.useEffect)(() => {
// const unsub2 = store.subscribe((state) => state.items, (a) => {
// })
return () => {
// unsub2()
};
}, []);
(0, react_1.useEffect)(() => {
store.getState().LoadViewConfig();
}, []);
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}));
}
function useResStore(selector, equals) {
const store = (0, react_1.useContext)(Context);
if (!store)
throw new Error('Missing ResAppProvider');
if (selector) {
return (0, zustand_1.useStore)(store, selector, equals);
}
else {
return store;
}
}
exports.useResStore = useResStore;