Skip to content

Commit

Permalink
Improve error handling when loading Y.Maps (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kachkaev committed Jan 29, 2022
1 parent b5ac620 commit e571cee
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/components/inherited-map/components/map/map.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import * as Sentry from "@sentry/nextjs";
import { observer } from "mobx-react";
import * as React from "react";
import styled from "styled-components";

import { useStore } from "../../models/root-store";
import { StyledMap } from "./styles";
// import { debounce } from 'utils'

const ErrorMessage = styled.div`
position: absolute;
bottom: 20px;
font-size: 1.5rem;
left: 0;
right: 0;
text-align: center;
color: red;
`;

export const Map = observer(() => {
const { mapStore } = useStore();
// const boundsChangeHandler = React.useCallback( // TODO
Expand All @@ -22,6 +34,10 @@ export const Map = observer(() => {
[mapStore],
);
React.useEffect(() => {
if (!window.ymaps) {
return;
}

window.ymaps.ready(["Heatmap"]).then(() => {
const { center, zoom } = mapStore;
const map = new window.ymaps.Map(
Expand Down Expand Up @@ -67,5 +83,21 @@ export const Map = observer(() => {

const mapRef = React.useRef();

React.useEffect(() => {
if (!window.ymaps) {
Sentry.captureException(
new Error("Map was not rendered because window.ymaps is undefined"),
);
}
}, []);

if (!window.ymaps) {
return (
<ErrorMessage>
Яндекс.Карты не загрузились. Попробуйте обновить страницу.
</ErrorMessage>
);
}

return <StyledMap id="map" ref={mapRef} />;
});

0 comments on commit e571cee

Please sign in to comment.