11import { Amplify , Auth } from 'aws-amplify' ;
22import maplibregl from 'maplibre-gl' ;
33import { AmplifyMapLibreRequest } from 'maplibre-gl-js-amplify' ;
4- import React , { useEffect , useMemo , useState } from 'react' ;
4+ import React , { forwardRef , useEffect , useMemo , useState } from 'react' ;
55import ReactMapGL from 'react-map-gl' ;
6- import type { MapProps , TransformRequestFunction } from 'react-map-gl' ;
6+ import type { MapProps , MapRef , TransformRequestFunction } from 'react-map-gl' ;
77
88export type MapViewProps = Omit < MapProps , 'transformRequest' > ;
99
@@ -21,66 +21,64 @@ export type MapViewProps = Omit<MapProps, 'transformRequest'>;
2121 * return <MapView />
2222 * }
2323 */
24- export const MapView = ( {
25- mapLib,
26- mapStyle,
27- style,
28- ...props
29- } : MapViewProps ) => {
30- const amplifyConfig = Amplify . configure ( ) as any ;
31- const geoConfig = useMemo (
32- ( ) =>
33- amplifyConfig . geo ?. amazon_location_service ??
34- amplifyConfig . geo ?. AmazonLocationService ??
35- { } ,
36- [ amplifyConfig ]
37- ) ;
38- const [ transformRequest , setTransformRequest ] = useState <
39- TransformRequestFunction | undefined
40- > ( ) ;
24+ export const MapView = forwardRef < MapRef , MapViewProps > (
25+ ( { mapLib, mapStyle, style, ...props } , ref ) => {
26+ const amplifyConfig = Amplify . configure ( ) as any ;
27+ const geoConfig = useMemo (
28+ ( ) =>
29+ amplifyConfig . geo ?. amazon_location_service ??
30+ amplifyConfig . geo ?. AmazonLocationService ??
31+ { } ,
32+ [ amplifyConfig ]
33+ ) ;
34+ const [ transformRequest , setTransformRequest ] = useState <
35+ TransformRequestFunction | undefined
36+ > ( ) ;
4137
42- const styleProps = useMemo < React . CSSProperties > (
43- ( ) => ( {
44- height : '100vh' ,
45- position : 'relative' ,
46- width : '100vw' ,
47- ...style ,
48- } ) ,
49- [ style ]
50- ) ;
38+ const styleProps = useMemo < React . CSSProperties > (
39+ ( ) => ( {
40+ height : '100vh' ,
41+ position : 'relative' ,
42+ width : '100vw' ,
43+ ...style ,
44+ } ) ,
45+ [ style ]
46+ ) ;
5147
52- /**
53- * The transformRequest is a callback used by react-map-gl before it makes a request for an external URL. It signs
54- * the request with AWS Sigv4 Auth, provided valid credentials, and is how we integrate react-map-gl with Amplify Geo
55- * and Amazon Location Service. Once the transformRequest is created, we render the map.
56- */
57- useEffect ( ( ) => {
58- ( async ( ) => {
59- const credentials = await Auth . currentUserCredentials ( ) ;
48+ /**
49+ * The transformRequest is a callback used by react-map-gl before it makes a request for an external URL. It signs
50+ * the request with AWS Sigv4 Auth, provided valid credentials, and is how we integrate react-map-gl with Amplify Geo
51+ * and Amazon Location Service. Once the transformRequest is created, we render the map.
52+ */
53+ useEffect ( ( ) => {
54+ ( async ( ) => {
55+ const credentials = await Auth . currentUserCredentials ( ) ;
6056
61- if ( credentials ) {
62- const { region } = geoConfig ;
63- const { transformRequest : amplifyTransformRequest } =
64- new AmplifyMapLibreRequest ( credentials , region ) ;
65- setTransformRequest ( ( ) => amplifyTransformRequest ) ;
66- }
67- } ) ( ) ;
68- } , [ geoConfig ] ) ;
57+ if ( credentials ) {
58+ const { region } = geoConfig ;
59+ const { transformRequest : amplifyTransformRequest } =
60+ new AmplifyMapLibreRequest ( credentials , region ) ;
61+ setTransformRequest ( ( ) => amplifyTransformRequest ) ;
62+ }
63+ } ) ( ) ;
64+ } , [ geoConfig ] ) ;
6965
70- /**
71- * The mapLib property is used by react-map-gl@v7 to override the underlying map library. The default library is
72- * mapbox-gl-js, which uses its own copyrighted license. We override the map library with the BSD-licensed
73- * maplibre-gl-js.
74- *
75- * The default mapStyle we use is just the map ID provided by aws-exports.
76- */
77- return transformRequest ? (
78- < ReactMapGL
79- { ...props }
80- mapLib = { mapLib ?? maplibregl }
81- mapStyle = { mapStyle ?? geoConfig . maps ?. default }
82- style = { styleProps }
83- transformRequest = { transformRequest }
84- />
85- ) : null ;
86- } ;
66+ /**
67+ * The mapLib property is used by react-map-gl@v7 to override the underlying map library. The default library is
68+ * mapbox-gl-js, which uses its own copyrighted license. We override the map library with the BSD-licensed
69+ * maplibre-gl-js.
70+ *
71+ * The default mapStyle we use is just the map ID provided by aws-exports.
72+ */
73+ return transformRequest ? (
74+ < ReactMapGL
75+ { ...props }
76+ mapLib = { mapLib ?? maplibregl }
77+ mapStyle = { mapStyle ?? geoConfig . maps ?. default }
78+ ref = { ref }
79+ style = { styleProps }
80+ transformRequest = { transformRequest }
81+ />
82+ ) : null ;
83+ }
84+ ) ;
0 commit comments