Skip to content

Commit

Permalink
Hook fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
starsep committed Jun 30, 2024
1 parent 32420d8 commit ea7e2c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"start": "bunx --bun vite",
"build": "vite build",
"typecheck": "tsc -p ./tsconfig.json --noEmit",
"lint-fix": "biome check . --apply",
"lint-fix": "biome check . --write",
"check": "biome ci .",
"test": "bun test",
"css-build": "sass sass/mystyles.scss src/mystyles.css"
Expand Down
21 changes: 15 additions & 6 deletions src/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { osmAuth } from "osm-auth";
import React, { Suspense, useEffect, useMemo, useState } from "react";
import React, {
Suspense,
useCallback,
useEffect,
useMemo,
useState,
} from "react";
import { AppContext } from "~/appContext";
import CustomModal from "~/components/modal";
import WebGLMissingInfo from "~/components/webGLMissingInfo";
Expand Down Expand Up @@ -52,7 +58,7 @@ function Main() {
const [osmUsername, setOsmUsername] = useState("");
const [openChangesetId, setOpenChangesetId] = useState("");

const handleLogIn = () => {
const handleLogIn = useCallback(() => {
auth.authenticate(() => {
updateOsmUsernameState(auth, setOsmUsername);
if (modalState.type === ModalType.NeedToLogin) {
Expand All @@ -66,7 +72,7 @@ function Main() {
window.location.hash,
);
});
};
}, [auth, modalState.type]);

useEffect(() => {
if (
Expand All @@ -79,12 +85,15 @@ function Main() {
}
}, [handleLogIn]);

const handleLogOut = () => {
const handleLogOut = useCallback(() => {
auth.logout();
setOsmUsername("");
};
}, [auth]);

const authState: AuthState = { auth, osmUsername };
const authState: AuthState = useMemo<AuthState>(
() => ({ auth, osmUsername }),
[osmUsername, auth],
);

const appContext = useMemo(
() => ({
Expand Down
35 changes: 22 additions & 13 deletions src/components/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import maplibregl, {
type MapMouseEvent,
} from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import React, { type FC, useEffect, useRef, useState } from "react";
import React, {
type FC,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { useTranslation } from "react-i18next";
import { useAppContext } from "~/appContext";
import { fetchCountriesData, fetchNodeDataFromBackend } from "~/backend";
Expand Down Expand Up @@ -182,18 +188,21 @@ const MapView: FC<MapViewProps> = ({ openChangesetId, setOpenChangesetId }) => {
fetchData().catch(console.error);
}, [language, setCountriesDataLanguage, setCountriesData]);

function addMaplibreGeocoder(map: maplibregl.Map) {
if (maplibreGeocoderRef.current !== null) {
map.removeControl(maplibreGeocoderRef.current);
}
const newMaplibreGeocoder = new MaplibreGeocoder(nominatimGeocoder, {
maplibregl,
placeholder: t("sidebar.find_location"),
});
newMaplibreGeocoder.setLanguage(language);
map.addControl(newMaplibreGeocoder);
maplibreGeocoderRef.current = newMaplibreGeocoder;
}
const addMaplibreGeocoder = useCallback(
(map: maplibregl.Map) => {
if (maplibreGeocoderRef.current !== null) {
map.removeControl(maplibreGeocoderRef.current);
}
const newMaplibreGeocoder = new MaplibreGeocoder(nominatimGeocoder, {
maplibregl,
placeholder: t("sidebar.find_location"),
});
newMaplibreGeocoder.setLanguage(language);
map.addControl(newMaplibreGeocoder);
maplibreGeocoderRef.current = newMaplibreGeocoder;
},
[t, language],
);

useEffect(() => {
if (mapContainer.current === null) return;
Expand Down

0 comments on commit ea7e2c8

Please sign in to comment.