import React, { useEffect } from "react"; import { FormControl, Select, MenuItem, SelectChangeEvent, Typography, Box, } from "@mui/material"; import { observer } from "mobx-react-lite"; import { authStore, cityStore, selectedCityStore, type City } from "@shared"; import { MapPin } from "lucide-react"; export const CitySelector: React.FC = observer(() => { const { selectedCity, setSelectedCity, isLocked } = selectedCityStore; const canLoadAllCities = authStore.isAdmin && authStore.canRead("cities"); useEffect(() => { if (canLoadAllCities) { cityStore.getCities("ru"); return; } authStore.fetchMeCities().catch(() => undefined); }, [canLoadAllCities]); const baseCities: City[] = canLoadAllCities ? cityStore.cities["ru"].data : authStore.meCities["ru"].map((uc) => ({ id: uc.city_id, name: uc.name, country: "", country_code: "", arms: "", })); const currentCities: City[] = selectedCity?.id ? (() => { const exists = baseCities.some((city) => city.id === selectedCity.id); if (exists) { return baseCities; } return [selectedCity, ...baseCities]; })() : baseCities; const handleCityChange = (event: SelectChangeEvent) => { const cityId = event.target.value; if (cityId === "") { setSelectedCity(null); return; } const city = currentCities.find((c) => c.id === Number(cityId)); if (city) { setSelectedCity(city); } }; return ( ); });