Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: 마커 업데이트 로직 리팩토링 #103

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4c7adb9
refactor: activate마커 핸들링 관련 코드 리팩토링
Sangjun-man May 11, 2023
7955314
feat: 즐겨찾기 필터링 버튼에 사용되는 이미지, 데이터 추가
Sangjun-man May 11, 2023
86c8eab
Merge branch 'main' into 94/feat
Sangjun-man May 11, 2023
569e71a
refactor: activeCafeMarker 로직 useActiveCafeMarker hooks로 분리
Sangjun-man May 11, 2023
f698152
refactor: 사용하지 않는 util 함수 제거
Sangjun-man May 11, 2023
6d91f5e
fix: cameraMove 버그 수정
Sangjun-man May 11, 2023
44b5c49
refactor: myLocationMarker 관련 코드 리팩토링
Sangjun-man May 11, 2023
061f461
refactor: 내위치 찾기 마커 로직 useMyLocationMarker 훅으로 분리
Sangjun-man May 17, 2023
851e555
refactor: map source 가져오는 로직 useMapSources로 분리
Sangjun-man May 17, 2023
1fa3f74
refactor : 필터링 기능 source에서 하는게 아닌 update마커에서 하도록 수정
Sangjun-man May 17, 2023
370fc79
refactor: mapBox eventlistenter 추가하는 로직 리팩토링
Sangjun-man May 26, 2023
d832240
refactor: mapEventListner callback에 Map 파라미터 전달
Sangjun-man May 26, 2023
687a9d5
refactor: 기기에 따라 다른 map 로드하도록 수정
Sangjun-man May 26, 2023
0226ab9
refactor: 소스 추가방식 수정, useUpdateMarker 리팩토링
Sangjun-man May 26, 2023
5010c1d
refactor: useMarkerUpdate 변수명 변경, 주석 수정
Sangjun-man May 27, 2023
ee3020c
refactor: 맵 source, layer 생성 및 변경 로직 과정 리팩토링
Sangjun-man May 27, 2023
9b38b94
refactor: useMapEventListenr 파라미터 전달 방식 변경
Sangjun-man May 27, 2023
d18c36d
style: useMapSourceAndLayer console.log 제거
Sangjun-man May 27, 2023
66842c8
Merge branch 'main' into 94/feat
Sangjun-man May 27, 2023
14518f6
feat: 맵이 회전하고 있다면 마커 업데이트 하지 않음
yeoularu Jun 6, 2023
a504263
Merge branch 'main' into 101/feat
yeoularu Jun 8, 2023
3891f5d
refactor: 이전 커밋 롤백(맵이 회전하면 마커 업데이트 하지 않음)
yeoularu Jun 8, 2023
61c1219
Merge branch 'main' into 101/feat
yeoularu Jun 8, 2023
e583102
refactor: 플러터에서 받은 스트링화된 즐겨찾기 배열 처리 유틸함수로 만들어 적용
yeoularu Jun 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/assets/img/FavoriteColoredIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/FavoriteGrayIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export { default as ReasonableColoredIcon } from './img/ReasonableColoredIcon.pn
export { default as ReasonableGrayIcon } from './img/ReasonableGrayIcon.png';
export { default as StudyColoredIcon } from './img/StudyColoredIcon.png';
export { default as StudyGrayIcon } from './img/StudyGrayIcon.png';
export { default as FavoirteColoredIcon } from './img/FavoriteColoredIcon.png';
export { default as FavoriteGrayIcon } from './img/FavoriteGrayIcon.png';
39 changes: 0 additions & 39 deletions src/components/MapComp/eventHandler/addFeatureLayer.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/components/MapComp/hooks/useActivateCafeMarker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useRecoilValue } from 'recoil';
import useMap from './useMap';
import { useEffect } from 'react';
import { activatedCafeIdAtom } from '@states/infoWindow';
import { CustomGeoJSONFeatures } from '@libs/types/map';

interface UseActivateCafeMarkerHookProps {
add: (features: CustomGeoJSONFeatures) => void;
remove: () => void;
features: CustomGeoJSONFeatures[] | CustomGeoJSONFeatures[][];
removeCondition?: boolean[];
}

