Files
var-monorepo/apps/dispatch/app/_store/aircraftsStore.ts
2025-04-10 21:24:05 -07:00

108 lines
2.0 KiB
TypeScript

import { create } from "zustand";
export interface Aircraft {
id: string;
bosName: string;
bosNameShort: string;
fmsStatus: string;
fmsLog: {
status: string;
timestamp: string;
user: string;
}[];
location: {
lat: number;
lon: number;
altitude: number;
speed: number;
};
locationHistory: {
lat: number;
lon: number;
altitude: number;
speed: number;
timestamp: string;
}[];
}
interface AircraftStore {
aircrafts: Aircraft[];
setAircrafts: (aircrafts: Aircraft[]) => void;
setAircraft: (aircraft: Aircraft) => void;
}
export const useAircraftsStore = create<AircraftStore>((set) => ({
aircrafts: [
{
id: "1",
bosName: "Aircraft 1",
bosNameShort: "A1",
fmsStatus: "1",
fmsLog: [],
location: {
lat: 52.546781040592776,
lon: 13.369535209542219,
altitude: 0,
speed: 0,
},
locationHistory: [],
},
{
id: "2",
bosName: "Aircraft 2",
bosNameShort: "A2",
fmsStatus: "2",
fmsLog: [],
location: {
lat: 52.54588546048977,
lon: 13.46470691054384,
altitude: 0,
speed: 0,
},
locationHistory: [],
},
{
id: "3",
bosName: "Aircraft 3",
bosNameShort: "A3",
fmsStatus: "3",
fmsLog: [],
location: {
lat: 52.497519717230155,
lon: 13.342040806552554,
altitude: 0,
speed: 0,
},
locationHistory: [],
},
{
id: "4",
bosName: "Aircraft 4",
bosNameShort: "A4",
fmsStatus: "6",
fmsLog: [],
location: {
lat: 52.50175041192073,
lon: 13.478628701227349,
altitude: 0,
speed: 0,
},
locationHistory: [],
},
],
setAircrafts: (aircrafts) => set({ aircrafts }),
setAircraft: (aircraft) =>
set((state) => {
const existingAircraftIndex = state.aircrafts.findIndex(
(a) => a.id === aircraft.id,
);
if (existingAircraftIndex !== -1) {
const updatedAircrafts = [...state.aircrafts];
updatedAircrafts[existingAircraftIndex] = aircraft;
return { aircrafts: updatedAircrafts };
} else {
return { aircrafts: [...state.aircrafts, aircraft] };
}
}),
}));