mirror of
https://github.com/fankes/komari-theme-purcarte.git
synced 2025-12-12 20:46:43 +08:00
fix: 修复私有状态处理逻辑
This commit is contained in:
@@ -5,8 +5,9 @@ import {
|
|||||||
useContext,
|
useContext,
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { wsService } from "../services/api";
|
import { getWsService } from "../services/api";
|
||||||
import type { NodeStats } from "../types/node";
|
import type { NodeStats } from "../types/node";
|
||||||
|
import { useNodeData } from "./NodeDataContext";
|
||||||
|
|
||||||
interface LiveData {
|
interface LiveData {
|
||||||
online: string[];
|
online: string[];
|
||||||
@@ -38,30 +39,37 @@ export const LiveDataProvider = ({
|
|||||||
enableWebSocket = true,
|
enableWebSocket = true,
|
||||||
}: LiveDataProviderProps) => {
|
}: LiveDataProviderProps) => {
|
||||||
const [liveData, setLiveData] = useState<LiveData | null>(null);
|
const [liveData, setLiveData] = useState<LiveData | null>(null);
|
||||||
|
const { nodes, loading } = useNodeData();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!enableWebSocket) {
|
// 只有在加载完成、站点非私有且启用了 WebSocket 时才连接
|
||||||
|
if (!loading && nodes !== "private" && enableWebSocket) {
|
||||||
|
const wsService = getWsService(); // 在需要时才获取实例
|
||||||
|
console.log("连接------", loading, nodes, enableWebSocket);
|
||||||
|
|
||||||
|
const handleWebSocketData = (data: LiveData) => {
|
||||||
|
if (data.online && data.data) {
|
||||||
|
setLiveData({
|
||||||
|
online: [...data.online],
|
||||||
|
data: { ...data.data },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const unsubscribe = wsService.subscribe(handleWebSocketData);
|
||||||
|
wsService.connect();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
unsubscribe();
|
||||||
|
wsService.disconnect(); // 确保在组件卸载或条件变化时断开连接
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// 如果条件不满足,确保断开任何现有连接
|
||||||
|
const wsService = getWsService(); // 获取实例以调用 disconnect
|
||||||
wsService.disconnect();
|
wsService.disconnect();
|
||||||
setLiveData(null);
|
setLiveData(null);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}, [loading, nodes, enableWebSocket]);
|
||||||
const handleWebSocketData = (data: LiveData) => {
|
|
||||||
if (data.online && data.data) {
|
|
||||||
setLiveData({
|
|
||||||
online: [...data.online],
|
|
||||||
data: { ...data.data },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const unsubscribe = wsService.subscribe(handleWebSocketData);
|
|
||||||
wsService.connect();
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
unsubscribe();
|
|
||||||
};
|
|
||||||
}, [enableWebSocket]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LiveDataContext.Provider value={{ liveData }}>
|
<LiveDataContext.Provider value={{ liveData }}>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import type { NodeData, PublicInfo, HistoryRecord } from "../types/node";
|
|||||||
function useNodesInternal() {
|
function useNodesInternal() {
|
||||||
const [staticNodes, setStaticNodes] = useState<NodeData[] | "private">([]);
|
const [staticNodes, setStaticNodes] = useState<NodeData[] | "private">([]);
|
||||||
const [publicSettings, setPublicSettings] = useState<PublicInfo | null>(null);
|
const [publicSettings, setPublicSettings] = useState<PublicInfo | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
const fetchNodes = useCallback(async () => {
|
const fetchNodes = useCallback(async () => {
|
||||||
|
|||||||
@@ -231,5 +231,12 @@ export class WebSocketService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建 WebSocket 服务实例
|
// 延迟 WebSocket 服务实例的创建
|
||||||
export const wsService = new WebSocketService();
|
let wsServiceInstance: WebSocketService | null = null;
|
||||||
|
|
||||||
|
export function getWsService(): WebSocketService {
|
||||||
|
if (!wsServiceInstance) {
|
||||||
|
wsServiceInstance = new WebSocketService();
|
||||||
|
}
|
||||||
|
return wsServiceInstance;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user