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 4 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
74 changes: 7 additions & 67 deletions src/components/details.page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import Image from 'next/image';
import { Attachement } from '@/api/settings';
import { LatLngTuple } from 'leaflet';
import { useTranslations } from 'next-intl';

import { convertAttachementsToImages } from '@/lib/utils';
import { Badge } from '@/components/ui/badge';
import { Icons, propsForSVGPresentation } from '@/components/icons';

import ButtonCenterView from './button-center-view';
import ButtonClose from './button-close';
import Carousel from './carousel';
import MeterLength from './length';
import { MetadataList } from './metadata-list';

type Props = {
content: {
Expand All @@ -35,7 +31,6 @@ type Props = {
};

export default function DetailsPageUI({ content }: Props) {
const t = useTranslations('details');
return (
<article>
{content.attachments.length > 0 && (
Expand All @@ -62,67 +57,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
86 changes: 43 additions & 43 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,19 @@ export const GeometryItem = ({
);
}

const hasDetails = layer.type !== undefined && layer.url && properties?.id;
const pointerClassName = hasDetails ? '' : '!cursor-[unset]';

const featureEventHandler = {
click: () => {
if (hasDetails) {
router.push(
`/map/${layer?.type}/${properties?.id}?${params.toString()}`,
);
}
},
};
Bo-Duke marked this conversation as resolved.
Show resolved Hide resolved

if (geometry.type === 'Point' || geometry.type === 'MultiPoint') {
const coordinatesAsMultiPoint =
geometry.type === 'Point' ? [geometry.coordinates] : geometry.coordinates;
Expand All @@ -90,8 +73,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 +113,14 @@ export const GeometryItem = ({
weight: 10,
opacity: 0,
}}
className={layer.type}
eventHandlers={{
mouseover: e => e.target.setStyle({ opacity: 0.5 }),
mouseout: e => e.target.setStyle({ opacity: 0 }),
...featureEventHandler,
}}
className={`streams-hover ${pointerClassName}`}
Bo-Duke marked this conversation as resolved.
Show resolved Hide resolved
>
<MetaData properties={properties} layer={layer} />
<GeometryTooltip properties={properties} layer={layer} />
</Polyline>
</Fragment>
);
Expand All @@ -140,9 +129,10 @@ export const GeometryItem = ({
<Polyline
positions={group.map(([lat, lng]) => [lng, lat])}
pathOptions={options.style as GeoJSONOptions}
className={layer.type}
className={`${layer.type} ${pointerClassName}`}
eventHandlers={featureEventHandler}
>
<MetaData properties={properties} layer={layer} />
<GeometryTooltip properties={properties} layer={layer} />
</Polyline>
);
})}
Expand All @@ -164,9 +154,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] ${pointerClassName}`}
pane="tilePane"
>
<MetaData properties={properties} layer={layer} />
<GeometryTooltip properties={properties} layer={layer} />
</Polygon>
))}
</>
Expand Down
58 changes: 58 additions & 0 deletions src/components/map/geometry-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Image from 'next/image';
import { Layer } from '@/api/settings';
import { GeoJsonProperties } from 'geojson';
import { Tooltip } from 'react-leaflet';

import { MetadataList } from '../metadata-list';

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-64 !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}
small
/>
{properties.description && (
<span
className="mb-1 line-clamp-2"
dangerouslySetInnerHTML={{ __html: properties.description }}
></span>
)}
</div>
</div>
</Tooltip>
);
};
60 changes: 0 additions & 60 deletions src/components/map/popup.tsx

This file was deleted.

Loading
Loading