Update App.js, new modular structure

This commit is contained in:
2025-10-18 21:19:17 +02:00
parent b666b35fed
commit 0fb168b050
16 changed files with 1118 additions and 453 deletions

83
src/hooks/useAppState.js Normal file
View File

@@ -0,0 +1,83 @@
import React from 'react';
import { LOCAL_STORAGE_KEY, getDefaultState } from '../constants/defaults.js';
export const useAppState = () => {
const [appState, setAppState] = React.useState(getDefaultState);
const [isLoaded, setIsLoaded] = React.useState(false);
React.useEffect(() => {
try {
const savedState = window.localStorage.getItem(LOCAL_STORAGE_KEY);
if (savedState) {
const parsedState = JSON.parse(savedState);
const defaults = getDefaultState();
setAppState({
...defaults,
...parsedState,
pkParams: {...defaults.pkParams, ...parsedState.pkParams},
uiSettings: {...defaults.uiSettings, ...parsedState.uiSettings},
});
}
} catch (error) {
console.error("Failed to load state", error);
}
setIsLoaded(true);
}, []);
React.useEffect(() => {
if (isLoaded) {
try {
const stateToSave = {
pkParams: appState.pkParams,
doses: appState.doses,
steadyStateConfig: appState.steadyStateConfig,
therapeuticRange: appState.therapeuticRange,
doseIncrement: appState.doseIncrement,
uiSettings: appState.uiSettings,
};
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(stateToSave));
} catch (error) {
console.error("Failed to save state", error);
}
}
}, [appState, isLoaded]);
const updateState = (key, value) => {
setAppState(prev => ({ ...prev, [key]: value }));
};
const updateNestedState = (parentKey, childKey, value) => {
setAppState(prev => ({
...prev,
[parentKey]: { ...prev[parentKey], [childKey]: value }
}));
};
const updateUiSetting = (key, value) => {
const newUiSettings = { ...appState.uiSettings, [key]: value };
if (key === 'simulationDays') {
const simDaysNum = parseInt(value, 10) || 1;
const dispDaysNum = parseInt(newUiSettings.displayedDays, 10) || 1;
if (dispDaysNum > simDaysNum) {
newUiSettings.displayedDays = String(simDaysNum);
}
}
setAppState(prev => ({ ...prev, uiSettings: newUiSettings }));
};
const handleReset = () => {
if (window.confirm("Bist du sicher, dass du alle Einstellungen auf die Standardwerte zurücksetzen möchtest? Dies kann nicht rückgängig gemacht werden.")) {
window.localStorage.removeItem(LOCAL_STORAGE_KEY);
window.location.reload();
}
};
return {
appState,
isLoaded,
updateState,
updateNestedState,
updateUiSetting,
handleReset
};
};