function useActivateCafeMarker({ add, remove, features, removeCondition }: UseActivateCafeMarkerHookProps) {
const activatedCafeId = useRecoilValue(activatedCafeIdAtom);
const allFeatures = features.flat();
const mapRef = useMap();

useEffect(() => {
const map = mapRef.current;
if (!map) return;

const activatedCafeFeature = allFeatures.find((feature) => feature.properties.cafeId === activatedCafeId);
if (!activatedCafeFeature) return;

add(activatedCafeFeature);

return () => {
remove();
};
}, [activatedCafeId]);

useEffect(() => {
if (removeCondition && removeCondition.find((value) => value === true)) remove();
}, [removeCondition]);
}
export default useActivateCafeMarker;
80 changes: 80 additions & 0 deletions src/components/MapComp/hooks/useAddSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { MAP_SOURCE_RENDER_CAFE_LIST } from '@constants/map';
import { CustomGeoJSONFeatures } from '@libs/types/map';
import { FeatureCollection } from 'geojson';
import { MapboxEvent } from 'mapbox-gl';
import useMapEventListner from './useMapEventListner';
import { useEffect, useState } from 'react';
import useMap from './useMap';

interface UseMapSourceProps {
filteredFeatures: CustomGeoJSONFeatures[];
}
function useMapSourceAndLayer({ filteredFeatures }: UseMapSourceProps, deps: any[]) {
const mapRef = useMap();
const [loaded, setLoaded] = useState(false);
const onMapLoad = ({ target: targetMap }: MapboxEvent) => {
useMapSourceAndLayer.addSourceAndLayer({
map: targetMap,
sourceFeatures: filteredFeatures,
name: MAP_SOURCE_RENDER_CAFE_LIST,
});
setLoaded(true);
};

useMapEventListner(
{
type: 'load',
callback: onMapLoad,
effect: (map) => {
if (map.getLayer(MAP_SOURCE_RENDER_CAFE_LIST)) {
map.removeLayer(MAP_SOURCE_RENDER_CAFE_LIST);
map.removeSource(MAP_SOURCE_RENDER_CAFE_LIST);
}
},
},
[]
);

useEffect(() => {
const map = mapRef.current;
if (!(map && loaded)) return;
if (map.getLayer(MAP_SOURCE_RENDER_CAFE_LIST)) {
map.removeLayer(MAP_SOURCE_RENDER_CAFE_LIST);
map.removeSource(MAP_SOURCE_RENDER_CAFE_LIST);
}
useMapSourceAndLayer.addSourceAndLayer({
map,
sourceFeatures: filteredFeatures,
name: MAP_SOURCE_RENDER_CAFE_LIST,
});
}, [...deps, loaded]);
}

export default useMapSourceAndLayer;

interface AddSourceAndLayerProps {
map: mapboxgl.Map;
sourceFeatures: CustomGeoJSONFeatures[];
name: string;
}

useMapSourceAndLayer.addSourceAndLayer = ({ map, sourceFeatures, name }: AddSourceAndLayerProps) => {
map.addSource(name, {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: sourceFeatures,
} as FeatureCollection,
cluster: true,
clusterMaxZoom: 18,
clusterRadius: 60,
});
map.addLayer({
id: name,
type: 'circle',
source: name,
paint: {
'circle-opacity': 0,
},
});
};
3 changes: 2 additions & 1 deletion src/components/MapComp/hooks/useCameraMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ function useCameraMove() {

prevCameraState.current = currentCameraState.current;
const nextState = { ...prevCameraState.current, ...tiltNextState({ coordinate }), ...option };
currentCameraState.current = nextState;

map.flyTo(nextState);

currentCameraState.current = { ...nextState, pitch: 0, bearing: 0 };
};

const flyToPrev = () => {
Expand Down
26 changes: 26 additions & 0 deletions src/components/MapComp/hooks/useFiltering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FilterId } from '@libs/types/filter';
import { CustomGeoJSONFeatures } from '@libs/types/map';
import { activeFilterIdAtom } from '@states/clusterList';
import { geoJsonAtom } from '@states/map';
import { useRecoilValue } from 'recoil';

