|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useMemo, |
| 4 | + useState, |
| 5 | +} from 'react'; |
| 6 | +import { |
| 7 | + Container, |
| 8 | + NumberOutput, |
| 9 | + TextOutput, |
| 10 | +} from '@ifrc-go/ui'; |
| 11 | +import { useTranslation } from '@ifrc-go/ui/hooks'; |
| 12 | +import { maxSafe } from '@ifrc-go/ui/utils'; |
| 13 | +import { |
| 14 | + _cs, |
| 15 | + isDefined, |
| 16 | + isNotDefined, |
| 17 | + listToMap, |
| 18 | +} from '@togglecorp/fujs'; |
| 19 | +import { |
| 20 | + MapBounds, |
| 21 | + MapLayer, |
| 22 | + MapSource, |
| 23 | +} from '@togglecorp/re-map'; |
| 24 | +import { type CirclePaint } from 'mapbox-gl'; |
| 25 | + |
| 26 | +import GlobalMap from '#components/domain/GlobalMap'; |
| 27 | +import Link from '#components/Link'; |
| 28 | +import MapContainerWithDisclaimer from '#components/MapContainerWithDisclaimer'; |
| 29 | +import MapPopup from '#components/MapPopup'; |
| 30 | +import useCountry from '#hooks/domain/useCountry'; |
| 31 | +import { |
| 32 | + DEFAULT_MAP_PADDING, |
| 33 | + DURATION_MAP_ZOOM, |
| 34 | +} from '#utils/constants'; |
| 35 | +import { getCountryListBoundingBox } from '#utils/map'; |
| 36 | +import { type GoApiResponse } from '#utils/restRequest'; |
| 37 | + |
| 38 | +import i18n from './i18n.json'; |
| 39 | +import styles from './styles.module.css'; |
| 40 | + |
| 41 | +type OperationLearningStatsResponse = GoApiResponse<'/api/v2/ops-learning/stats/'>; |
| 42 | +const sourceOptions: mapboxgl.GeoJSONSourceRaw = { |
| 43 | + type: 'geojson', |
| 44 | +}; |
| 45 | + |
| 46 | +interface CountryProperties { |
| 47 | + countryId: number; |
| 48 | + name: string; |
| 49 | + learningCount: number; |
| 50 | +} |
| 51 | +interface ClickedPoint { |
| 52 | + feature: GeoJSON.Feature<GeoJSON.Point, CountryProperties>; |
| 53 | + lngLat: mapboxgl.LngLatLike; |
| 54 | +} |
| 55 | + |
| 56 | +const MIN_LEARNING_COUNT = 0; |
| 57 | +const LEARNING_COUNT_LOW_COLOR = 'var(--go-ui-color-blue-30)'; |
| 58 | +const LEARNING_COUNT_HIGH_COLOR = 'var(--go-ui-color-blue-90)'; |
| 59 | + |
| 60 | +interface Props { |
| 61 | + className?: string; |
| 62 | + learningByCountry: OperationLearningStatsResponse['learning_by_country'] | undefined; |
| 63 | +} |
| 64 | + |
| 65 | +function OperationalLearningMap(props: Props) { |
| 66 | + const strings = useTranslation(i18n); |
| 67 | + const { |
| 68 | + className, |
| 69 | + learningByCountry, |
| 70 | + } = props; |
| 71 | + |
| 72 | + const [ |
| 73 | + clickedPointProperties, |
| 74 | + setClickedPointProperties, |
| 75 | + ] = useState<ClickedPoint | undefined>(); |
| 76 | + |
| 77 | + const countries = useCountry(); |
| 78 | + |
| 79 | + const countriesMap = useMemo(() => ( |
| 80 | + listToMap(countries, (country) => country.id) |
| 81 | + ), [countries]); |
| 82 | + |
| 83 | + const learningCountGeoJSON = useMemo( |
| 84 | + (): GeoJSON.FeatureCollection<GeoJSON.Geometry> | undefined => { |
| 85 | + if ((countries?.length ?? 0) < 1 || (learningByCountry?.length ?? 0) < 1) { |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + |
| 89 | + const features = learningByCountry |
| 90 | + .map((value) => { |
| 91 | + const country = countriesMap?.[value.country_id]; |
| 92 | + if (isNotDefined(country)) { |
| 93 | + return undefined; |
| 94 | + } |
| 95 | + return { |
| 96 | + type: 'Feature' as const, |
| 97 | + geometry: country.centroid as { |
| 98 | + type: 'Point', |
| 99 | + coordinates: [number, number], |
| 100 | + }, |
| 101 | + properties: { |
| 102 | + countryId: country.id, |
| 103 | + name: country.name, |
| 104 | + learningCount: value.count, |
| 105 | + }, |
| 106 | + }; |
| 107 | + }) |
| 108 | + .filter(isDefined) ?? []; |
| 109 | + |
| 110 | + return { |
| 111 | + type: 'FeatureCollection', |
| 112 | + features, |
| 113 | + }; |
| 114 | + }, |
| 115 | + [learningByCountry, countriesMap, countries], |
| 116 | + ); |
| 117 | + |
| 118 | + const bluePointHaloCirclePaint: CirclePaint = useMemo(() => { |
| 119 | + const countriesWithLearning = learningByCountry?.filter((value) => value.count > 0); |
| 120 | + |
| 121 | + const maxScaleValue = countriesWithLearning && countriesWithLearning.length > 0 |
| 122 | + ? Math.max( |
| 123 | + ...(countriesWithLearning |
| 124 | + .map((country) => country.count)), |
| 125 | + ) |
| 126 | + : 0; |
| 127 | + |
| 128 | + return { |
| 129 | + 'circle-opacity': 0.9, |
| 130 | + 'circle-color': [ |
| 131 | + 'interpolate', |
| 132 | + ['linear'], |
| 133 | + ['number', ['get', 'learningCount']], |
| 134 | + 0, |
| 135 | + LEARNING_COUNT_LOW_COLOR, |
| 136 | + maxScaleValue, |
| 137 | + LEARNING_COUNT_HIGH_COLOR, |
| 138 | + ], |
| 139 | + 'circle-radius': [ |
| 140 | + 'interpolate', |
| 141 | + ['linear'], |
| 142 | + ['zoom'], |
| 143 | + 3, 10, |
| 144 | + 8, 15, |
| 145 | + ], |
| 146 | + }; |
| 147 | + }, [learningByCountry]); |
| 148 | + |
| 149 | + const handlePointClose = useCallback(() => { |
| 150 | + setClickedPointProperties(undefined); |
| 151 | + }, []); |
| 152 | + |
| 153 | + const handlePointClick = useCallback( |
| 154 | + (feature: mapboxgl.MapboxGeoJSONFeature, lngLat: mapboxgl.LngLatLike) => { |
| 155 | + setClickedPointProperties({ |
| 156 | + feature: feature as unknown as ClickedPoint['feature'], |
| 157 | + lngLat, |
| 158 | + }); |
| 159 | + return true; |
| 160 | + }, |
| 161 | + [setClickedPointProperties], |
| 162 | + ); |
| 163 | + |
| 164 | + const maxLearning = useMemo(() => ( |
| 165 | + maxSafe( |
| 166 | + learningByCountry?.map((value) => value.count), |
| 167 | + ) |
| 168 | + ), [learningByCountry]); |
| 169 | + |
| 170 | + const bounds = useMemo( |
| 171 | + () => { |
| 172 | + if (isNotDefined(learningByCountry)) { |
| 173 | + return undefined; |
| 174 | + } |
| 175 | + const countryList = learningByCountry |
| 176 | + .map((d) => countriesMap?.[d.country_id]) |
| 177 | + .filter(isDefined); |
| 178 | + return getCountryListBoundingBox(countryList); |
| 179 | + }, |
| 180 | + [countriesMap, learningByCountry], |
| 181 | + ); |
| 182 | + |
| 183 | + return ( |
| 184 | + <Container |
| 185 | + className={_cs(styles.operationLearningMap, className)} |
| 186 | + footerClassName={styles.footer} |
| 187 | + footerContent={( |
| 188 | + <div className={styles.legend}> |
| 189 | + <div className={styles.legendLabel}>{strings.learningCount}</div> |
| 190 | + <div className={styles.legendContent}> |
| 191 | + <div |
| 192 | + className={styles.gradient} |
| 193 | + style={{ background: `linear-gradient(90deg, ${LEARNING_COUNT_LOW_COLOR}, ${LEARNING_COUNT_HIGH_COLOR})` }} |
| 194 | + /> |
| 195 | + <div className={styles.labelList}> |
| 196 | + <NumberOutput |
| 197 | + value={MIN_LEARNING_COUNT} |
| 198 | + /> |
| 199 | + <NumberOutput |
| 200 | + value={maxLearning} |
| 201 | + /> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + </div> |
| 205 | + )} |
| 206 | + childrenContainerClassName={styles.mainContent} |
| 207 | + > |
| 208 | + <GlobalMap> |
| 209 | + <MapContainerWithDisclaimer |
| 210 | + className={styles.mapContainer} |
| 211 | + title={strings.downloadMapTitle} |
| 212 | + /> |
| 213 | + {isDefined(learningCountGeoJSON) && ( |
| 214 | + <MapSource |
| 215 | + sourceKey="points" |
| 216 | + sourceOptions={sourceOptions} |
| 217 | + geoJson={learningCountGeoJSON} |
| 218 | + > |
| 219 | + <MapLayer |
| 220 | + layerKey="points-halo-circle" |
| 221 | + onClick={handlePointClick} |
| 222 | + layerOptions={{ |
| 223 | + type: 'circle', |
| 224 | + paint: bluePointHaloCirclePaint, |
| 225 | + }} |
| 226 | + /> |
| 227 | + </MapSource> |
| 228 | + )} |
| 229 | + {clickedPointProperties?.lngLat && ( |
| 230 | + <MapPopup |
| 231 | + onCloseButtonClick={handlePointClose} |
| 232 | + coordinates={clickedPointProperties.lngLat} |
| 233 | + heading={( |
| 234 | + <Link |
| 235 | + to="countriesLayout" |
| 236 | + urlParams={{ |
| 237 | + countryId: clickedPointProperties.feature.properties.countryId, |
| 238 | + }} |
| 239 | + > |
| 240 | + {clickedPointProperties.feature.properties.name} |
| 241 | + </Link> |
| 242 | + )} |
| 243 | + childrenContainerClassName={styles.popupContent} |
| 244 | + > |
| 245 | + <Container |
| 246 | + headingLevel={5} |
| 247 | + > |
| 248 | + <TextOutput |
| 249 | + value={clickedPointProperties.feature.properties.learningCount} |
| 250 | + label={strings.learningCount} |
| 251 | + valueType="number" |
| 252 | + /> |
| 253 | + </Container> |
| 254 | + </MapPopup> |
| 255 | + )} |
| 256 | + {isDefined(bounds) && ( |
| 257 | + <MapBounds |
| 258 | + duration={DURATION_MAP_ZOOM} |
| 259 | + bounds={bounds} |
| 260 | + padding={DEFAULT_MAP_PADDING} |
| 261 | + /> |
| 262 | + )} |
| 263 | + </GlobalMap> |
| 264 | + </Container> |
| 265 | + ); |
| 266 | +} |
| 267 | + |
| 268 | +export default OperationalLearningMap; |
0 commit comments