|
| 1 | +import type { FunctionComponent } from 'preact'; |
| 2 | +import { useCallback } from 'preact/hooks'; |
| 3 | +import type { |
| 4 | + Feature, |
| 5 | + FeatureCollection, |
| 6 | + GeoJsonProperties, |
| 7 | + Geometry, |
| 8 | + Point, |
| 9 | +} from 'geojson'; |
| 10 | +import Icon from '../Icon'; |
| 11 | +import { selectGameConfig } from '../gameConfig'; |
| 12 | +import { useAppSelector } from '../store'; |
| 13 | + |
| 14 | +interface Props { |
| 15 | + results: FeatureCollection<Point>; |
| 16 | +} |
| 17 | + |
| 18 | +const makeRed = <T extends Geometry, P extends GeoJsonProperties>( |
| 19 | + f: Feature<T, P> |
| 20 | +) => ({ |
| 21 | + ...f, |
| 22 | + properties: { ...f.properties, title: 'Target', 'marker-color': '#f00' }, |
| 23 | +}); |
| 24 | + |
| 25 | +const makePlacements = <P extends GeoJsonProperties>( |
| 26 | + f: Feature<Point, P>, |
| 27 | + i: number |
| 28 | +): Feature<Point, P> => { |
| 29 | + if (i >= 9) { |
| 30 | + return f; |
| 31 | + } else { |
| 32 | + return { |
| 33 | + ...f, |
| 34 | + properties: { ...f.properties, 'marker-symbol': i + 1 }, |
| 35 | + }; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const MapLink: FunctionComponent<Props> = ({ results }) => { |
| 40 | + const gameConfig = useAppSelector(selectGameConfig); |
| 41 | + const target = gameConfig?.target; |
| 42 | + |
| 43 | + const onClick = useCallback(() => { |
| 44 | + const a = document.createElement('a'); |
| 45 | + let targets: Feature[]; |
| 46 | + if (!target) { |
| 47 | + targets = []; |
| 48 | + } else if (target.type === 'FeatureCollection') { |
| 49 | + targets = target.features; |
| 50 | + } else { |
| 51 | + targets = [target]; |
| 52 | + } |
| 53 | + const resultsWithTarget = { |
| 54 | + ...results, |
| 55 | + features: [ |
| 56 | + ...targets.map(makeRed), |
| 57 | + ...results.features.slice(0, 100).map(makePlacements), |
| 58 | + ], |
| 59 | + }; |
| 60 | + const url = |
| 61 | + 'http://geojson.io/#data=data:application/json,' + |
| 62 | + encodeURIComponent(JSON.stringify(resultsWithTarget)); |
| 63 | + window.open(url, '_blank'); |
| 64 | + }, [results, target]); |
| 65 | + |
| 66 | + return <Icon name="map-location-dot" onClick={onClick} label="View Map" />; |
| 67 | +}; |
| 68 | + |
| 69 | +export default MapLink; |
0 commit comments