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