Skip to content

Commit

Permalink
Type clusterRef (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bettelstab committed Jan 31, 2025
1 parent 4b444b6 commit a27d568
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
10 changes: 3 additions & 7 deletions src/Components/Map/UtopiaMapInner.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { LatLng } from 'leaflet'
import {
Children,
Expand Down Expand Up @@ -47,6 +42,7 @@ import { SelectPosition } from './Subcomponents/SelectPosition'
import type { ItemFormPopupProps } from '#types/ItemFormPopupProps'
import type { UtopiaMapProps } from '#types/UtopiaMapProps'
import type { Feature, Geometry as GeoJSONGeometry } from 'geojson'
import type { MutableRefObject, SetStateAction } from 'react'

const mapDivRef = createRef()

Expand Down Expand Up @@ -154,7 +150,7 @@ export function UtopiaMapInner({
url='https://tile.osmand.net/hd/{z}/{x}/{y}.png'
/>
<MarkerClusterGroup
ref={(r) => setClusterRef(r)}
ref={(r: SetStateAction<MutableRefObject<typeof MarkerClusterGroup>>) => setClusterRef(r)}
showCoverageOnHover
chunkedLoading
maxClusterRadius={50}
Expand All @@ -164,7 +160,7 @@ export function UtopiaMapInner({
isValidElement<{
setItemFormPopup: React.Dispatch<React.SetStateAction<ItemFormPopupProps>>
itemFormPopup: ItemFormPopupProps | null
clusterRef: React.MutableRefObject<undefined>
clusterRef: React.MutableRefObject<typeof MarkerClusterGroup>
}>(child)
? cloneElement(child, { setItemFormPopup, itemFormPopup, clusterRef })

Check failure on line 165 in src/Components/Map/UtopiaMapInner.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an `any` value
: child,
Expand Down
19 changes: 11 additions & 8 deletions src/Components/Map/hooks/useClusterRef.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
/* eslint-disable react/prop-types */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { createContext, useContext, useState } from 'react'

import type MarkerClusterGroup from 'react-leaflet-cluster'

type UseClusterRefManagerResult = ReturnType<typeof useClusterRefManager>

type ClusterRef = React.MutableRefObject<typeof MarkerClusterGroup>

const ClusterRefContext = createContext<UseClusterRefManagerResult>({
clusterRef: {} as React.MutableRefObject<undefined>,
clusterRef: {} as typeof MarkerClusterGroup,
setClusterRef: () => {},
})

function useClusterRefManager(): {
clusterRef: any
setClusterRef: React.Dispatch<React.SetStateAction<React.MutableRefObject<undefined>>>
clusterRef: typeof MarkerClusterGroup
setClusterRef: React.Dispatch<
React.SetStateAction<React.MutableRefObject<typeof MarkerClusterGroup>>
>
} {
const [clusterRef, setClusterRef] = useState<React.MutableRefObject<undefined>>(
{} as React.MutableRefObject<undefined>,
)
const [clusterRef, setClusterRef] = useState<ClusterRef>({} as React.MutableRefObject<undefined>)

return { clusterRef, setClusterRef }
}
Expand All @@ -28,7 +31,7 @@ export const ClusterRefProvider: React.FunctionComponent<{
<ClusterRefContext.Provider value={useClusterRefManager()}>{children}</ClusterRefContext.Provider>
)

export const useClusterRef = (): any => {
export const useClusterRef = (): typeof MarkerClusterGroup => {
const { clusterRef } = useContext(ClusterRefContext)
return clusterRef
}
Expand Down
4 changes: 2 additions & 2 deletions types/LayerProps.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Item } from './Item'
import type { ItemFormPopupProps } from './ItemFormPopupProps'
import type { ItemsApi } from './ItemsApi'
import type { ItemType } from './ItemType'
import type MarkerClusterGroup from 'react-leaflet-cluster'

export interface LayerProps {
id?: string
Expand Down Expand Up @@ -37,6 +38,5 @@ export interface LayerProps {
item_presets?: Record<string, unknown>
setItemFormPopup?: React.Dispatch<React.SetStateAction<ItemFormPopupProps | null>>
itemFormPopup?: ItemFormPopupProps | null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
clusterRef?: any
clusterRef?: React.MutableRefObject<typeof MarkerClusterGroup>[]
}

0 comments on commit a27d568

Please sign in to comment.