Files
xkhofmann-tfdidesign-md11-l…/PackageSources/js-bundle/src/components/stationEntry/StationEntryF.tsx
T
2026-02-13 02:50:43 +01:00

222 lines
9.1 KiB
TypeScript

import { FC, useEffect, useState } from 'react';
import { LoadingState } from '../../types/general';
import { WASMDataF } from '../../types/WASMData';
import { CoherentCallStationEntryF, loadAircraft, unloadAircraft } from '../../utils/utils';
import ActionBar from '../actionbar/ActionBar';
import Input from '../input/Input';
interface StationEntryProps {
WASMData: WASMDataF;
loadingState: LoadingState;
gsxActive: boolean;
gsxFuelActive: boolean;
setLoadingState: (newState: LoadingState) => void;
}
const StationEntryF: FC<StationEntryProps> = ({ WASMData, loadingState, gsxActive, gsxFuelActive, setLoadingState }) => {
const [upper1, setUpper1] = useState(WASMData.targetPayload.upper1);
const [upper2, setUpper2] = useState(WASMData.targetPayload.upper2);
const [upper3, setUpper3] = useState(WASMData.targetPayload.upper3);
const [upper4, setUpper4] = useState(WASMData.targetPayload.upper4);
const [lowerForward, setLowerForward] = useState(WASMData.targetPayload.lowerForward);
const [lowerRear, setLowerRear] = useState(WASMData.targetPayload.lowerRear);
const [fuel, setFuel] = useState(Math.round(WASMData.targetPayload.fuel));
const ZFW = () => {
if (loadingState !== 'loaded' && !gsxActive) return Math.round(WASMData.targetPayload.total);
return Math.round(WASMData.livePayload.total);
};
const ZFWValid = () => {
return ZFW() <= WASMData.limits.maxZFW;
};
const GW = () => {
return fuel + ZFW();
};
const GWValid = () => {
return GW() <= WASMData.limits.maxTOW;
};
const handleInput = (input: string, maxValue: number, setter: (value: number) => void) => {
if (!input) {
setter(0);
return;
}
const converted = parseInt(input);
if (converted) {
if (converted < 0) setter(0);
else if (converted > maxValue) setter(maxValue);
else setter(converted);
}
};
const updateData = () => {
CoherentCallStationEntryF(upper1, upper2, upper3, upper4, lowerForward, lowerRear, fuel);
};
useEffect(() => updateData(), [upper1, upper2, upper3, upper4, lowerForward, lowerRear, fuel]);
useEffect(
() =>
setFuel((prev) => {
if (prev > WASMData.limits.maxFuel) return WASMData.limits.maxFuel;
return prev;
}),
[WASMData.userData.isER]
);
return (
<>
<div className="block flex w-full flex-col opacity-100 transition-opacity duration-150 ease-linear mb-4">
<div className="relative flex w-full items-center justify-between rounded-md bg-zinc-600 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>Fuel ({WASMData.userData.isImperial ? 'lbs' : 'kg'})</label>
<Input
type="text"
placeholder=""
className={`w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right`}
value={fuel}
onChange={(value) => handleInput(value, WASMData.limits.maxFuel, setFuel)}
disabled={loadingState !== 'preview' || gsxFuelActive}
/>
</div>
</div>
</div>
<div className="block flex w-full flex-col opacity-100 transition-opacity duration-150 ease-linear mb-4">
<div className="relative flex w-full items-center justify-between rounded-t-md bg-zinc-600 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>Upper 1</label>
<Input
type="text"
placeholder=""
className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
value={upper1}
onChange={(value) => handleInput(value, WASMData.limits.upper1, setUpper1)}
disabled={loadingState !== 'preview' || gsxActive}
/>
</div>
</div>
<div className="relative flex w-full items-center justify-between bg-zinc-700 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>Upper 2</label>
<Input
type="text"
placeholder=""
className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
value={upper2}
onChange={(value) => handleInput(value, WASMData.limits.upper2, setUpper2)}
disabled={loadingState !== 'preview' || gsxActive}
/>
</div>
</div>
<div className="relative flex w-full items-center justify-between bg-zinc-600 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>Upper 3</label>
<Input
type="text"
placeholder=""
className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
value={upper3}
onChange={(value) => handleInput(value, WASMData.limits.upper3, setUpper3)}
disabled={loadingState !== 'preview' || gsxActive}
/>
</div>
</div>
<div className="relative flex w-full items-center justify-between bg-zinc-700 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>Upper 4</label>
<Input
type="text"
placeholder=""
className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
value={upper4}
onChange={(value) => handleInput(value, WASMData.limits.upper4, setUpper4)}
disabled={loadingState !== 'preview' || gsxActive}
/>
</div>
</div>
<div className="relative flex w-full items-center justify-between bg-zinc-600 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>Forward Cargo ({WASMData.userData.isImperial ? 'lbs' : 'kg'})</label>
<Input
type="text"
topKeyboard
placeholder=""
className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
value={lowerForward}
onChange={(value) => handleInput(value, WASMData.limits.lowerForward, setLowerForward)}
disabled={loadingState !== 'preview' || gsxActive}
/>
</div>
</div>
<div className="relative flex w-full items-center justify-between rounded-b-md bg-zinc-700 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>Aft Cargo ({WASMData.userData.isImperial ? 'lbs' : 'kg'})</label>
<Input
type="text"
topKeyboard
placeholder=""
className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
value={lowerRear}
onChange={(value) => handleInput(value, WASMData.limits.lowerRear, setLowerRear)}
disabled={loadingState !== 'preview' || gsxActive}
/>
</div>
</div>
</div>
<div className="block flex w-full flex-col opacity-100 transition-opacity duration-150 ease-linear mb-4">
<div className="relative flex w-full items-center justify-between rounded-t-md bg-zinc-600 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>
{loadingState !== 'loaded' && !gsxActive ? 'Expected' : 'Actual'} ZFW (
{WASMData.userData.isImperial ? 'lbs' : 'kg'})
</label>
<input
type="text"
placeholder=""
className={`w-1/2 rounded-lg border ${ZFWValid() ? 'border-white' : 'border-red-500 text-red-500'} bg-zinc-700 px-3 py-2 text-right`}
disabled
value={ZFW()}
/>
</div>
</div>
<div className="relative flex w-full items-center justify-between rounded-b-md bg-zinc-700 p-2 px-4">
<div className="flex w-full items-center justify-between text-xs">
<label>
{loadingState !== 'loaded' && !gsxActive ? 'Expected' : 'Actual'} GW (
{WASMData.userData.isImperial ? 'lbs' : 'kg'})
</label>
<input
type="text"
placeholder=""
className={`w-1/2 rounded-lg border ${GWValid() ? 'border-white' : 'border-red-500 text-red-500'} bg-zinc-700 px-3 py-2 text-right`}
disabled
value={GW()}
/>
</div>
</div>
</div>
<ActionBar
loadingState={loadingState}
loadDisabled={!ZFWValid() || !GWValid()}
GSXSync={WASMData.options.GSXSync}
load={() => {
setLoadingState('loaded');
loadAircraft();
}}
unload={() => {
setLoadingState('preview');
unloadAircraft();
}}
/>
</>
);
};
export default StationEntryF;