You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { IAppData, MenuItem } from '@/types'
  2. import { formatMenuData } from '@/utils'
  3. import { isAuthenticated } from '@/utils/auth.ts'
  4. import { atom, createStore } from 'jotai'
  5. import { atomWithQuery } from 'jotai-tanstack-query'
  6. import { atomWithStorage } from 'jotai/utils'
  7. import systemServ from '../service/system.ts'
  8. /**
  9. * app全局状态
  10. */
  11. export const appStore = createStore()
  12. export const appAtom = atomWithStorage<Partial<IAppData>>('app', {
  13. name: 'Crazy Pro',
  14. version: '1.0.0',
  15. language: 'zh-CN',
  16. })
  17. export const getAppData = () => {
  18. return appStore.get(appAtom)
  19. }
  20. export const getToken = () => {
  21. return appStore.get(appAtom).token
  22. }
  23. export const setToken = (token: string) => {
  24. appStore.set(appAtom, { token })
  25. }
  26. export const menuDataAtom = atomWithQuery(() => ({
  27. queryKey: [ 'menus' ],
  28. queryFn: async () => {
  29. if (!isAuthenticated()) {
  30. return []
  31. }
  32. return await systemServ.menus.list()
  33. },
  34. select: data => formatMenuData(data as any ?? []),
  35. }))
  36. export const selectedMenuIdAtom = atom<number>(0)
  37. export const selectedMenuAtom = atom<MenuItem | unknown>(undefined)
  38. export const byIdMenuAtom = atomWithQuery((get) => ({
  39. queryKey: [ 'selectedMenu', get(selectedMenuIdAtom) ],
  40. queryFn: async ({ queryKey: [ , id ] }) => {
  41. return await systemServ.menus.info(id as number)
  42. },
  43. select: data => data.data,
  44. }))