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

import { IAppData, MenuItem } from '@/types'
import { formatMenuData } from '@/utils'
import { isAuthenticated } from '@/utils/auth.ts'
import { atom, createStore } from 'jotai'
import { atomWithQuery } from 'jotai-tanstack-query'
import { atomWithStorage } from 'jotai/utils'
import systemServ from '../service/system.ts'
/**
* app全局状态
*/
export const appStore = createStore()
export const appAtom = atomWithStorage<Partial<IAppData>>('app', {
name: 'Crazy Pro',
version: '1.0.0',
language: 'zh-CN',
})
export const getAppData = () => {
return appStore.get(appAtom)
}
export const getToken = () => {
return appStore.get(appAtom).token
}
export const setToken = (token: string) => {
appStore.set(appAtom, { token })
}
export const menuDataAtom = atomWithQuery(() => ({
queryKey: [ 'menus' ],
queryFn: async () => {
if (!isAuthenticated()) {
return []
}
return await systemServ.menus.list()
},
select: data => formatMenuData(data as any ?? []),
}))
export const selectedMenuIdAtom = atom<number>(0)
export const selectedMenuAtom = atom<MenuItem | unknown>(undefined)
export const byIdMenuAtom = atomWithQuery((get) => ({
queryKey: [ 'selectedMenu', get(selectedMenuIdAtom) ],
queryFn: async ({ queryKey: [ , id ] }) => {
return await systemServ.menus.info(id as number)
},
select: data => data.data,
}))