menu.ts 667 B

1234567891011121314151617181920212223242526272829
  1. import {TreeRoleMenuData} from '@models';
  2. import {MENU_STORAGE} from '@utils';
  3. import {createStore} from 'zustand/vanilla';
  4. type State = {menus: TreeRoleMenuData[]};
  5. type Action = {
  6. setMenu(state: TreeRoleMenuData[]): void;
  7. clear(): void;
  8. };
  9. export const menuStore = createStore<State & Action>(function (set) {
  10. const menusStorage = sessionStorage.getItem(MENU_STORAGE);
  11. const menus = menusStorage
  12. ? (JSON.parse(menusStorage) as TreeRoleMenuData[])
  13. : [];
  14. return {
  15. menus,
  16. setMenu(menus) {
  17. sessionStorage.setItem(MENU_STORAGE, JSON.stringify(menus));
  18. set({menus});
  19. },
  20. clear() {
  21. set({menus: []});
  22. },
  23. };
  24. });