Merged in monorepo-step-1 (pull request #1080)
Migrate to a monorepo setup - step 1 * Move web to subfolder /apps/scandic-web * Yarn + transitive deps - Move to yarn - design-system package removed for now since yarn doesn't support the parameter for token (ie project currently broken) - Add missing transitive dependencies as Yarn otherwise prevents these imports - VS Code doesn't pick up TS path aliases unless you open /apps/scandic-web instead of root (will be fixed with monorepo) * Pin framer-motion to temporarily fix typing issue https://github.com/adobe/react-spectrum/issues/7494 * Pin zod to avoid typ error There seems to have been a breaking change in the types returned by zod where error is now returned as undefined instead of missing in the type. We should just handle this but to avoid merge conflicts just pin the dependency for now. * Pin react-intl version Pin version of react-intl to avoid tiny type issue where formatMessage does not accept a generic any more. This will be fixed in a future commit, but to avoid merge conflicts just pin for now. * Pin typescript version Temporarily pin version as newer versions as stricter and results in a type error. Will be fixed in future commit after merge. * Setup workspaces * Add design-system as a monorepo package * Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN * Fix husky for monorepo setup * Update netlify.toml * Add lint script to root package.json * Add stub readme * Fix react-intl formatMessage types * Test netlify.toml in root * Remove root toml * Update netlify.toml publish path * Remove package-lock.json * Update build for branch/preview builds Approved-by: Linus Flood
This commit is contained in:
committed by
Linus Flood
parent
667cab6fb6
commit
80100e7631
53
apps/scandic-web/hooks/maps/use-map-viewport.ts
Normal file
53
apps/scandic-web/hooks/maps/use-map-viewport.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
// Hook to use supercluster with @visgl/react-google-map.
|
||||
// Implemented according to https://github.com/visgl/react-google-maps/tree/main/examples/custom-marker-clustering
|
||||
|
||||
import { useMap } from "@vis.gl/react-google-maps"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
import type { BBox } from "geojson"
|
||||
|
||||
type MapViewportOptions = {
|
||||
padding?: number
|
||||
}
|
||||
|
||||
export function useMapViewport({ padding = 0 }: MapViewportOptions = {}) {
|
||||
const map = useMap()
|
||||
const [bbox, setBbox] = useState<BBox>([-180, -90, 180, 90])
|
||||
const [zoom, setZoom] = useState(0)
|
||||
|
||||
// observe the map to get current bounds
|
||||
useEffect(() => {
|
||||
if (!map) return
|
||||
|
||||
const listener = map.addListener("bounds_changed", () => {
|
||||
const bounds = map.getBounds()
|
||||
const zoom = map.getZoom()
|
||||
const projection = map.getProjection()
|
||||
|
||||
if (!bounds || !zoom || !projection) return
|
||||
|
||||
const sw = bounds.getSouthWest()
|
||||
const ne = bounds.getNorthEast()
|
||||
|
||||
const paddingDegrees = degreesPerPixel(zoom) * padding
|
||||
|
||||
const n = Math.min(90, ne.lat() + paddingDegrees)
|
||||
const s = Math.max(-90, sw.lat() - paddingDegrees)
|
||||
|
||||
const w = sw.lng() - paddingDegrees
|
||||
const e = ne.lng() + paddingDegrees
|
||||
|
||||
setBbox([w, s, e, n])
|
||||
setZoom(zoom)
|
||||
})
|
||||
|
||||
return () => listener.remove()
|
||||
}, [map, padding])
|
||||
|
||||
return { bbox, zoom }
|
||||
}
|
||||
|
||||
function degreesPerPixel(zoomLevel: number) {
|
||||
// 360° divided by the number of pixels at the zoom-level
|
||||
return 360 / (Math.pow(2, zoomLevel) * 256)
|
||||
}
|
||||
42
apps/scandic-web/hooks/maps/use-supercluster.ts
Normal file
42
apps/scandic-web/hooks/maps/use-supercluster.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { useEffect, useMemo, useReducer } from "react"
|
||||
import Supercluster, {type ClusterProperties } from "supercluster"
|
||||
|
||||
import { useMapViewport } from "./use-map-viewport"
|
||||
|
||||
import type { FeatureCollection, GeoJsonProperties, Point } from "geojson"
|
||||
|
||||
export function useSupercluster<T extends GeoJsonProperties>(
|
||||
geojson: FeatureCollection<Point, T>,
|
||||
superclusterOptions: Supercluster.Options<T, ClusterProperties>
|
||||
) {
|
||||
// create the clusterer and keep it
|
||||
const clusterer = useMemo(() => {
|
||||
return new Supercluster(superclusterOptions)
|
||||
}, [superclusterOptions])
|
||||
|
||||
// version-number for the data loaded into the clusterer
|
||||
// (this is needed to trigger updating the clusters when data was changed)
|
||||
const [version, dataWasUpdated] = useReducer((x: number) => x + 1, 0)
|
||||
|
||||
// when data changes, load it into the clusterer
|
||||
useEffect(() => {
|
||||
clusterer.load(geojson.features)
|
||||
dataWasUpdated()
|
||||
}, [clusterer, geojson])
|
||||
|
||||
// get bounding-box and zoomlevel from the map
|
||||
const { bbox, zoom } = useMapViewport({ padding: 100 })
|
||||
|
||||
// retrieve the clusters within the current viewport
|
||||
const clusters = useMemo(() => {
|
||||
// don't try to read clusters before data was loaded into the clusterer (version===0),
|
||||
// otherwise getClusters will crash
|
||||
if (!clusterer || version === 0) return []
|
||||
|
||||
return clusterer.getClusters(bbox, zoom)
|
||||
}, [version, clusterer, bbox, zoom])
|
||||
|
||||
return {
|
||||
clusters,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user