- 在 Providers 组件中引入 ErrorProvider 以处理全局错误状态 - 更新 PlayerAppShell 组件的注释,说明网络状态横幅的用途 - 在 lotteryHttp 中添加对 500、502、503 错误的处理,更新全局错误状态 - 导出 useNetworkStatus 和 useIsOffline 钩子以支持网络状态管理
73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
"use client";
|
||
|
||
import { create } from "zustand";
|
||
|
||
// 颜色常量
|
||
export const ERROR_COLORS = {
|
||
success: "#52c41a",
|
||
error: "#ff4d4f",
|
||
warning: "#faad14",
|
||
neutral: "#d9d9d9",
|
||
} as const;
|
||
|
||
type ErrorType = "network" | "server" | null;
|
||
|
||
interface ErrorState {
|
||
// 网络断开状态
|
||
isOffline: boolean;
|
||
setIsOffline: (value: boolean) => void;
|
||
|
||
// 服务器错误状态 (500)
|
||
isServerError: boolean;
|
||
serverErrorMessage: string;
|
||
setServerError: (isError: boolean, message?: string) => void;
|
||
clearServerError: () => void;
|
||
|
||
// 当前主要错误类型(优先级:server > network)
|
||
currentErrorType: ErrorType;
|
||
}
|
||
|
||
export const useErrorStore = create<ErrorState>((set, get) => ({
|
||
// 初始状态
|
||
isOffline: typeof window !== "undefined" ? !navigator.onLine : false,
|
||
isServerError: false,
|
||
serverErrorMessage: "服务器暂时不可用,请稍后重试",
|
||
currentErrorType: null,
|
||
|
||
// Actions
|
||
setIsOffline: (value: boolean) => {
|
||
set({ isOffline: value });
|
||
// 更新当前错误类型
|
||
const { isServerError } = get();
|
||
if (isServerError) {
|
||
set({ currentErrorType: "server" });
|
||
} else if (value) {
|
||
set({ currentErrorType: "network" });
|
||
} else {
|
||
set({ currentErrorType: null });
|
||
}
|
||
},
|
||
|
||
setServerError: (isError: boolean, message?: string) => {
|
||
set({
|
||
isServerError: isError,
|
||
serverErrorMessage: message || "服务器暂时不可用,请稍后重试",
|
||
});
|
||
// 更新当前错误类型 - 服务器错误优先级更高
|
||
if (isError) {
|
||
set({ currentErrorType: "server" });
|
||
} else {
|
||
const { isOffline } = get();
|
||
set({ currentErrorType: isOffline ? "network" : null });
|
||
}
|
||
},
|
||
|
||
clearServerError: () => {
|
||
const { isOffline } = get();
|
||
set({
|
||
isServerError: false,
|
||
currentErrorType: isOffline ? "network" : null,
|
||
});
|
||
},
|
||
}));
|