Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[web][photos] Add location search and coordinate input to edit location dialog

Anand Baburajand95118206fe4
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index d95af76d1f..a0d9aed7d8 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -806,5 +806,6 @@
"add_location": "Add location",
"add_location_button": "Add",
"save_location": "Save location",
- "tap_to_select_location": "Click on the map to select a location"
+ "tap_to_select_location": "Click on the map to select a location",
+ "search_location": "Search for a location"
}
diff --git a/web/packages/new/photos/components/EditLocationDialog.tsx b/web/packages/new/photos/components/EditLocationDialog.tsx
index 4f34a7c120..843480c1bc 100644
--- a/web/packages/new/photos/components/EditLocationDialog.tsx
+++ b/web/packages/new/photos/components/EditLocationDialog.tsx
@@ -1,13 +1,17 @@
import AddIcon from "@mui/icons-material/Add";
+import ArrowForwardIcon from "@mui/icons-material/ArrowForward";
import CloseIcon from "@mui/icons-material/Close";
import DoneIcon from "@mui/icons-material/Done";
import RemoveIcon from "@mui/icons-material/Remove";
+import SearchIcon from "@mui/icons-material/Search";
import {
Box,
Button,
CircularProgress,
+ ClickAwayListener,
Dialog,
IconButton,
+ InputBase,
Stack,
styled,
Typography,
@@ -234,6 +238,13 @@ interface EditableMapProps {
onLocationSelect: (location: Location) => void;
}
+interface NominatimResult {
+ place_id: number;
+ display_name: string;
+ lat: string;
+ lon: string;
+}
+
const EditableMap: React.FC<EditableMapProps> = ({
open,
initialLocation,
@@ -247,6 +258,119 @@ const EditableMap: React.FC<EditableMapProps> = ({
// Capture initial location at mount time to avoid map reset on file updates
const initialLocationRef = useRef(initialLocation);
+ // Search state
+ const [searchQuery, setSearchQuery] = useState("");
+ const [searchResults, setSearchResults] = useState<NominatimResult[]>([]);
+ const [showResults, setShowResults] = useState(false);
+ const [isSearching, setIsSearching] = useState(false);
+ const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+
+ // Manual coordinate input state
+ const [manualLat, setManualLat] = useState<string | null>(null);
+ const [manualLon, setManualLon] = useState<string | null>(null);
+
+ // Reset manual inputs when dialog opens or selectedLocation changes
+ useEffect(() => {
+ setManualLat(null);
+ setManualLon(null);
+ }, [open, selectedLocation]);
+
+ // Search using Nominatim API
+ const handleSearch = (query: string) => {
+ setSearchQuery(query);
+
+ if (searchTimeoutRef.current) {
+ clearTimeout(searchTimeoutRef.current);
+ }
+
+ if (query.trim().length < 3) {
+ setSearchResults([]);
+ setShowResults(false);
+ return;
+ }
+
+ searchTimeoutRef.current = setTimeout(async () => {
+ setIsSearching(true);
+ try {
+ const response = await fetch(
+ `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(query)}&limit=5`,
+ { headers: { "Accept-Language": "en" } },
+ );
+ const data = (await response.json()) as NominatimResult[];
+ setSearchResults(data);
+ setShowResults(data.length > 0);
+ } catch (error) {
+ console.error("Search failed:", error);
+ setSearchResults([]);
+ } finally {
+ setIsSearching(false);
+ }
+ }, 300);
+ };
+
+ const handleSelectResult = (result: NominatimResult) => {
+ const lat = parseFloat(result.lat);
+ const lng = parseFloat(result.lon);
+ onLocationSelect({ latitude: lat, longitude: lng });
+
+ // Update map view
+ if (mapRef.current) {
+ mapRef.current.setView([lat, lng], 12);
+
+ // Update or create marker
+ if (markerRef.current) {
+ markerRef.current.setLatLng([lat, lng]);
+ } else if (leaflet) {
+ const marker = leaflet
+ .marker([lat, lng], { draggable: true })
+ .addTo(mapRef.current);
+ markerRef.current = marker;
+
+ marker.on("dragend", () => {
+ const pos = marker.getLatLng();
+ onLocationSelect({ latitude: pos.lat, longitude: pos.lng });
+ });
+ }
+ }
+
+ setSearchQuery("");
+ setSearchResults([]);
+ setShowResults(false);
+ };
+
+ const handleManualCoordinates = () => {
+ const latStr = manualLat ?? selectedLocation?.latitude.toFixed(6) ?? "";
+ const lonStr =
+ manualLon ?? selectedLocation?.longitude.toFixed(6) ?? "";
+ const lat = parseFloat(latStr);
+ const lon = parseFloat(lonStr);
+ if (isNaN(lat) || isNaN(lon)) return;
+ if (lat < -90 || lat > 90 || lon < -180 || lon > 180) return;
+
+ onLocationSelect({ latitude: lat, longitude: lon });
+
+ if (mapRef.current) {
+ mapRef.current.setView([lat, lon], 12);
+
+ if (markerRef.current) {
+ markerRef.current.setLatLng([lat, lon]);
+ } else if (leaflet) {
+ const marker = leaflet
+ .marker([lat, lon], { draggable: true })
+ .addTo(mapRef.current);
+ markerRef.current = marker;
+
+ marker.on("dragend", () => {
+ const pos = marker.getLatLng();
+ onLocationSelect({ latitude: pos.lat, longitude: pos.lng });
+ });
+ }
+ }
+
+ setManualLat(null);
+ setManualLon(null);
+ };
+
const urlTemplate = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
const attribution =
'&copy; <a target="_blank" rel="noopener" href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
@@ -367,18 +491,93 @@ const EditableMap: React.FC<EditableMapProps> = ({
return (
<MapWrapper>
<MapContainer ref={mapContainerRef} />
- <MapOverlay>
- {selectedLocation ? (
- <Typography variant="body" sx={{ color: "text.muted" }}>
- {selectedLocation.latitude.toFixed(6)},{" "}
- {selectedLocation.longitude.toFixed(6)}
- </Typography>
- ) : (
- <Typography variant="body" sx={{ color: "text.muted" }}>
- {t("tap_to_select_location")}
- </Typography>
- )}
- </MapOverlay>
+ <SearchContainer>
+ <ClickAwayListener onClickAway={() => setShowResults(false)}>
+ <Box sx={{ position: "relative", width: "100%" }}>
+ <SearchInputWrapper>
+ <SearchIcon
+ sx={{ color: "text.muted", fontSize: 20 }}
+ />
+ <InputBase
+ placeholder={t("search_location")}
+ value={searchQuery}
+ onChange={(e) => handleSearch(e.target.value)}
+ onFocus={() =>
+ searchResults.length > 0 &&
+ setShowResults(true)
+ }
+ sx={{ ml: 1 }}
+ inputProps={{ style: { padding: 0 } }}
+ />
+ <CircularProgress
+ size={16}
+ sx={{ ml: 1, opacity: isSearching ? 1 : 0 }}
+ />
+ </SearchInputWrapper>
+ {showResults && (
+ <SearchResultsList>
+ {searchResults.map((result) => (
+ <SearchResultItem
+ key={result.place_id}
+ onClick={() =>
+ handleSelectResult(result)
+ }
+ >
+ <Typography
+ variant="body"
+ sx={{
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+ whiteSpace: "nowrap",
+ }}
+ >
+ {result.display_name}
+ </Typography>
+ </SearchResultItem>
+ ))}
+ </SearchResultsList>
+ )}
+ </Box>
+ </ClickAwayListener>
+ </SearchContainer>
+ <CoordinateControls>
+ <CoordinateInputWrapper>
+ <CoordinateInput
+ placeholder="Lat"
+ value={
+ manualLat ??
+ selectedLocation?.latitude.toFixed(6) ??
+ ""
+ }
+ onChange={(e) => setManualLat(e.target.value)}
+ type="number"
+ inputProps={{ step: "any" }}
+ />
+ </CoordinateInputWrapper>
+ <CoordinateInputWrapper>
+ <CoordinateInput
+ placeholder="Lon"
+ value={
+ manualLon ??
+ selectedLocation?.longitude.toFixed(6) ??
+ ""
+ }
+ onChange={(e) => setManualLon(e.target.value)}
+ type="number"
+ inputProps={{ step: "any" }}
+ />
+ </CoordinateInputWrapper>
+ <GoToLocationButton
+ onClick={handleManualCoordinates}
+ disabled={
+ manualLat === null &&
+ manualLon === null &&
+ !selectedLocation
+ }
+ >
+ <ArrowForwardIcon sx={{ fontSize: 16 }} />
+ </GoToLocationButton>
+ </CoordinateControls>
<ZoomControls>
<ZoomButton onClick={handleZoomIn} aria-label={t("zoom_in")}>
<AddIcon />
@@ -399,22 +598,57 @@ const MapWrapper = styled("div")({
const MapContainer = styled("div")({ height: "100%", width: "100%" });
-const MapOverlay = styled("div")(({ theme }) => ({
+const CoordinateControls = styled("div")(({ theme }) => ({
position: "absolute",
- top: theme.spacing(2),
- left: "50%",
- transform: "translateX(-50%)",
+ bottom: theme.spacing(2),
+ left: theme.spacing(2),
zIndex: 1000,
+ display: "flex",
+ alignItems: "center",
+ gap: theme.spacing(1),
+}));
+
+const CoordinateInputWrapper = styled("div")(({ theme }) => ({
+ display: "flex",
+ alignItems: "center",
backgroundColor: theme.vars.palette.background.paper,
- padding: theme.spacing(1, 2),
+ padding: theme.spacing(1, 1.5),
borderRadius: "9999px",
- boxShadow: theme.shadows[2],
+ boxShadow: theme.shadows[4],
+}));
+
+const CoordinateInput = styled(InputBase)(({ theme }) => ({
+ "& input": {
+ width: 82,
+ padding: 0,
+ fontSize: "0.875rem",
+ textAlign: "left",
+ "&::placeholder": { color: theme.vars.palette.text.muted, opacity: 1 },
+ "&::-webkit-outer-spin-button, &::-webkit-inner-spin-button": {
+ WebkitAppearance: "none",
+ margin: 0,
+ },
+ MozAppearance: "textfield",
+ },
+}));
+
+const GoToLocationButton = styled(IconButton)(({ theme }) => ({
+ backgroundColor: theme.vars.palette.text.base,
+ color: theme.vars.palette.background.default,
+ width: 36,
+ height: 36,
+ boxShadow: theme.shadows[4],
+ "&:hover": { backgroundColor: theme.vars.palette.text.muted },
+ "&.Mui-disabled": {
+ backgroundColor: theme.vars.palette.text.faint,
+ color: theme.vars.palette.background.default,
+ },
}));
const ZoomControls = styled(Stack)(({ theme }) => ({
position: "absolute",
right: theme.spacing(3),
- bottom: theme.spacing(6),
+ bottom: theme.spacing(5),
zIndex: 1000,
gap: theme.spacing(1),
}));
@@ -422,12 +656,56 @@ const ZoomControls = styled(Stack)(({ theme }) => ({
const ZoomButton = styled(IconButton)(({ theme }) => ({
backgroundColor: theme.vars.palette.background.paper,
boxShadow: theme.shadows[4],
- width: 48,
- height: 48,
- borderRadius: 16,
+ width: 32,
+ height: 32,
+ borderRadius: 12,
transition: "transform 0.2s ease-out",
"&:hover": {
backgroundColor: theme.vars.palette.background.paper,
transform: "scale(1.05)",
},
}));
+
+const SearchContainer = styled("div")(({ theme }) => ({
+ position: "absolute",
+ top: theme.spacing(2),
+ left: theme.spacing(2),
+ right: theme.spacing(2),
+ zIndex: 1001,
+ display: "flex",
+ justifyContent: "center",
+}));
+
+const SearchInputWrapper = styled("div")(({ theme }) => ({
+ display: "inline-flex",
+ alignItems: "center",
+ backgroundColor: theme.vars.palette.background.paper,
+ padding: theme.spacing(1),
+ paddingLeft: theme.spacing(1.5),
+ borderRadius: "9999px",
+ boxShadow: theme.shadows[4],
+}));
+
+const SearchResultsList = styled("div")(({ theme }) => ({
+ position: "absolute",
+ top: "100%",
+ left: 0,
+ right: 0,
+ maxWidth: 280,
+ marginTop: theme.spacing(1),
+ backgroundColor: theme.vars.palette.background.paper,
+ borderRadius: theme.spacing(1),
+ overflow: "hidden",
+ maxHeight: 250,
+ overflowY: "auto",
+ boxShadow: theme.shadows[4],
+}));
+
+const SearchResultItem = styled("div")(({ theme }) => ({
+ padding: theme.spacing(1.5, 2),
+ cursor: "pointer",
+ "&:hover": { backgroundColor: theme.vars.palette.action.hover },
+ "&:not(:last-child)": {
+ borderBottom: `1px solid ${theme.vars.palette.divider}`,
+ },
+}));