110 lines
3.2 KiB
JavaScript
110 lines
3.2 KiB
JavaScript
export const useFlightPlanUtils = () => {
|
|
const getIcon = (type) => {
|
|
const icons = {
|
|
"poi": "pin_drop",
|
|
"geocode": "travel_explore",
|
|
"airport": "local_airport"
|
|
};
|
|
|
|
return icons[type] || "place"
|
|
}
|
|
|
|
const getMarkersFrom = (flightPlan) => {
|
|
const segments = flightPlan.navlog.length;
|
|
|
|
const markers = flightPlan.navlog.map(m => m.from);
|
|
if (segments > 0 && flightPlan.navlog[segments - 1].to) {
|
|
markers.push(flightPlan.navlog[segments - 1].to);
|
|
}
|
|
|
|
return markers;
|
|
}
|
|
|
|
return {getIcon, getMarkersFrom}
|
|
}
|
|
|
|
export const useFlightPlanCalculations = () => {
|
|
const getDuration = (distance, speed) => Math.round(distance / speed * 60)
|
|
|
|
const getTotalDistance = (flightPlan) => {
|
|
if (!flightPlan.navlog.length) return null;
|
|
|
|
return Math.round(flightPlan.navlog.map(t => t.distance).reduce((a, b) => a + b));
|
|
}
|
|
|
|
const getTotalDuration = (flightPlan, plannedSpeed = undefined) => {
|
|
if (!plannedSpeed) plannedSpeed = flightPlan.plannedSpeed;
|
|
if (!flightPlan.navlog.length) return null;
|
|
|
|
return flightPlan.navlog.map(t => getDuration(t.distance, plannedSpeed)).reduce((a, b) => a + b)
|
|
}
|
|
|
|
return {getDuration, getTotalDistance, getTotalDuration}
|
|
}
|
|
|
|
export const useFlightPlanObjects = () => {
|
|
const editableNameTypes = ["geocode", "mapPoint"];
|
|
|
|
const markerObject = ({name, type, airportId, pointOfInterestId, gpsLatitude, gpsLongitude}) => ({
|
|
name, type, gpsLatitude, gpsLongitude, pointOfInterestId, airportId
|
|
});
|
|
|
|
const googleMapsCustomMarkerObject = (marker, overrideAnchorPoint = undefined) => {
|
|
const anchorPoints = {
|
|
airport: "CENTER",
|
|
geocode: "BOTTOM_CENTER",
|
|
poi: "BOTTOM_CENTER",
|
|
mapPoint: "BOTTOM_CENTER",
|
|
}
|
|
|
|
return {
|
|
anchorPoint: overrideAnchorPoint || anchorPoints[marker.type],
|
|
position: {lat: marker.gpsLatitude, lng: marker.gpsLongitude}
|
|
}
|
|
}
|
|
|
|
const googleMapsMarkerObject = (marker) => {
|
|
return {
|
|
label: marker.name,
|
|
draggable: editableNameTypes.includes(marker.type),
|
|
position: {lat: marker.gpsLatitude, lng: marker.gpsLongitude}
|
|
}
|
|
}
|
|
|
|
const infoWindowObject = ({active, position, marker, poi, airport, reportingPoint, airspaces, isInTrack}) => {
|
|
if (!position) position = {};
|
|
if (!position.lat) position.lat = 0;
|
|
if (!position.lng) position.lng = 0;
|
|
|
|
let title = null;
|
|
|
|
if (airport) {
|
|
if (!reportingPoint) {
|
|
title = `${airport.icaoCode} (${airport.name})`;
|
|
position.lat = airport.gpsLatitude;
|
|
position.lng = airport.gpsLongitude;
|
|
} else {
|
|
title = `${airport.icaoCode}:${reportingPoint.name}`
|
|
position.lat = reportingPoint.gpsLatitude;
|
|
position.lng = reportingPoint.gpsLongitude;
|
|
}
|
|
}
|
|
|
|
if (marker) {
|
|
title = marker.name;
|
|
position.lat = marker.gpsLatitude;
|
|
position.lng = marker.gpsLongitude;
|
|
}
|
|
|
|
if (poi) {
|
|
title = poi.name;
|
|
position.lat = poi.gpsLatitude;
|
|
position.lng = poi.gpsLongitude;
|
|
}
|
|
|
|
return {title, active, position, airspaces, poi, marker, airport, reportingPoint, isInTrack};
|
|
};
|
|
|
|
return {markerObject, googleMapsMarkerObject, googleMapsCustomMarkerObject, infoWindowObject, editableNameTypes}
|
|
}
|