Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map interactions enhancements #59

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 91 additions & 62 deletions src/components/details.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,92 @@ type Props = {
};
};

export default function DetailsPageUI({ content }: Props) {
export type MetadataListProps = {
length?: number;
descent?: number;
flow?: string;
type?: {
label: string;
category: {
label: string;
};
pictogram?: string;
};
};

export const MetadataList = ({
length,
descent,
flow,
type,
}: MetadataListProps) => {
const t = useTranslations('details');
return (
<dl className="flex items-center gap-2 py-2">
dtrucs marked this conversation as resolved.
Show resolved Hide resolved
{length !== undefined && (
<>
<dt>
<Icons.chevronsLeftRight
className="text-primary"
{...propsForSVGPresentation}
/>
<span className="sr-only">{t('length')}</span>
</dt>
<dd className="mr-2">
<MeterLength length={length} />
</dd>
</>
)}
{descent !== undefined && (
<>
<dt>
<Icons.arrowDownRight
className="text-primary"
{...propsForSVGPresentation}
/>
<span className="sr-only">{t('descent')}</span>
</dt>
<dd className="mr-2">
<MeterLength length={descent} />
</dd>
</>
)}
{flow && (
<>
<dt>
<Icons.waves
className="text-primary"
{...propsForSVGPresentation}
/>
<span className="sr-only">{t('flow')}</span>
</dt>
<dd className="mr-2">{flow}</dd>
</>
)}
{type && (
<>
<dt>{type.category.label} :</dt>
<dd>
<Badge className="gap-2">
{type.pictogram && (
<Image
loading="lazy"
src={type.pictogram}
width={24}
height={24}
alt=""
/>
)}
<span>{type.label}</span>
</Badge>
</dd>
</>
)}
</dl>
);
};

export default function DetailsPageUI({ content }: Props) {
return (
<article>
{content.attachments.length > 0 && (
Expand All @@ -62,67 +146,12 @@ export default function DetailsPageUI({ content }: Props) {
)}
<ButtonClose />
</div>
<dl className="flex items-center gap-2 py-2">
{content.length !== undefined && (
<>
<dt>
<Icons.chevronsLeftRight
className="text-primary"
{...propsForSVGPresentation}
/>
<span className="sr-only">{t('length')}</span>
</dt>
<dd className="mr-2">
<MeterLength length={content.length} />
</dd>
</>
)}
{content.descent !== undefined && (
<>
<dt>
<Icons.arrowDownRight
className="text-primary"
{...propsForSVGPresentation}
/>
<span className="sr-only">{t('descent')}</span>
</dt>
<dd className="mr-2">
<MeterLength length={content.descent} />
</dd>
</>
)}
{content.flow && (
<>
<dt>
<Icons.waves
className="text-primary"
{...propsForSVGPresentation}
/>
<span className="sr-only">{t('flow')}</span>
</dt>
<dd className="mr-2">{content.flow}</dd>
</>
)}
{content.type && (
<>
<dt>{content.type.category.label} :</dt>
<dd>
<Badge className="gap-2">
{content.type.pictogram && (
<Image
loading="lazy"
src={content.type.pictogram}
width={24}
height={24}
alt=""
/>
)}
<span>{content.type.label}</span>
</Badge>
</dd>
</>
)}
</dl>
<MetadataList
length={content.length}
descent={content.descent}
flow={content.flow}
type={content.type}
/>
</header>

<div dangerouslySetInnerHTML={{ __html: content.description }} />
Expand Down
79 changes: 38 additions & 41 deletions src/components/map/geometry-item.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { Fragment } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { Layer } from '@/api/settings';
import { GeoJsonProperties, Geometry } from 'geojson';
import { GeoJSONOptions } from 'leaflet';
import { GeoJSONOptions, PathOptions } from 'leaflet';
import { renderToStaticMarkup } from 'react-dom/server';
import {
Popup as LeafletPopup,
Marker,
Polygon,
Polyline,
Tooltip,
} from 'react-leaflet';
import { Marker, Polygon, Polyline } from 'react-leaflet';

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

import { GeometryTooltip } from './geometry-tooltip';

type Props = {
geometry: Geometry;
Expand All @@ -23,41 +19,15 @@ type Props = {
options: GeoJSONOptions;
};

const MetaData = ({
properties,
layer,
}: {
properties: GeoJsonProperties;
layer: Layer;
}) => {
if (properties === null || (!properties.name && !properties.category)) {
return null;
}
return (
<>
<Tooltip>{properties.name ?? properties.category}</Tooltip>
{layer.type !== undefined && layer.url && properties.id && (
<LeafletPopup>
<Popup
name={properties.name ?? properties.category}
description={properties.description}
attachments={properties.attachments}
type={layer.type}
id={properties.id}
/>
</LeafletPopup>
)}
</>
);
};

export const GeometryItem = ({
geometry,
properties,
id,
layer,
options = { style: {} },
}: Props) => {
const params = useSearchParams();
const router = useRouter();
if (geometry.type === 'GeometryCollection') {
return (
<>
Expand All @@ -75,6 +45,16 @@ export const GeometryItem = ({
);
}

const featureEventHandler = {
click: () => {
if (layer.type !== undefined && layer.url && properties?.id) {
router.push(
`/map/${layer?.type}/${properties?.id}?${params.toString()}`,
);
}
},
};

if (geometry.type === 'Point' || geometry.type === 'MultiPoint') {
const coordinatesAsMultiPoint =
geometry.type === 'Point' ? [geometry.coordinates] : geometry.coordinates;
Expand All @@ -90,8 +70,9 @@ export const GeometryItem = ({
key={`point-${id}-${index}`}
position={[lng, lat]}
icon={DefaultMarker(icon, 1)}
eventHandlers={featureEventHandler}
>
<MetaData properties={properties} layer={layer} />
<GeometryTooltip properties={properties} layer={layer} />
</Marker>
);
})}
Expand Down Expand Up @@ -129,9 +110,14 @@ export const GeometryItem = ({
weight: 10,
opacity: 0,
}}
eventHandlers={{
mouseover: e => e.target.setStyle({ opacity: 0.5 }),
mouseout: e => e.target.setStyle({ opacity: 0 }),
...featureEventHandler,
}}
className={layer.type}
>
<MetaData properties={properties} layer={layer} />
<GeometryTooltip properties={properties} layer={layer} />
</Polyline>
</Fragment>
);
Expand All @@ -141,8 +127,9 @@ export const GeometryItem = ({
positions={group.map(([lat, lng]) => [lng, lat])}
pathOptions={options.style as GeoJSONOptions}
className={layer.type}
eventHandlers={featureEventHandler}
>
<MetaData properties={properties} layer={layer} />
<GeometryTooltip properties={properties} layer={layer} />
</Polyline>
);
})}
Expand All @@ -164,9 +151,19 @@ export const GeometryItem = ({
line.map<[number, number]>(([lat, lng]) => [lng, lat]),
)}
pathOptions={options.style as GeoJSONOptions}
eventHandlers={{
mouseover: e => e.target.setStyle({ fillOpacity: 0.8 }),
mouseout: e =>
e.target.setStyle({
fillOpacity:
(options.style as PathOptions)?.fillOpacity ?? 0.2,
}),
...featureEventHandler,
}}
className="transition-[fill-opacity]"
pane="tilePane"
>
<MetaData properties={properties} layer={layer} />
<GeometryTooltip properties={properties} layer={layer} />
</Polygon>
))}
</>
Expand Down
57 changes: 57 additions & 0 deletions src/components/map/geometry-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Image from 'next/image';
import { Layer } from '@/api/settings';
import { GeoJsonProperties } from 'geojson';
import { Tooltip } from 'react-leaflet';

import { MetadataList } from '../details.page';

export const GeometryTooltip = ({
properties,
layer,
}: {
properties: GeoJsonProperties;
layer: Layer;
}) => {
if (properties === null || (!properties.name && !properties.category)) {
return null;
}
if (layer.type === undefined || !layer.url || !properties.id) {
return <Tooltip>{properties.name ?? properties.category}</Tooltip>;
}
return (
<Tooltip
sticky
className="w-72 !overflow-hidden !whitespace-normal !rounded-xl !border-0 !p-0"
>
<div className="flex flex-col">
{properties.attachments?.[0]?.thumbnail && (
<Image
loading="lazy"
className="aspect-[4/3] h-auto w-auto object-cover transition-all group-hover:scale-105"
src={properties.attachments[0].thumbnail}
alt=""
width="400"
height="300"
/>
)}
<div className="p-4">
<h3 className="line-clamp-2 text-lg font-bold">
{properties.name ?? properties.category}
</h3>
<MetadataList
descent={properties.descent}
flow={properties.flow}
length={properties.length}
type={properties.type}
/>
{properties.description && (
<span
className="mb-1 line-clamp-2"
dangerouslySetInnerHTML={{ __html: properties.description }}
></span>
)}
</div>
</div>
</Tooltip>
);
};
Loading
Loading