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>('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(0) export const selectedMenuAtom = atom(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, }))