Working version(some)
This commit is contained in:
@ -5,7 +5,7 @@ import {
|
||||
Navigate,
|
||||
} from 'react-router-dom';
|
||||
import Login from './pages/Login';
|
||||
import Dashboard from './pages/Dashboard';
|
||||
import LaunchPage from './pages/LaunchPage';
|
||||
import { ReactNode, useEffect, useState } from 'react';
|
||||
import './App.css';
|
||||
|
||||
@ -66,7 +66,7 @@ const App = () => {
|
||||
path="/"
|
||||
element={
|
||||
<AuthCheck>
|
||||
<Dashboard />
|
||||
<LaunchPage />
|
||||
</AuthCheck>
|
||||
}
|
||||
/>
|
||||
|
@ -1,31 +0,0 @@
|
||||
import { Box, Typography, Button } from '@mui/material';
|
||||
import { useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
const Dashboard = () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
// Проверяем авторизацию при монтировании компонента
|
||||
const savedConfig = localStorage.getItem('launcher_config');
|
||||
if (!savedConfig || !JSON.parse(savedConfig).accessToken) {
|
||||
navigate('/login');
|
||||
}
|
||||
}, [navigate]);
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem('launcher_config');
|
||||
navigate('/login');
|
||||
};
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Typography variant="h4">Добро пожаловать в лаунчер</Typography>
|
||||
<Button onClick={handleLogout} variant="contained" color="error">
|
||||
Выйти
|
||||
</Button>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default Dashboard;
|
166
src/renderer/pages/LaunchPage.tsx
Normal file
166
src/renderer/pages/LaunchPage.tsx
Normal file
@ -0,0 +1,166 @@
|
||||
import { Box, Typography, Button, Snackbar, Alert } from '@mui/material';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
electron: {
|
||||
ipcRenderer: {
|
||||
invoke(channel: string, ...args: unknown[]): Promise<any>;
|
||||
on(channel: string, func: (...args: unknown[]) => void): void;
|
||||
removeAllListeners(channel: string): void;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const LaunchPage = () => {
|
||||
const navigate = useNavigate();
|
||||
const [isDownloading, setIsDownloading] = useState(false);
|
||||
const [downloadProgress, setDownloadProgress] = useState(0);
|
||||
const [installStatus, setInstallStatus] = useState('');
|
||||
const [notification, setNotification] = useState<{
|
||||
open: boolean;
|
||||
message: string;
|
||||
severity: 'success' | 'error' | 'info';
|
||||
}>({ open: false, message: '', severity: 'info' });
|
||||
const [installStep, setInstallStep] = useState('');
|
||||
const [installMessage, setInstallMessage] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
const savedConfig = localStorage.getItem('launcher_config');
|
||||
if (!savedConfig || !JSON.parse(savedConfig).accessToken) {
|
||||
navigate('/login');
|
||||
}
|
||||
|
||||
window.electron.ipcRenderer.on('download-progress', (progress: any) => {
|
||||
setDownloadProgress(progress as number);
|
||||
});
|
||||
|
||||
// Добавляем слушатель для статуса установки
|
||||
window.electron.ipcRenderer.on('installation-progress', (data: any) => {
|
||||
setInstallStatus((data as { status: string }).status);
|
||||
});
|
||||
|
||||
// Обновляем слушатель для статуса установки
|
||||
window.electron.ipcRenderer.on('installation-status', (data: any) => {
|
||||
const { step, message } = data as { step: string; message: string };
|
||||
setInstallStep(step);
|
||||
setInstallMessage(message);
|
||||
});
|
||||
|
||||
return () => {
|
||||
window.electron.ipcRenderer.removeAllListeners('download-progress');
|
||||
window.electron.ipcRenderer.removeAllListeners('installation-progress');
|
||||
window.electron.ipcRenderer.removeAllListeners('installation-status');
|
||||
};
|
||||
}, [navigate]);
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem('launcher_config');
|
||||
navigate('/login');
|
||||
};
|
||||
|
||||
const showNotification = (
|
||||
message: string,
|
||||
severity: 'success' | 'error' | 'info',
|
||||
) => {
|
||||
setNotification({ open: true, message, severity });
|
||||
};
|
||||
|
||||
const handleCloseNotification = () => {
|
||||
setNotification({ ...notification, open: false });
|
||||
};
|
||||
|
||||
const handleLaunchMinecraft = async () => {
|
||||
try {
|
||||
setIsDownloading(true);
|
||||
setDownloadProgress(0);
|
||||
|
||||
// Сначала проверяем и обновляем файлы
|
||||
const downloadResult = await window.electron.ipcRenderer.invoke(
|
||||
'download-and-extract',
|
||||
);
|
||||
|
||||
if (downloadResult?.success) {
|
||||
if (downloadResult.updated) {
|
||||
showNotification(
|
||||
`Сборка успешно обновлена до версии ${downloadResult.version}`,
|
||||
'success',
|
||||
);
|
||||
} else {
|
||||
showNotification(
|
||||
`Установлена актуальная версия сборки ${downloadResult.version}`,
|
||||
'info',
|
||||
);
|
||||
}
|
||||
|
||||
// Затем запускаем Minecraft
|
||||
const launchResult =
|
||||
await window.electron.ipcRenderer.invoke('launch-minecraft');
|
||||
|
||||
if (launchResult?.success) {
|
||||
showNotification('Minecraft успешно запущен!', 'success');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
showNotification(`Ошибка: ${error.message}`, 'error');
|
||||
} finally {
|
||||
setIsDownloading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Box sx={{ p: 3 }}>
|
||||
<Typography variant="h4" sx={{ mb: 3 }}>
|
||||
Добро пожаловать в лаунчер
|
||||
</Typography>
|
||||
|
||||
{isDownloading ? (
|
||||
<Box sx={{ mb: 3 }}>
|
||||
<Typography>Загрузка и установка: {downloadProgress}%</Typography>
|
||||
{installMessage && (
|
||||
<Typography variant="body1" sx={{ mt: 1, color: 'white' }}>
|
||||
{installMessage}
|
||||
</Typography>
|
||||
)}
|
||||
{installStep && (
|
||||
<Typography variant="body2" sx={{ color: 'white' }}>
|
||||
Шаг: {installStep}
|
||||
</Typography>
|
||||
)}
|
||||
</Box>
|
||||
) : (
|
||||
<Button
|
||||
variant="contained"
|
||||
color="primary"
|
||||
sx={{ mb: 3 }}
|
||||
onClick={handleLaunchMinecraft}
|
||||
>
|
||||
Запустить Minecraft
|
||||
</Button>
|
||||
)}
|
||||
|
||||
<Button onClick={handleLogout} variant="contained" color="error">
|
||||
Выйти
|
||||
</Button>
|
||||
|
||||
<Snackbar
|
||||
open={notification.open}
|
||||
autoHideDuration={6000}
|
||||
onClose={handleCloseNotification}
|
||||
>
|
||||
<Alert
|
||||
onClose={handleCloseNotification}
|
||||
severity={notification.severity}
|
||||
sx={{ width: '100%' }}
|
||||
>
|
||||
{notification.message}
|
||||
</Alert>
|
||||
</Snackbar>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default LaunchPage;
|
Reference in New Issue
Block a user