|
| 1 | +import { EquipmentProperties, PlaceProperties } from "@sozialhelden/a11yjson"; |
| 2 | +import "focus-visible"; |
| 3 | +import findIndex from "lodash/findIndex"; |
| 4 | +import includes from "lodash/includes"; |
| 5 | +import * as React from "react"; |
| 6 | +import "../../lib/NativeAppStrings"; |
| 7 | +import { hasBigViewport } from "../../lib/ViewportSize"; |
| 8 | +import config from "../../lib/config"; |
| 9 | +import savedState, { saveState } from "../../lib/savedState"; |
| 10 | +import { isTouchDevice } from "../../lib/userAgent"; |
| 11 | +import { PlaceDetailsProps } from "../app/PlaceDetailsProps"; |
| 12 | +import MainView, { UnstyledMainView } from "./MainView"; |
| 13 | + |
| 14 | +interface Props extends PlaceDetailsProps { |
| 15 | + className?: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface State { |
| 19 | + isMainMenuOpen: boolean; |
| 20 | + isSearchBarVisible: boolean; |
| 21 | + isOnSmallViewport: boolean; |
| 22 | + isSearchToolbarExpanded: boolean; |
| 23 | +} |
| 24 | + |
| 25 | +function isStickySearchBarSupported() { |
| 26 | + return hasBigViewport() && !isTouchDevice(); |
| 27 | +} |
| 28 | + |
| 29 | +class App extends React.Component<Props, State> { |
| 30 | + props: Props; |
| 31 | + |
| 32 | + state: State = { |
| 33 | + lat: null, |
| 34 | + lon: null, |
| 35 | + isSpecificLatLonProvided: false, |
| 36 | + zoom: null, |
| 37 | + isSearchBarVisible: isStickySearchBarSupported(), |
| 38 | + isMainMenuOpen: false, |
| 39 | + modalNodeState: null, |
| 40 | + isOnSmallViewport: false, |
| 41 | + isSearchToolbarExpanded: false, |
| 42 | + }; |
| 43 | + |
| 44 | + map: any; |
| 45 | + |
| 46 | + mainView: UnstyledMainView; |
| 47 | + |
| 48 | + static getDerivedStateFromProps(props: Props, state: State): Partial<State> { |
| 49 | + const newState: Partial<State> = { |
| 50 | + isSearchToolbarExpanded: false, |
| 51 | + isSearchBarVisible: isStickySearchBarSupported(), |
| 52 | + }; |
| 53 | + |
| 54 | + const parsedZoom = |
| 55 | + typeof props.zoom === "string" ? parseInt(props.zoom, 10) : null; |
| 56 | + const parsedLat = |
| 57 | + typeof props.lat === "string" ? parseFloat(props.lat) : null; |
| 58 | + const parsedLon = |
| 59 | + typeof props.lon === "string" ? parseFloat(props.lon) : null; |
| 60 | + |
| 61 | + newState.extent = state.extent || props.extent || null; |
| 62 | + newState.zoom = |
| 63 | + state.zoom || |
| 64 | + parsedZoom || |
| 65 | + Number.parseInt(savedState.map.lastZoom, 10) || |
| 66 | + null; |
| 67 | + newState.lat = |
| 68 | + state.lat || |
| 69 | + parsedLat || |
| 70 | + (savedState.map.lastCenter && |
| 71 | + Number.parseFloat(savedState.map.lastCenter[0])) || |
| 72 | + null; |
| 73 | + newState.lon = |
| 74 | + state.lon || |
| 75 | + parsedLon || |
| 76 | + (savedState.map.lastCenter && |
| 77 | + Number.parseFloat(savedState.map.lastCenter[1])) || |
| 78 | + null; |
| 79 | + |
| 80 | + newState.isSpecificLatLonProvided = |
| 81 | + Boolean(parsedLat) && Boolean(parsedLon); |
| 82 | + |
| 83 | + return newState; |
| 84 | + } |
| 85 | + |
| 86 | + onMoveEnd = (state: Partial<State>) => { |
| 87 | + let { zoom, lat, lon } = state; |
| 88 | + |
| 89 | + // Adjust zoom level to be stored in the local storage to make sure the user can |
| 90 | + // see some places when reloading the app after some time. |
| 91 | + const lastZoom = String( |
| 92 | + Math.max( |
| 93 | + zoom || 0, |
| 94 | + config.minZoomWithSetCategory, |
| 95 | + config.minZoomWithoutSetCategory |
| 96 | + ) |
| 97 | + ); |
| 98 | + |
| 99 | + saveState({ |
| 100 | + "map.lastZoom": lastZoom, |
| 101 | + "map.lastCenter.lat": String(lat), |
| 102 | + "map.lastCenter.lon": String(lon), |
| 103 | + "map.lastMoveDate": new Date().toString(), |
| 104 | + }); |
| 105 | + |
| 106 | + this.setState({ extent: null, lat, lon, zoom }); |
| 107 | + }; |
| 108 | + |
| 109 | + onMapClick = () => { |
| 110 | + if (this.state.isSearchToolbarExpanded) { |
| 111 | + this.closeSearch(); |
| 112 | + this.mainView && this.mainView.focusMap(); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + showSelectedFeature = ( |
| 117 | + featureId: string | number, |
| 118 | + properties?: PlaceProperties | EquipmentProperties | null |
| 119 | + ) => { |
| 120 | + if (!featureId) { |
| 121 | + debugger; |
| 122 | + } |
| 123 | + const featureIdString = featureId.toString(); |
| 124 | + const { routerHistory } = this.props; |
| 125 | + |
| 126 | + // show equipment inside their place details |
| 127 | + let routeName = "placeDetail"; |
| 128 | + const params = this.getCurrentParams() as any; |
| 129 | + |
| 130 | + params.id = featureIdString; |
| 131 | + delete params.eid; |
| 132 | + |
| 133 | + if ( |
| 134 | + ["elevator", "escalator"].includes(properties.category) && |
| 135 | + properties.placeInfoId |
| 136 | + ) { |
| 137 | + const placeInfoId = properties.placeInfoId; |
| 138 | + if (includes(["elevator", "escalator"], properties.category)) { |
| 139 | + routeName = "equipment"; |
| 140 | + params.id = placeInfoId; |
| 141 | + params.eid = featureIdString; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + let activeCluster = null; |
| 146 | + if (this.state.activeCluster) { |
| 147 | + const index = findIndex( |
| 148 | + this.state.activeCluster.features, |
| 149 | + // @ts-ignore |
| 150 | + (f) => (f.id || f._id) === featureIdString |
| 151 | + ); |
| 152 | + activeCluster = index !== -1 ? this.state.activeCluster : null; |
| 153 | + } |
| 154 | + |
| 155 | + this.setState({ activeCluster }, () => { |
| 156 | + routerHistory.push(routeName, params); |
| 157 | + }); |
| 158 | + }; |
| 159 | + |
| 160 | + render() { |
| 161 | + const { isSpecificLatLonProvided } = this.state; |
| 162 | + const isNodeRoute = Boolean(this.props.featureId); |
| 163 | + const mapMoveDate = savedState.map.lastMoveDate; |
| 164 | + const wasMapMovedRecently = |
| 165 | + mapMoveDate && +new Date() - +mapMoveDate < config.locateTimeout; |
| 166 | + const shouldLocateOnStart = |
| 167 | + !isSpecificLatLonProvided && !isNodeRoute && !wasMapMovedRecently; |
| 168 | + const isSearchBarVisible = this.state.isSearchBarVisible; |
| 169 | + |
| 170 | + return ( |
| 171 | + <> |
| 172 | + <GlobalStyle /> |
| 173 | + <MainView |
| 174 | + {...extraProps} |
| 175 | + ref={(mainView) => { |
| 176 | + this.mainView = mainView; |
| 177 | + }} |
| 178 | + /> |
| 179 | + </> |
| 180 | + ); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +export default App; |
0 commit comments