u
This commit is contained in:
1 parent
2efeb7cee2
commit
c0531c1d0a
35 files changed
+1817
-984
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { create } from 'zustand'
|
||||
import { enterDefaultSpaceApi } from '@/request/api/space'
|
||||
import { getUserSpaceApi } from '@/request/api/user'
|
||||
import { useMoneyCategoryStore } from './moneyCategory'
|
||||
import { useNavStore } from './navigation'
|
||||
@@ -33,26 +34,31 @@ export const useAppStore = create<AppState>((set, get) => ({
|
||||
}
|
||||
|
||||
try {
|
||||
// 初始化分类
|
||||
// 1️⃣ 初始化默认空间(如果用户当前没选中空间)
|
||||
if (!spaceStore.inSpace()) {
|
||||
const res = await enterDefaultSpaceApi()
|
||||
if (res.isSuccess) {
|
||||
if (res.data.space_id === 0) {
|
||||
// 跳转到空间管理页面
|
||||
}
|
||||
spaceStore.setCurrentSpace(res.data)
|
||||
}
|
||||
}
|
||||
|
||||
// 2️⃣ 初始化分类
|
||||
await moneyCategoryStore.init()
|
||||
|
||||
// 获取空间信息
|
||||
const res = await getUserSpaceApi()
|
||||
console.log(res.data)
|
||||
|
||||
spaceStore.setUserSpace(res.data)
|
||||
}
|
||||
catch (err) {
|
||||
console.error('app init error:', err)
|
||||
console.error('App init error:', err)
|
||||
}
|
||||
finally {
|
||||
// 5️⃣ 初始化完成
|
||||
set({ ready: true })
|
||||
}
|
||||
|
||||
// 初始化完成
|
||||
set({ ready: true })
|
||||
},
|
||||
|
||||
reset: () => {
|
||||
set({ ready: false })
|
||||
|
||||
useMoneyCategoryStore.getState().clear()
|
||||
useUserStore.getState().clear()
|
||||
useSpaceStore.getState().clear()
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import type { icons } from 'lucide-react-native'
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
import { router } from 'expo-router'
|
||||
import { create } from 'zustand'
|
||||
import { createJSONStorage, persist } from 'zustand/middleware'
|
||||
|
||||
export interface NavItem {
|
||||
label: string
|
||||
@@ -23,7 +21,7 @@ const DEFAULT_NAV_MENUS: NavItem[] = [
|
||||
{ label: '恋爱记', path: '/love', icon: 'Heart' },
|
||||
{ label: '聊天', path: '/chat', icon: 'MessageCircle' },
|
||||
{ label: '个人中心', path: '/user', icon: 'CircleUserRound' },
|
||||
{ label: '我的空间', path: '/space/manage', icon: 'Earth' },
|
||||
{ label: '空间管理', path: '/space/manage', icon: 'Earth' },
|
||||
]
|
||||
|
||||
export const useNavStore = create<NavState>()(
|
||||
|
||||
@@ -1,22 +1,33 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
import { create } from 'zustand'
|
||||
import { createJSONStorage, persist } from 'zustand/middleware'
|
||||
|
||||
interface Space {
|
||||
space_id: number
|
||||
role: number
|
||||
}
|
||||
|
||||
interface SpaceState {
|
||||
userSpace: UserSpace | null
|
||||
setUserSpace: (space: UserSpace | null) => void
|
||||
currentSpace: Space | null
|
||||
setCurrentSpace: (space: Space | null) => void
|
||||
clear: () => void
|
||||
inSpace: () => boolean
|
||||
}
|
||||
|
||||
export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
userSpace: null,
|
||||
setUserSpace: space => set({ userSpace: space }),
|
||||
clear: () => {
|
||||
set({
|
||||
userSpace: null,
|
||||
})
|
||||
},
|
||||
inSpace: () => {
|
||||
const userSpace = get().userSpace
|
||||
return userSpace !== null && userSpace.id !== 0
|
||||
},
|
||||
}))
|
||||
export const useSpaceStore = create<SpaceState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
currentSpace: null,
|
||||
setCurrentSpace: space => set({ currentSpace: space }),
|
||||
clear: () => set({ currentSpace: null }),
|
||||
inSpace: () => {
|
||||
const currentSpace = get().currentSpace
|
||||
return currentSpace !== null && currentSpace.space_id !== 0
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'space-storage',
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
},
|
||||
),
|
||||
)
|
||||
Reference in new issue
Block a user