Update migrated js to ts and shadcn
This commit is contained in:
90
src/hooks/useAppState.ts
Normal file
90
src/hooks/useAppState.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import React from 'react';
|
||||
import { LOCAL_STORAGE_KEY, getDefaultState, type AppState } from '../constants/defaults';
|
||||
|
||||
export const useAppState = () => {
|
||||
const [appState, setAppState] = React.useState<AppState>(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 = <K extends keyof AppState>(key: K, value: AppState[K]) => {
|
||||
setAppState(prev => ({ ...prev, [key]: value }));
|
||||
};
|
||||
|
||||
const updateNestedState = <P extends keyof AppState>(
|
||||
parentKey: P,
|
||||
childKey: string,
|
||||
value: any
|
||||
) => {
|
||||
setAppState(prev => ({
|
||||
...prev,
|
||||
[parentKey]: { ...(prev[parentKey] as any), [childKey]: value }
|
||||
}));
|
||||
};
|
||||
|
||||
const updateUiSetting = <K extends keyof AppState['uiSettings']>(
|
||||
key: K,
|
||||
value: AppState['uiSettings'][K]
|
||||
) => {
|
||||
const newUiSettings = { ...appState.uiSettings, [key]: value };
|
||||
if (key === 'simulationDays') {
|
||||
const simDaysNum = parseInt(value as string, 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
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user