function useFiltering() {
const allFeatures = useRecoilValue(geoJsonAtom);
const activeFilterId = useRecoilValue(activeFilterIdAtom);

const filteredFeatures = useFiltering.filterFeatures({ features: allFeatures, filterId: activeFilterId });

return [activeFilterId, filteredFeatures] as [FilterId, CustomGeoJSONFeatures[]];
}
export default useFiltering;

useFiltering.filterFeatures = ({ features, filterId }: { features: CustomGeoJSONFeatures[]; filterId: FilterId }) => {
const filteredFeatures: CustomGeoJSONFeatures[] = [];

features.forEach((feature: CustomGeoJSONFeatures) => {
if (feature.properties?.filterList.includes(filterId)) {
filteredFeatures.push(feature);
}
});
return filteredFeatures;
};
22 changes: 15 additions & 7 deletions src/components/MapComp/hooks/useMap.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import detectDevice from '@libs/utils/detectDevice';
import mapboxgl, { Map, MapboxOptions } from 'mapbox-gl';
import { useEffect, useState } from 'react';

Expand All @@ -6,13 +7,19 @@ import { useEffect, useState } from 'react';
// eslint-disable-next-line import/no-webpack-loader-syntax, @typescript-eslint/no-var-requires
mapboxgl.workerClass = require('worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker').default;

export const mapConfig: MapboxOptions = {
const style = {
mobile: 'mapbox://styles/sangjun/cli3dxlnw00xl01rh67rm3rbf', // layer 53
desktop: 'mapbox://styles/sangjun/cli3dqu07007901rb6l456z76', // layer 104
origin: 'mapbox://styles/sangjun/clgwfbvxr003s01r88wfc14jo', // layer 134
};

export const mapConfig: (deviceType: 'mobile' | 'desktop') => MapboxOptions = (deviceType) => ({
container: 'map',
style: 'mapbox://styles/sangjun/clgwfbvxr003s01r88wfc14jo',
style: `${style[deviceType]}?optimize=true`,
center: [127.0770389, 37.6257614],
zoom: 17,
maxZoom: 18,
minZoom: 13,
minZoom: 15,
maxBounds: [
[127.05, 37.59],
[127.09, 37.65],
Expand All @@ -21,7 +28,7 @@ export const mapConfig: MapboxOptions = {
name: 'lambertConformalConic',
parallels: [36, 35],
},
};
});

/**
* mapboxgl.Map 인스턴스 저장 객체
Expand All @@ -33,19 +40,20 @@ const mapRef: { current: Map | null } = { current: null };
* @returns Map | null
*
*/
function useMapRef() {
function useMap() {
const [, setLoad] = useState(false);

useEffect(() => {
const device = detectDevice();
if (mapRef.current instanceof Map) return;
try {
mapboxgl.accessToken = `${process.env.REACT_APP_MAPBOX_ACCESS_TOKKEN}`;
mapRef.current = new mapboxgl.Map(mapConfig);
mapRef.current = new mapboxgl.Map(mapConfig(device));
setLoad(true);
} catch (e) {
/* empty */
}
}, []);
return mapRef;
}
export default useMapRef;
export default useMap;
26 changes: 26 additions & 0 deletions src/components/MapComp/hooks/useMapEventListner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect } from 'react';
import useMap from './useMap';
import mapboxgl, { EventData, MapEventType, MapboxEvent } from 'mapbox-gl';

interface UseMapEventListnerProps {
type: keyof MapEventType;
callback: (e: MapboxEvent<undefined> & EventData) => void;
effect: (map: mapboxgl.Map) => void;
}
function useMapEventListner({ type, callback, effect }: UseMapEventListnerProps, dep: any[]) {
const mapRef = useMap();

useEffect(() => {
const map = mapRef.current;
if (!map) return;

effect(map);
map.on(type, callback);

return () => {
map.off(type, callback);
};
}, dep);
}

export default useMapEventListner;
Loading