import { create } from 'zustand' import { getNavbarHeight } from '@/utils/getNavbarHeight' interface GlobalState { location: Record getLocationLoading: boolean getLocationText: string statusNavbarHeightInfo: { statusBarHeight: number navbarHeight: number totalHeight: number } } interface GlobalActions { updateState: (payload: Record) => void getNavbarHeightInfo: () => void } // 完整的 Store 类型 type GlobalStore = GlobalState & GlobalActions // 创建 store export const useGlobalStore = create()((set, get) => ({ // 位置信息 location: {}, // 正在获取位置信息 getLocationLoading: false, // 获取位置信息文本 getLocationText: '定位中...', // 状态栏和导航栏高度信息 statusNavbarHeightInfo: { statusBarHeight: 0, navbarHeight: 0, totalHeight: 0 }, // 获取导航栏高度信息 getNavbarHeightInfo: () => { const { statusBarHeight, navbarHeight } = getNavbarHeight(); set({ statusNavbarHeightInfo: { statusBarHeight, navbarHeight, totalHeight: statusBarHeight + navbarHeight } }) }, // 更新store数据 updateState: (payload: Record) => { const state = get(); set({ ...state, ...(payload || {}) }) } })) // 导出便捷的 hooks export const useGlobalState = () => useGlobalStore((state) => state)