2025-09-03 23:17:35 +03:00
|
|
|
import Map from "./map";
|
|
|
|
|
import "./index.css";
|
2025-09-03 23:35:29 +03:00
|
|
|
import { ViewModeProps } from "../interface";
|
2025-09-04 14:26:29 +03:00
|
|
|
import { useNoteLabel, useSpacedUpdate } from "../../react/hooks";
|
2025-09-03 23:35:29 +03:00
|
|
|
import { DEFAULT_MAP_LAYER_NAME } from "./map_layer";
|
2025-09-03 23:57:38 +03:00
|
|
|
import { LatLng } from "leaflet";
|
2025-09-04 15:58:50 +03:00
|
|
|
import { useEffect, useState } from "preact/hooks";
|
|
|
|
|
import Marker from "./marker";
|
2025-09-04 15:47:56 +03:00
|
|
|
import froca from "../../../services/froca";
|
2025-09-04 15:58:50 +03:00
|
|
|
import FNote from "../../../entities/fnote";
|
2025-09-03 23:17:35 +03:00
|
|
|
|
|
|
|
|
const DEFAULT_COORDINATES: [number, number] = [3.878638227135724, 446.6630455551659];
|
|
|
|
|
const DEFAULT_ZOOM = 2;
|
2025-09-04 15:47:56 +03:00
|
|
|
export const LOCATION_ATTRIBUTE = "geolocation";
|
2025-09-03 23:17:35 +03:00
|
|
|
|
2025-09-03 23:57:38 +03:00
|
|
|
interface MapData {
|
|
|
|
|
view?: {
|
|
|
|
|
center?: LatLng | [number, number];
|
|
|
|
|
zoom?: number;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 15:47:56 +03:00
|
|
|
export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewModeProps<MapData>) {
|
2025-09-03 23:35:29 +03:00
|
|
|
const [ layerName ] = useNoteLabel(note, "map:style");
|
2025-09-04 15:58:50 +03:00
|
|
|
const [ notes, setNotes ] = useState<FNote[]>([]);
|
2025-09-04 14:26:29 +03:00
|
|
|
const spacedUpdate = useSpacedUpdate(() => {
|
2025-09-04 15:13:48 +03:00
|
|
|
if (viewConfig) {
|
|
|
|
|
saveConfig(viewConfig);
|
|
|
|
|
}
|
2025-09-04 14:26:29 +03:00
|
|
|
}, 5000);
|
|
|
|
|
|
2025-09-04 15:58:50 +03:00
|
|
|
useEffect(() => { froca.getNotes(noteIds).then(setNotes) }, [ noteIds ]);
|
2025-09-04 15:47:56 +03:00
|
|
|
|
2025-09-03 23:17:35 +03:00
|
|
|
return (
|
|
|
|
|
<div className="geo-view">
|
|
|
|
|
<Map
|
2025-09-04 15:13:48 +03:00
|
|
|
coordinates={viewConfig?.view?.center ?? DEFAULT_COORDINATES}
|
|
|
|
|
zoom={viewConfig?.view?.zoom ?? DEFAULT_ZOOM}
|
2025-09-03 23:35:29 +03:00
|
|
|
layerName={layerName ?? DEFAULT_MAP_LAYER_NAME}
|
2025-09-04 14:26:29 +03:00
|
|
|
viewportChanged={(coordinates, zoom) => {
|
2025-09-04 15:13:48 +03:00
|
|
|
if (!viewConfig) viewConfig = {};
|
|
|
|
|
viewConfig.view = { center: coordinates, zoom };
|
2025-09-04 14:26:29 +03:00
|
|
|
spacedUpdate.scheduleUpdate();
|
|
|
|
|
}}
|
2025-09-04 15:47:56 +03:00
|
|
|
>
|
2025-09-04 15:58:50 +03:00
|
|
|
{notes.map(note => <NoteMarker note={note} />)}
|
2025-09-04 15:47:56 +03:00
|
|
|
</Map>
|
2025-09-03 23:17:35 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-04 15:58:50 +03:00
|
|
|
|
|
|
|
|
function NoteMarker({ note }: { note: FNote }) {
|
|
|
|
|
const [ location ] = useNoteLabel(note, LOCATION_ATTRIBUTE);
|
|
|
|
|
const latLng = location?.split(",", 2).map((el) => parseFloat(el)) as [ number, number ] | undefined;
|
|
|
|
|
|
|
|
|
|
return latLng && <Marker coordinates={latLng} />
|
|
|
|
|
}
|