Spaces:
Runtime error
Runtime error
File size: 5,493 Bytes
cdb8847 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | import React, { useState, useEffect, useCallback } from 'react';
import apiClient from '../api/apiClient';
import { HealthStatus } from '../types/types';
interface PingLabServerProps {
autoCheckInterval?: number; // 自动检查间隔时间(毫秒),默认为30秒
showLatency?: boolean; // 是否显示延迟时间
}
const PingLabServer: React.FC<PingLabServerProps> = ({
autoCheckInterval = 30000,
showLatency = true,
}) => {
const [status, setStatus] = useState<HealthStatus | null>(null);
const [isChecking, setIsChecking] = useState<boolean>(false);
const [lastChecked, setLastChecked] = useState<Date | null>(null);
// 检查服务器状态
const checkServerStatus = useCallback(async () => {
setIsChecking(true);
try {
const result = await apiClient.checkHealth();
if (result.data) {
setStatus(result.data);
} else {
setStatus({
status: 'offline',
message: result.error || '未知错误',
});
}
} catch (error) {
setStatus({
status: 'offline',
message: error instanceof Error ? error.message : '检查失败',
});
} finally {
setIsChecking(false);
setLastChecked(new Date());
}
}, []);
// 手动检查
const handleManualCheck = () => {
checkServerStatus();
};
// 自动定时检查
useEffect(() => {
// 初始检查
checkServerStatus();
// 设置定时器
const intervalId = setInterval(checkServerStatus, autoCheckInterval);
// 清理定时器
return () => clearInterval(intervalId);
}, [checkServerStatus, autoCheckInterval]);
// 格式化上次检查时间
const getFormattedLastChecked = () => {
if (!lastChecked) return '尚未检查';
// 格式化为 HH:MM:SS
return lastChecked.toLocaleTimeString();
};
return (
<div className={`ping-server ${status?.status || 'unknown'}`}>
<h3>服务器状态</h3>
<div className="status-display">
<div className="status-indicator">
<span className={`status-dot ${status?.status || 'unknown'}`}></span>
<span className="status-text">
{status?.status === 'online' ? '在线' :
status?.status === 'offline' ? '离线' : '未知'}
</span>
</div>
{status?.message && (
<div className="status-message">{status.message}</div>
)}
{showLatency && status?.latency && (
<div className="status-latency">
延迟: {status.latency}ms
</div>
)}
<div className="last-checked">
上次检查: {getFormattedLastChecked()}
</div>
</div>
<button
onClick={handleManualCheck}
disabled={isChecking}
className="check-button"
>
{isChecking ? '检查中...' : '检查状态'}
</button>
<style jsx>{`
.ping-server {
padding: 15px;
border-radius: 6px;
background-color: #f8f9fa;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}
.ping-server.online {
border-left: 4px solid #28a745;
}
.ping-server.offline {
border-left: 4px solid #dc3545;
}
.ping-server.unknown {
border-left: 4px solid #6c757d;
}
h3 {
margin-top: 0;
margin-bottom: 15px;
font-size: 18px;
}
.status-display {
margin-bottom: 15px;
}
.status-indicator {
display: flex;
align-items: center;
margin-bottom: 8px;
}
.status-dot {
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 8px;
}
.status-dot.online {
background-color: #28a745;
box-shadow: 0 0 8px rgba(40, 167, 69, 0.6);
animation: pulse 2s infinite;
}
.status-dot.offline {
background-color: #dc3545;
}
.status-dot.unknown {
background-color: #6c757d;
}
.status-text {
font-weight: 500;
}
.status-message {
font-size: 14px;
margin-bottom: 8px;
}
.status-latency {
font-size: 14px;
color: #495057;
margin-bottom: 8px;
}
.last-checked {
font-size: 12px;
color: #6c757d;
}
.check-button {
padding: 6px 12px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s;
}
.check-button:hover:not(:disabled) {
background-color: #0069d9;
}
.check-button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
@keyframes pulse {
0% {
box-shadow: 0 0 0 0 rgba(40, 167, 69, 0.6);
}
70% {
box-shadow: 0 0 0 6px rgba(40, 167, 69, 0);
}
100% {
box-shadow: 0 0 0 0 rgba(40, 167, 69, 0);
}
}
`}</style>
</div>
);
};
export default PingLabServer;
|