Skip to content

Commit

Permalink
Use GeometryList component instead of GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrucs committed Aug 1, 2023
1 parent d4b4ece commit b1fa082
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 62 deletions.
64 changes: 5 additions & 59 deletions src/components/map/geojson.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,15 @@
import { useMemo } from 'react';
import { useSearchParams } from 'next/navigation';
import { Layer } from '@/api/settings';
import { useMapContext } from '@/context/map';
import { Feature } from 'geojson';
import L, { Icon, LatLng, Layer as LeafletLayer } from 'leaflet';
import { renderToStaticMarkup } from 'react-dom/server';
import { GeoJSON } from 'react-leaflet';

import { Icons } from '@/components/icons';
import { DefaultMarker } from '@/components/map/default-marker';
import Popup from '@/components/map/popup';
import { GeometryList } from '@/components/map/geometry-list';

type Props = {
layer: Layer;
};

export default function GeoJson({ layer }: Props) {
const searchParams = useSearchParams();
const { layers } = useMapContext();

const onEachFeaturehandler = (
feature: Feature,
layer: LeafletLayer,
type: string,
) => {
const { properties } = feature;

if (properties === null || !properties.name) {
return;
}

if (properties.name) {
layer.bindTooltip(properties.name);
}

if (layers === null || layers.length === 0) {
return;
}

if ('url' in (layers.find(item => item.type === type) || {})) {
layer.bindPopup(
renderToStaticMarkup(
<Popup
name={properties.name}
description={properties.description}
attachments={properties.attachments}
type={type}
id={properties.id}
params={searchParams}
/>,
),
{ offset: [4, -14] },
);
}
};

const pointToLayerHandler = (feature: Feature, latlng: LatLng) => {
const icon =
feature.properties?.type?.pictogram ??
renderToStaticMarkup(<Icons.info fill="white" />);
return L.marker(latlng, {
icon: DefaultMarker(icon, 1) as Icon,
});
};

const data = useMemo(() => {
const searchText = searchParams.get('text');
Expand All @@ -85,11 +32,10 @@ export default function GeoJson({ layer }: Props) {
}

return (
<GeoJSON
data={data}
style={layer.options.style}
onEachFeature={(...args) => onEachFeaturehandler(...args, layer.type)}
pointToLayer={pointToLayerHandler}
<GeometryList
contents={data.features}
layer={layer}
options={layer.options}
/>
);
}
5 changes: 2 additions & 3 deletions src/components/map/popup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URLSearchParams } from 'url';
import Image from 'next/image';
import Link from 'next/link';
import { useSearchParams } from 'next/navigation';

import {
Card,
Expand All @@ -12,7 +12,6 @@ import {

type Props = {
name: string;
params: URLSearchParams;
description: string;
attachments?: { thumbnail: string }[];
type: string;
Expand All @@ -21,12 +20,12 @@ type Props = {

export default function Popup({
attachments,
params,
name,
description,
type,
id,
}: Props) {
const params = useSearchParams();
return (
<Card className="group relative border-0">
{attachments?.[0]?.thumbnail && (
Expand Down

0 comments on commit b1fa082

Please sign in to comment.