Files
popa-launcher/src/renderer/components/ServerStatus/ServerStatus.tsx
2025-12-05 01:29:06 +05:00

119 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Box, Typography, Avatar } from '@mui/material';
import { useEffect, useState } from 'react';
interface ServerStatusProps {
serverIp: string;
serverPort?: number;
refreshInterval?: number; // Интервал обновления в миллисекундах
}
const ServerStatus = ({
serverIp,
serverPort,
refreshInterval = 60000, // По умолчанию обновление раз в минуту
}: ServerStatusProps) => {
const [serverStatus, setServerStatus] = useState<{
online: number;
max: number;
loading: boolean;
error: string | null;
icon: string | null;
motd: string;
}>({
online: 0,
max: 0,
loading: true,
error: null,
icon: null,
motd: '',
});
useEffect(() => {
// Функция для получения статуса сервера
const fetchServerStatus = async () => {
try {
setServerStatus((prev) => ({ ...prev, loading: true, error: null }));
console.log('Отправляем запрос на сервер с параметрами:', {
host: serverIp,
port: serverPort || 25565,
});
// Проверяем, что serverIp имеет значение
if (!serverIp) {
throw new Error('Адрес сервера не указан');
}
const result = await window.electron.ipcRenderer.invoke(
'get-server-status',
{
host: serverIp,
port: serverPort || 25565,
},
);
if (result.success) {
setServerStatus({
online: result.online,
max: result.max,
loading: false,
error: null,
icon: result.icon,
motd: result.motd || serverIp,
});
} else {
setServerStatus({
online: 0,
max: 0,
loading: false,
error: result.error || 'Неизвестная ошибка',
icon: null,
motd: '',
});
}
} catch (error) {
console.error('Ошибка при получении статуса сервера:', error);
setServerStatus((prev) => ({
...prev,
loading: false,
error: 'Ошибка при получении статуса сервера',
icon: null,
}));
}
};
// Загрузка при первом рендере
fetchServerStatus();
// Периодическое обновление
const interval = setInterval(fetchServerStatus, refreshInterval);
return () => clearInterval(interval);
}, [serverIp, serverPort, refreshInterval]);
return (
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
{/* Отображаем иконку сервера или иконку по умолчанию */}
{serverStatus.icon ? (
<Avatar
src={serverStatus.icon}
alt={serverStatus.motd || 'Minecraft сервер'}
sx={{ width: '2em', height: '2em' }}
/>
) : (
<Avatar sx={{ width: '2em', height: '2em', bgcolor: 'primary.main' }}>
?
</Avatar>
)}
{serverStatus.error ? (
<Typography color="error">Ошибка загрузки</Typography>
) : (
<Typography sx={{ fontWeight: 'bold' }}>
{serverStatus.online} / {serverStatus.max} игроков
</Typography>
)}
</Box>
);
};
export default ServerStatus;