mt-flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
77 lines (76 loc) • 2.71 kB
JavaScript
'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { createErrorSlice } from '@/store/error.slice';
import { createContext, useContext, useRef } from 'react';
import { create, useStore } from 'zustand';
import { devtools, persist, subscribeWithSelector } from 'zustand/middleware';
import { createAppAdminSlice } from './app-admin.slice';
import { createAppSlice } from './app.slice';
import { createConfigSlice } from './config.slice';
import { createDesignSlice } from './design.slice';
export const createResAppStore = (initProps) => {
return create()(devtools(persist(subscribeWithSelector((...a) => {
return {
...createAppSlice(...a),
...createDesignSlice(...a),
...createAppAdminSlice(...a),
...createConfigSlice(...a),
...createErrorSlice(...a),
//初始值,
//提示:可以用SSR的方式设置初始值,能覆盖各个Slice中内部的初始值,这样,Slice内部可以不用管初始值的相关部分。
...initProps,
};
}), {
name: `app`,
version: 1,
// skipHydration: true,
onRehydrateStorage(state) {
// console.log("onRehydrateStorage[res]", state)
},
})));
};
//-----------context
const Context = createContext(null);
export function AppProvider({ children, ...props }) {
const storeRef = useRef();
if (!storeRef.current) {
storeRef.current = createResAppStore(props);
}
return (_jsxs(Context.Provider, { value: storeRef.current, children: [children, _jsx(SubscriptionSetup, {})] }));
}
//------------事件订阅
function SubscriptionSetup() {
// const store = useAppStore()
// const setDebug = useAppStore(x => x.setDebug)
// useEffect(() => {
// const unsub2 = store.subscribe((state) => state.items, (a) => {
// // console.log("items 事件:", a)
// })
// const unsubActivateId = store.subscribe((state) => state.activateId, (value) => {
// // console.log("activateId 事件:", value)
// })
// return () => {
// unsub2()
// unsubActivateId()
// }
// }, [])
// useEffect(() => {
// store.getState().LoadViewConfig()
// }, [])
// useEffect(() => {
// setDebug(true)
// }, [])
return _jsx(_Fragment, {});
}
export function useAppStore(selector, equals) {
const store = useContext(Context);
if (!store)
throw new Error('Missing AppProvider');
if (selector) {
// eslint-disable-next-line
return useStore(store, selector, equals);
}
else {
return store;
}
}