62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
|
|
// Добавляем определение типа Config
|
|
interface Config {
|
|
username: string;
|
|
password: string;
|
|
memory: number;
|
|
comfortVersion: string;
|
|
accessToken: string;
|
|
clientToken: string;
|
|
uuid?: string; // Добавляем uuid, который используется для авторизации
|
|
}
|
|
|
|
const useConfig = () => {
|
|
const [config, setConfig] = useState<Config>({
|
|
username: '',
|
|
password: '',
|
|
memory: 4096,
|
|
comfortVersion: '',
|
|
accessToken: '',
|
|
clientToken: '',
|
|
});
|
|
|
|
const loadInitialConfig = () => {
|
|
try {
|
|
const savedConfig = localStorage.getItem('launcher_config');
|
|
if (savedConfig) {
|
|
return JSON.parse(savedConfig);
|
|
}
|
|
} catch (error) {
|
|
console.log('Ошибка загрузки конфигурации:', error);
|
|
}
|
|
return {
|
|
username: '',
|
|
password: '',
|
|
comfortVersion: '',
|
|
accessToken: '',
|
|
clientToken: '',
|
|
};
|
|
};
|
|
|
|
useEffect(() => {
|
|
const savedConfig = loadInitialConfig();
|
|
setConfig(savedConfig);
|
|
}, []);
|
|
|
|
const saveConfig = (newConfig: Partial<Config>) => {
|
|
const updatedConfig = { ...config, ...newConfig };
|
|
setConfig(updatedConfig);
|
|
localStorage.setItem('launcher_config', JSON.stringify(updatedConfig));
|
|
};
|
|
|
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const { name, value } = e.target;
|
|
setConfig((prev) => ({ ...prev, [name]: value }));
|
|
};
|
|
|
|
return { config, setConfig, saveConfig, handleInputChange };
|
|
};
|
|
|
|
export default useConfig;
|