|
| 1 | +import { useCallback } from 'react' |
| 2 | + |
| 3 | +import { ViewDispatch } from '@eplant/View' |
| 4 | +import { useTheme } from '@mui/material' |
| 5 | +import { |
| 6 | + APIProvider, |
| 7 | + Map, |
| 8 | + MapCameraChangedEvent, |
| 9 | + MapEvent, |
| 10 | + useMap, |
| 11 | +} from '@vis.gl/react-google-maps' |
| 12 | + |
| 13 | +import { getColor } from '../eFP/svg' |
| 14 | +import GeneDistributionChart from '../eFP/Viewer/GeneDistributionChart' |
| 15 | +import Legend from '../eFP/Viewer/legend' |
| 16 | + |
| 17 | +import MapMarker from './MapMarker' |
| 18 | +import { WorldEFPAction, WorldEFPData, WorldEFPState } from './types' |
| 19 | + |
| 20 | +interface MapContainerProps { |
| 21 | + activeData: WorldEFPData |
| 22 | + state: WorldEFPState |
| 23 | + dispatch: ViewDispatch<WorldEFPAction> |
| 24 | +} |
| 25 | +const MapContainer = ({ activeData, state, dispatch }: MapContainerProps) => { |
| 26 | + const theme = useTheme() |
| 27 | + const map = useMap('WorldEFP') |
| 28 | + |
| 29 | + const hangleDragEnd = (event: MapEvent) => { |
| 30 | + const mapPos = map?.getCenter() |
| 31 | + if (!mapPos) return |
| 32 | + |
| 33 | + const coords = { lat: mapPos.lat(), lng: mapPos.lng() } |
| 34 | + dispatch({ |
| 35 | + type: 'set-map-position', |
| 36 | + position: coords, |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + const handleZoom = (event: MapCameraChangedEvent) => { |
| 41 | + dispatch({ |
| 42 | + type: 'set-map-zoom', |
| 43 | + zoom: event.detail.zoom, |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <Map |
| 49 | + defaultCenter={state.position} |
| 50 | + defaultZoom={2} |
| 51 | + mapId={import.meta.env.VITE_MAP_ID} |
| 52 | + streetViewControl={false} |
| 53 | + mapTypeId={'roadmap'} |
| 54 | + mapTypeControl={false} |
| 55 | + onDragend={hangleDragEnd} |
| 56 | + onZoomChanged={handleZoom} |
| 57 | + id='WorldEFP' |
| 58 | + > |
| 59 | + {activeData.positions.map((pos, index) => { |
| 60 | + const color = getColor( |
| 61 | + activeData.efpData.groups[index].mean, |
| 62 | + activeData.efpData.groups[index], |
| 63 | + 1, |
| 64 | + theme, |
| 65 | + state.colorMode, |
| 66 | + activeData.efpData.groups[index].std, |
| 67 | + state.maskThreshold, |
| 68 | + state.maskingEnabled |
| 69 | + ) |
| 70 | + return ( |
| 71 | + <MapMarker |
| 72 | + theme={theme} |
| 73 | + key={index} |
| 74 | + color={color} |
| 75 | + data={activeData.efpData.groups[index]} |
| 76 | + position={pos} |
| 77 | + ></MapMarker> |
| 78 | + ) |
| 79 | + })} |
| 80 | + <Legend |
| 81 | + sx={(theme) => ({ |
| 82 | + position: 'absolute', |
| 83 | + left: theme.spacing(2), |
| 84 | + bottom: theme.spacing(4), |
| 85 | + zIndex: 10, |
| 86 | + })} |
| 87 | + colorMode={'absolute'} |
| 88 | + data={activeData.efpData} |
| 89 | + ></Legend> |
| 90 | + <GeneDistributionChart data={activeData.efpData} /> |
| 91 | + </Map> |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +export default MapContainer |
0 commit comments