forked from aws-samples/amazon-location-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
91 lines (79 loc) · 2.49 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import { ICredentials } from "@aws-amplify/core";
import { Geo, AmazonLocationServiceMapStyle } from "@aws-amplify/geo";
import Amplify, { Auth } from "aws-amplify";
import { AmplifyMapLibreRequest } from "maplibre-gl-js-amplify";
import { StrictMode, useEffect, useState } from "react";
import ReactDOM from "react-dom";
import ReactMapGL, {
MapRequest,
NavigationControl,
ViewportProps,
} from "react-map-gl";
import awsconfig from "./aws-exports";
import "maplibre-gl/dist/maplibre-gl.css";
import "./index.css";
import { TransformRequestFunction } from "maplibre-gl";
// initialize Amplify (auth, etc.)
Amplify.configure(awsconfig);
const App = () => {
const [credentials, setCredentials] = useState<ICredentials>();
const [transformRequest, setRequestTransformer] =
useState<TransformRequestFunction>();
const [viewport, setViewport] = useState<Partial<ViewportProps>>({
longitude: -123.1187,
latitude: 49.2819,
zoom: 10,
});
useEffect(() => {
// fetch AWS credentials from Amazon Cognito using Amplify Auth
const fetchCredentials = async () => {
setCredentials(await Auth.currentUserCredentials());
};
fetchCredentials();
}, []);
// create a new transformRequest function whenever the credentials change
useEffect(() => {
if (credentials != null) {
const { transformRequest } = new AmplifyMapLibreRequest(
credentials,
(Geo.getDefaultMap() as AmazonLocationServiceMapStyle).region
);
// wrap the new value in an anonymous function to prevent React from recognizing it as a
// function and immediately calling it
setRequestTransformer(() => transformRequest);
}
}, [credentials]);
return (
<div>
{transformRequest ? (
<ReactMapGL
{...viewport}
width="100%"
height="100vh"
transformRequest={
transformRequest as (
url?: string,
resourceType?: string
) => MapRequest
}
mapStyle={Geo.getDefaultMap().mapName}
onViewportChange={setViewport}
>
<div style={{ position: "absolute", left: 20, top: 20 }}>
<NavigationControl showCompass={false} />
</div>
</ReactMapGL>
) : (
<h1>Loading...</h1>
)}
</div>
);
};
ReactDOM.render(
<StrictMode>
<App />
</StrictMode>,
document.getElementById("root")
);