|
| 1 | +import { Button } from "@components/UI/Button.tsx"; |
| 2 | +import { Input } from "@components/UI/Input.tsx"; |
| 3 | +import { BaseMap } from "@components/Map.tsx"; |
| 4 | +import { Marker } from "react-map-gl/maplibre"; |
| 5 | +import { MapPin } from "lucide-react"; |
| 6 | +import { useCallback, useState } from "react"; |
| 7 | +import { useTranslation } from "react-i18next"; |
| 8 | +import { create } from "@bufbuild/protobuf"; |
| 9 | +import { Protobuf } from "@meshtastic/core"; |
| 10 | +import { useToast } from "@core/hooks/useToast.ts"; |
| 11 | + |
| 12 | +interface FixedPositionPickerProps { |
| 13 | + currentPosition?: { |
| 14 | + latitudeI?: number; |
| 15 | + longitudeI?: number; |
| 16 | + altitude?: number; |
| 17 | + }; |
| 18 | + isEnabled: boolean; |
| 19 | + onSetPosition: (message: Protobuf.Admin.AdminMessage) => void; |
| 20 | + onRequestUpdate: (message: Protobuf.Admin.AdminMessage) => void; |
| 21 | +} |
| 22 | + |
| 23 | +export const FixedPositionPicker = ({ |
| 24 | + currentPosition, |
| 25 | + isEnabled, |
| 26 | + onSetPosition, |
| 27 | + onRequestUpdate, |
| 28 | +}: FixedPositionPickerProps) => { |
| 29 | + const { t } = useTranslation("config"); |
| 30 | + const { toast } = useToast(); |
| 31 | + |
| 32 | + // State for fixed position inputs (in degrees, not integer format) |
| 33 | + const [latitude, setLatitude] = useState<string>( |
| 34 | + currentPosition?.latitudeI ? String(currentPosition.latitudeI / 1e7) : "" |
| 35 | + ); |
| 36 | + const [longitude, setLongitude] = useState<string>( |
| 37 | + currentPosition?.longitudeI ? String(currentPosition.longitudeI / 1e7) : "" |
| 38 | + ); |
| 39 | + const [altitude, setAltitude] = useState<string>( |
| 40 | + currentPosition?.altitude ? String(currentPosition.altitude) : "" |
| 41 | + ); |
| 42 | + |
| 43 | + const handleMapClick = useCallback((event: any) => { |
| 44 | + const { lng, lat } = event.lngLat; |
| 45 | + setLatitude(lat.toFixed(7)); |
| 46 | + setLongitude(lng.toFixed(7)); |
| 47 | + }, []); |
| 48 | + |
| 49 | + const handleRequestPosition = useCallback(() => { |
| 50 | + const message = create(Protobuf.Admin.AdminMessageSchema, { |
| 51 | + payloadVariant: { |
| 52 | + case: "getOwnerRequest", |
| 53 | + value: true, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + onRequestUpdate(message); |
| 58 | + |
| 59 | + toast({ |
| 60 | + title: t("position.fixedPosition.requestSent.title"), |
| 61 | + description: t("position.fixedPosition.requestSent.description"), |
| 62 | + }); |
| 63 | + }, [onRequestUpdate, toast, t]); |
| 64 | + |
| 65 | + const handleSetFixedPosition = useCallback(() => { |
| 66 | + const lat = parseFloat(latitude); |
| 67 | + const lon = parseFloat(longitude); |
| 68 | + const alt = parseInt(altitude); |
| 69 | + |
| 70 | + if (isNaN(lat) || isNaN(lon)) { |
| 71 | + toast({ |
| 72 | + title: t("position.fixedPosition.error.title"), |
| 73 | + description: t("position.fixedPosition.error.invalidCoordinates"), |
| 74 | + variant: "destructive", |
| 75 | + }); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Check if fixedPosition is enabled in config |
| 80 | + if (!isEnabled) { |
| 81 | + toast({ |
| 82 | + title: t("position.fixedPosition.error.title"), |
| 83 | + description: t("position.fixedPosition.error.notEnabled"), |
| 84 | + variant: "destructive", |
| 85 | + }); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Convert degrees to integer format (multiply by 1e7) |
| 90 | + const latitudeI = Math.round(lat * 1e7); |
| 91 | + const longitudeI = Math.round(lon * 1e7); |
| 92 | + |
| 93 | + console.log("Setting fixed position:", { |
| 94 | + latitude: lat, |
| 95 | + longitude: lon, |
| 96 | + altitude: isNaN(alt) ? 0 : alt, |
| 97 | + latitudeI, |
| 98 | + longitudeI, |
| 99 | + }); |
| 100 | + |
| 101 | + const message = create(Protobuf.Admin.AdminMessageSchema, { |
| 102 | + payloadVariant: { |
| 103 | + case: "setFixedPosition", |
| 104 | + value: create(Protobuf.Mesh.PositionSchema, { |
| 105 | + latitudeI, |
| 106 | + longitudeI, |
| 107 | + altitude: isNaN(alt) ? 0 : alt, |
| 108 | + time: Math.floor(Date.now() / 1000), |
| 109 | + }), |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + onSetPosition(message); |
| 114 | + |
| 115 | + toast({ |
| 116 | + title: t("position.fixedPosition.success.title"), |
| 117 | + description: t("position.fixedPosition.success.description", { |
| 118 | + lat: lat.toFixed(6), |
| 119 | + lon: lon.toFixed(6), |
| 120 | + }), |
| 121 | + }); |
| 122 | + }, [latitude, longitude, altitude, isEnabled, onSetPosition, toast, t]); |
| 123 | + |
| 124 | + return ( |
| 125 | + <div className="mt-4 space-y-3 p-4 border rounded-md bg-muted/50"> |
| 126 | + {/* Map Picker */} |
| 127 | + <div className="w-full h-64 rounded-md overflow-hidden border"> |
| 128 | + <BaseMap |
| 129 | + onClick={handleMapClick} |
| 130 | + initialViewState={{ |
| 131 | + latitude: latitude |
| 132 | + ? parseFloat(latitude) |
| 133 | + : currentPosition?.latitudeI |
| 134 | + ? currentPosition.latitudeI / 1e7 |
| 135 | + : 0, |
| 136 | + longitude: longitude |
| 137 | + ? parseFloat(longitude) |
| 138 | + : currentPosition?.longitudeI |
| 139 | + ? currentPosition.longitudeI / 1e7 |
| 140 | + : 0, |
| 141 | + zoom: (latitude && longitude) || currentPosition?.latitudeI ? 13 : 1.8, |
| 142 | + }} |
| 143 | + > |
| 144 | + {latitude && longitude && ( |
| 145 | + <> |
| 146 | + <Marker |
| 147 | + longitude={parseFloat(longitude)} |
| 148 | + latitude={parseFloat(latitude)} |
| 149 | + anchor="bottom" |
| 150 | + > |
| 151 | + <MapPin className="w-8 h-8 text-red-500 fill-red-500/20" /> |
| 152 | + </Marker> |
| 153 | + </> |
| 154 | + )} |
| 155 | + </BaseMap> |
| 156 | + </div> |
| 157 | + <p className="text-xs text-muted-foreground text-center"> |
| 158 | + {t("position.fixedPosition.map.hint")} |
| 159 | + </p> |
| 160 | + |
| 161 | + <div className="grid grid-cols-1 gap-3 sm:grid-cols-3"> |
| 162 | + <div className="space-y-1.5"> |
| 163 | + <label htmlFor="latitude" className="text-xs font-medium"> |
| 164 | + {t("position.fixedPosition.latitude.label")} |
| 165 | + </label> |
| 166 | + <Input |
| 167 | + id="latitude" |
| 168 | + type="number" |
| 169 | + step="any" |
| 170 | + placeholder="37.7749" |
| 171 | + value={latitude} |
| 172 | + onChange={(e) => setLatitude(e.target.value)} |
| 173 | + className="h-8 text-sm" |
| 174 | + /> |
| 175 | + </div> |
| 176 | + |
| 177 | + <div className="space-y-1.5"> |
| 178 | + <label htmlFor="longitude" className="text-xs font-medium"> |
| 179 | + {t("position.fixedPosition.longitude.label")} |
| 180 | + </label> |
| 181 | + <Input |
| 182 | + id="longitude" |
| 183 | + type="number" |
| 184 | + step="any" |
| 185 | + placeholder="-122.4194" |
| 186 | + value={longitude} |
| 187 | + onChange={(e) => setLongitude(e.target.value)} |
| 188 | + className="h-8 text-sm" |
| 189 | + /> |
| 190 | + </div> |
| 191 | + |
| 192 | + <div className="space-y-1.5"> |
| 193 | + <label htmlFor="altitude" className="text-xs font-medium"> |
| 194 | + {t("position.fixedPosition.altitude.label")} |
| 195 | + </label> |
| 196 | + <Input |
| 197 | + id="altitude" |
| 198 | + type="number" |
| 199 | + placeholder="100" |
| 200 | + value={altitude} |
| 201 | + onChange={(e) => setAltitude(e.target.value)} |
| 202 | + className="h-8 text-sm" |
| 203 | + /> |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + |
| 207 | + <div className="flex gap-2 flex-wrap"> |
| 208 | + <Button |
| 209 | + onClick={handleSetFixedPosition} |
| 210 | + variant="outline" |
| 211 | + size="sm" |
| 212 | + className="flex-1 sm:flex-none" |
| 213 | + > |
| 214 | + {t("position.fixedPosition.setButton")} |
| 215 | + </Button> |
| 216 | + <Button |
| 217 | + onClick={handleRequestPosition} |
| 218 | + variant="subtle" |
| 219 | + size="sm" |
| 220 | + className="flex-1 sm:flex-none" |
| 221 | + > |
| 222 | + {t("position.fixedPosition.requestButton")} |
| 223 | + </Button> |
| 224 | + </div> |
| 225 | + </div> |
| 226 | + ); |
| 227 | +}; |
0 commit comments