102 lines
2.5 KiB
TypeScript
102 lines
2.5 KiB
TypeScript
import { create } from "zustand";
|
|
import { getNavbarHeight } from "@/utils/getNavbarHeight";
|
|
import { getCurrentLocation } from "@/utils/locationUtils";
|
|
|
|
interface GlobalState {
|
|
location: Record<string, any>;
|
|
getLocationLoading: boolean;
|
|
getLocationText: string;
|
|
statusNavbarHeightInfo: {
|
|
statusBarHeight: number;
|
|
navBarHeight: number;
|
|
totalHeight: number;
|
|
};
|
|
// GuideBar 显示/隐藏状态
|
|
showGuideBar: boolean;
|
|
// GuideBar z-index 层级
|
|
guideBarZIndex: 'low' | 'high';
|
|
}
|
|
|
|
interface GlobalActions {
|
|
updateState: (payload: Record<string, any>) => void;
|
|
getNavbarHeightInfo: () => void;
|
|
getCurrentLocationInfo: () => any;
|
|
// GuideBar 控制方法
|
|
setShowGuideBar: (show: boolean) => void;
|
|
setGuideBarZIndex: (zIndex: 'low' | 'high') => void;
|
|
toggleGuideBar: () => void;
|
|
}
|
|
// 完整的 Store 类型
|
|
type GlobalStore = GlobalState & GlobalActions;
|
|
|
|
// 创建 store
|
|
export const useGlobalStore = create<GlobalStore>()((set, get) => ({
|
|
// 位置信息
|
|
location: {},
|
|
// 正在获取位置信息
|
|
getLocationLoading: true,
|
|
// 获取位置信息文本
|
|
getLocationText: "定位中...",
|
|
|
|
// 状态栏和导航栏高度信息
|
|
statusNavbarHeightInfo: {
|
|
statusBarHeight: 0,
|
|
navBarHeight: 0,
|
|
totalHeight: 0,
|
|
},
|
|
|
|
// GuideBar 状态
|
|
showGuideBar: true,
|
|
guideBarZIndex: 'high',
|
|
|
|
// 获取导航栏高度信息
|
|
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<string, any>) => {
|
|
const state = get();
|
|
set({
|
|
...state,
|
|
...(payload || {}),
|
|
});
|
|
},
|
|
|
|
// GuideBar 控制方法
|
|
setShowGuideBar: (show: boolean) => {
|
|
console.log('[Store] setShowGuideBar called with:', show);
|
|
set({ showGuideBar: show });
|
|
console.log('[Store] showGuideBar updated to:', show);
|
|
},
|
|
|
|
setGuideBarZIndex: (zIndex: 'low' | 'high') => {
|
|
set({ guideBarZIndex: zIndex });
|
|
},
|
|
|
|
toggleGuideBar: () => {
|
|
const { showGuideBar } = get();
|
|
set({ showGuideBar: !showGuideBar });
|
|
},
|
|
}));
|
|
|
|
// 导出便捷的 hooks
|
|
export const useGlobalState = () => useGlobalStore((state) => state);
|